简体   繁体   English

单击按钮以使文本出现在另一个表单文本框中

[英]Click button for a text to appear in a another form textbox

I am using Visual Studio. 我正在使用Visual Studio。 I have 2 forms (formA and formB). 我有2种形式(formA和formB)。 There is a button on formA. 在formA上有一个按钮。 When I click it, I want a word to appear on formB. 当我单击它时,我希望一个单词出现在formB上。 This is what I have in formA.cs. 这就是formA.cs中的内容。 FormB is the public class name in formB. FormB是formB中的公共类名称。

 private void button1_Click(object sender, System.EventArgs e)
    {
        FormB search1 = new FormB();
        search1.TextBox.Text = "test123";
    }

However, when I run my program, no text appears in formB. 但是,当我运行程序时,formB中没有文本。 Can you please help? 你能帮忙吗?

Make a constructor on FormB with parameter; 使用参数在FormB上构造一个构造函数;

public FormB(string str)
{
  InitializeComponent();
  textBox1.Text = str;
}

then from FormA initialize FormB and pass your parameter. 然后从FormA初始化FormB并传递您的参数。

private void button1_Click(object sender, System.EventArgs e)
{
FormB search1 = new FormB("test123");
search1.Show();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM