如何对按钮进行编码以在另一个窗体的文本框中输入值



我有带有 4 个按钮的表单 1,当我单击一个按钮时,它会打开一个新表单。每个按钮打开相同的表单,但我希望相应的按钮在表单 2 上的两个不同文本框中输入特定值。

表格 1 按钮 A;表格2 textbox1= 400 textbox2 =0.4

表格 1 按钮 B;表格2 textbox1= 350 textbox2 =0.9

表格 1 按钮 C;表格2 textbox1= 700 textbox2 =0.6

表格 1 按钮 D;表格2 textbox1= USER DEFINED textbox2 = USER DEFINED

我将如何去做

 //This is the current text
 // Form1:   
private void ButtonA_Click(object sender, EventArgs e)
    {
           Form2 numb = new form2();
           numb.FormClosed += new FormClosedEventHandler(numb_FormClosed);
           this.Hide();
           CalcForm.Show();
    }

你可以从第一个窗体设置所需文本框的值,如下所示,但在它之前,请确保已将该文本框设置为内部,以便您可以从第一个窗体(在 Form.Designer.cs 中(访问它:

internal System.Windows.Forms.TextBox textBox1;

private void ButtonA_Click(object sender, EventArgs e)
{
       Form2 numb = new form2();
       numb.FormClosed += new FormClosedEventHandler(numb_FormClosed);
       numb.textbox1.Text = "400";
       numb.textbox2.Text = "0.4";
       this.Hide();
       CalcForm.Show();
}

另一种方法是为 Form2 定义参数化构造函数,并在该构造函数中设置 TextBox 的值,如下所示:

public Form2(string a,string b)
{
    textBox1.Text = a;
    textBox2.Text = b;
}

private void ButtonA_Click(object sender, EventArgs e)
{
       Form2 numb = new form2("aaaa","bbbb");
       numb.FormClosed += new FormClosedEventHandler(numb_FormClosed);
       this.Hide();
       CalcForm.Show();
}

最新更新