(转载)C# winform 在一个窗体中如何设置另一个窗体的TextBox的值

方法1:修改控件的访问修饰符。(不建议使用此法)

public System.Windows.Forms.TextBox textBox1;

在调用时就能直接访问

Form1 frm = new Form1();
frm.textBox1.Text = "方法1";
frm.Show();

方法2:是通过构造函数/指定公开方法传入,然后为对应控件赋值。

public Form2(string text)
{
    InitializeComponent();
 
    this.textBox1.Text = text;
}

调用时

Form2 frm = new Form2("方法2");
frm.Show();

方法3:是通过公开属性来设置,此法甚好。

public string Text3
{
    get { return this.textBox1.Text; }
    set { this.textBox1.Text = value; }
}

调用如下

Form3 frm = new Form3();
frm.Text3 = "方法3";
frm.Show();

猜你喜欢

转载自www.cnblogs.com/cnwuchao/p/10586755.html