0

我对文本框有一个恼人的问题。我在一个文本框中输入数据,然后单击按钮,我需要在另一个文本框中显示结果。我真的不知道为什么我的方法不起作用。我没有收到任何错误,但它不会显示结果。谢谢!所有文本框都是在运行时动态创建的。这是代码:

     private void button2_Click(object sender, EventArgs e)
    {
        TextBox tbox8 = new TextBox();
        tbox8.Name = "textBox8";
        tbox8.Location = new System.Drawing.Point(54 + (0), 55);
        tbox8.Size = new System.Drawing.Size(53, 20);
        this.Controls.Add(tbox8);
        tbox8.BackColor = System.Drawing.SystemColors.InactiveCaption;
        tbox8.TextChanged += new EventHandler(tbox8_TextChanged);

        TextBox tbox9 = new TextBox();
        tbox9.Name = "textBox9";
        tbox9.Location = new System.Drawing.Point(54 + (60), 55);
        tbox9.Size = new System.Drawing.Size(53, 20);
        this.Controls.Add(tbox9);
        tbox9.BackColor = System.Drawing.SystemColors.InactiveCaption;
        tbox9.TextChanged += new EventHandler(tbox9_TextChanged);

     }//button_click

    //input data into texbox8
    TextBox tbox;
    double var1;
    private void tbox8_TextChanged(object sender, EventArgs e)
    {
        tbox = sender as TextBox;
        var1 = Convert.ToDouble(tbox.Text);
    }

    //display the result in textbox9
    TextBox tbox2;
    private void tbox9_TextChanged(object sender, EventArgs e)
    {
        tbox2 = sender as TextBox;
    }

    //perform calculation and on button click display data on referenced textbox
    private void button3_Click(object sender, EventArgs e)
    {
        double result2 = var1 * 2;
        if( tbox2 != null)
        {
            tbox2.Text = result2.ToString(); 
        }      
    }
4

3 回答 3

3

这是您的代码,注释掉了不必要的位,并添加了一些新行:

TextBox tbox8 = new TextBox(); //make it a member variable
TextBox tbox9 = new TextBox(); //same for this one

private void button2_Click(object sender, EventArgs e)
{
    tbox8.Name = "textBox8";
    tbox8.Location = new System.Drawing.Point(54 + (0), 55);
    tbox8.Size = new System.Drawing.Size(53, 20);
    this.Controls.Add(tbox8);
    tbox8.BackColor = System.Drawing.SystemColors.InactiveCaption;
    tbox8.TextChanged += new EventHandler(tbox8_TextChanged);

    tbox9.Name = "textBox9";
    tbox9.Location = new System.Drawing.Point(54 + (60), 55);
    tbox9.Size = new System.Drawing.Size(53, 20);
    this.Controls.Add(tbox9);
    tbox9.BackColor = System.Drawing.SystemColors.InactiveCaption;
    tbox9.TextChanged += new EventHandler(tbox9_TextChanged);

 }//button_click

//input data into texbox8
//TextBox tbox;
double var1;
private void tbox8_TextChanged(object sender, EventArgs e)
{
    //tbox = sender as TextBox;
    var1 = Convert.ToDouble(tbox8.Text);//tbox8
}

//display the result in textbox9
//TextBox tbox2;//tbox2 is actually tbox9 anyway
private void tbox9_TextChanged(object sender, EventArgs e)
{
//    tbox2 = sender as TextBox;//so we don't need a tbox2
    //do something
}

//perform calculation and on button click display data on referenced textbox
private void button3_Click(object sender, EventArgs e)
{
    double result2 = var1 * 2;
    //if( null != tbox9 ) 
    //{
        tbox9.Text = result2.ToString(); //changed to tbox9
    //}      
}

然而,这仍然存在问题。当用户多次单击 button2 时会发生什么?

于 2012-08-22T17:15:52.757 回答
0

假设 tbox8(尝试使用有意义的名称顺便说一句)是应该输入的文本,而 tbox14 是您期望输出的位置,为什么只在 tbox14 的 TextChanged 事件中设置 tbox2 变量?您的代码可能无法正常工作,因为 tbox2 始终为空,因为您从未为 tbox14 执行 TextChanged 事件。为什么你使用变量 tbox2 而不是只使用 tbox14 呢?

此外,在 tbox8 的 TextChanged 事件中解析文本可能是错误的地方。TextChanged 事件将在每次击键时触发。为什么不在 button3 Click 处理程序中只做一次呢?或者,当它失去焦点时这样做。

试试这个(注意:并不是说这仍然不是糟糕的代码)。首先,摆脱所有 TextChanged 事件,然后:

private void button3_Click(object sender, EventArgs e)
{
    var1 = Convert.ToDouble(tbox8.Text);
    double result2 = var1 * 2;
    tbox14.Text = result2.ToString();    
}
于 2012-08-22T17:16:16.953 回答
0

假设 result * 2 不会导致双精度数的算术溢出,这样的事情应该可以工作。如果输入tbox8的值不能解析成double,点击button3后tbox9显示的结果为0。

由于我没有使用 TextBox 的任何事件,因此我不需要为它们创建处理程序。在单击按钮2后,我还将禁用它,以便无法再次单击它。

如果您需要再次单击 button2,则需要从另一个按钮事件处理程序或其他东西启用它,以便您有机会在启用它(button2)之前从 this.Controls 集合中删除 tbox8 和 tbox9 以防止应用程序试图创建它们的其他副本。

private void button2_Click(object sender, EventArgs e) {
    button2.Enabled = false;
    TextBox tbox8 = new TextBox();
    tbox8.Name = "textBox8";
    tbox8.Location = new System.Drawing.Point(54 + (0), 55);
    tbox8.Size = new System.Drawing.Size(53, 20);
    this.Controls.Add(tbox8);
    tbox8.BackColor = System.Drawing.SystemColors.InactiveCaption;

    TextBox tbox9 = new TextBox();
    tbox9.Name = "textBox9";
    tbox9.Location = new System.Drawing.Point(54 + (60), 55);
    tbox9.Size = new System.Drawing.Size(53, 20);
    this.Controls.Add(tbox9);
    tbox9.BackColor = System.Drawing.SystemColors.InactiveCaption; }

private void button3_Click(object sender, EventArgs e) {
    double result = 0;
    double.TryParse(tbox8.Text, out result);
    tbox9.Text = (double)(result * 2).ToString(); }
于 2012-08-22T18:57:41.797 回答