【问题标题】:How to code a button to enter a value in a textbox on another form如何编写按钮以在另一个表单的文本框中输入值
【发布时间】:2019-08-17 22:43:39
【问题描述】:

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

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

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

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

表格 1 按钮 D;表单2textbox1= USER DEFINEDtextbox2 = 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();
    }

【问题讨论】:

  • 您可以为第二种形式创建参数化构造函数,为其提供值。

标签: c# button newforms


【解决方案1】:

您可以像下面那样从第一个表单设置所需文本框的值,但在此之前确保您已将该文本框设置为内部,以便您可以从第一个表单访问它(在 Form.Designer. c):

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();
}

【讨论】:

  • 我做了参数化的方式,它的工作!谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-10-18
  • 2011-10-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-05-17
  • 1970-01-01
相关资源
最近更新 更多