不添加到文本框

时间:2014-07-31 04:36:08

标签: c#

大家好,我基本上是在C#的第一步,我已经做了几个小时了。来自java我快速接受这个,但我最近偶然发现了一些不应该发生的事情。我在一个名为Input

的.cs中有这个
        frmCodeGenerator f = new frmCodeGenerator();

    private void cmdOkay_Click(object sender, EventArgs e)
    {
        f.updateString(int.Parse(txtInput.Text));
        this.Hide();
    }

现在不工作的部分是f.updateString

我曾尝试使用数字框,但这也不起作用。但是在frmCodeGenerator中我有4个按钮使用当前的updateString方法本身,它似乎正在运行,因为我也想要它。任何人都可以向我解释为什么这不起作用。

这是我的updateString方法,适合任何需要它的人

        public void updateString(int amount)
    {
        g.generateNumbers(amount);
        txtOutput.Text = g.print.ToString();
        g.print.Clear();
    }

此致。

这是我的两个班级

    using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class frmCodeGenerator : Form
    {

        GenerateCode g = new GenerateCode();

        public frmCodeGenerator()
        {
            InitializeComponent();
        }

        private void codeGen_Load(object sender, EventArgs e)
        {
            txtOutput.BackColor = Color.FromArgb(255, 255, 255, 196);
        }

        private void char8_Click(object sender, EventArgs e)
        {
            updateString(8);
        }

        private void char16_Click(object sender, EventArgs e)
        {
            updateString(16);
        }

        private void char32_Click(object sender, EventArgs e)
        {
            updateString(32);
        }

        private void char64_Click(object sender, EventArgs e)
        {
            updateString(64);
        }

        private void charInput_Click(object sender, EventArgs e)
        {
            Input i = new Input();
            i.Show();
        }

        public void updateString(int amount)
        {
            g.GenerateCodes(amount);
            txtOutput.Text = g.print.ToString();
            g.print.Clear();
        }

        private void txtOutput_TextChanged(object sender, EventArgs e)
        {

        }
    }
}

和另一个班级

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace WindowsFormsApplication1
{
    class GenerateCode
    {
        String[] avaliable = 
        { 
        "A", "B", "C", "D", "E", "F", "G", "H",
        "I", "J", "K", "L", "M", "N", "O", "P",
        "Q", "R", "S", "T", "U", "V", "W", "X",
        "Y", "Z", "0", "1", "2", "3", "4", "5",
        "6", "7", "8", "9"
        };

        public StringBuilder print = new StringBuilder();
        Random random = new Random();
        /**
         * Basic number generator with the amount which
         * is the number of times i add to the stringbuilder called print.
         * */
        public void GenerateCodes(int amount)
        {
            for (int i = 0; i < amount; i++)
            {
                print.Append(avaliable[random.Next(avaliable.Length)]);
            }
        }
    }
}

1 个答案:

答案 0 :(得分:2)

让我把你所拥有的东西拼凑起来:

带有文本框的输入表单,允许您输入数字:

public class Input:Form 
{
    frmCodeGenerator f = new frmCodeGenerator();

    private void cmdOkay_Click(object sender, EventArgs e)
    {
        f.updateString(int.Parse(txtInput.Text));
        this.Hide();
    }
}

你的主/开始表单frmCodeGenerator有一些文本框来显示生成的代码。您有一个按钮用于实例化,并使用以下代码显示输入表单:

    private void charInput_Click(object sender, EventArgs e)
    {
        Input i = new Input();
        i.Show();
    }

并且您希望在输入表单中输入的代码以frmGenerated形式显示。

问题是:您在输入WITHOUT中显示frmCodeGenerator的新实例。我假设您不想创建新表单,只是重新使用existimg表单。

您有两个选择:一个是创建输入ShowDialog并从属性中获取值,另一个是将frmCodeGenerator的引用传递给您输入表单。

选项1

创建一个公共属性并调用ShowDialog(以保持对输入表单的关注)

frmCodeGenerator更改

    private void charInput_Click(object sender, EventArgs e)
    {
        Input i = new Input();
        i.ShowDialog();
        updateString(i.Value);
    }

输入更改

public class Input:Form 
{
    public int Value { get; private set;}

    private void cmdOkay_Click(object sender, EventArgs e)
    {
        // improve to use Int32.TryParse
        Value = int.Parse(txtInput.Text));
        this.Hide();
    }
}

选项2

提供指向来电表格的链接......

frmCodeGenerator更改

    private void charInput_Click(object sender, EventArgs e)
    {
        Input i = new Input(this); // provide a reference to the current form
        i.Show();
    }

输入更改

public class Input:Form 
{
    private frmCodeGenerator myFrmCodeGenerator; // hold the caller
    // extra constructor
    public Input(frmCodeGenerator frm)
    {
       myFrmCodeGenerator = frm;
    }   

    private void cmdOkay_Click(object sender, EventArgs e)
    {
        // improve to use Int32.TryParse

        // call the method on our original form
        myFrmCodeGenerator.updateString(int.Parse(txtInput.Text));

        this.Hide();
    }
}

选项不太可能

使用最少量代码来修复当前版本中出现问题的修复

public class Input:Form 
{
    frmCodeGenerator f = new frmCodeGenerator();

    private void cmdOkay_Click(object sender, EventArgs e)
    {
        f.updateString(int.Parse(txtInput.Text));
        // this will Show your newly created Form but now you have two of them...
        f.Show(); 
        this.Hide();
    }
}