[英]How do i pass value from User Control to another User Control
I need to pass a string that from my first UserControl to the second UserControl.我需要将一个字符串从我的第一个 UserControl 传递到第二个 UserControl。 How do i do?
我该怎么做?
You can define custom property to achieve it.您可以定义自定义属性来实现它。
First, define property UC2
in Form1.cs
to access the SecondUC instance
.首先,在
Form1.cs
中定义属性UC2
以访问SecondUC instance
。
public SecondUC UC2
{
get { return secondUC1; }
set { secondUC1 = value; }
}
Then, define property TB2
in SecondUC.cs
to access the TextBox instance
that in SecondUC
.然后,在
SecondUC
中定义属性TB2
以访问SecondUC.cs
中的TextBox instance
。
public TextBox TB2
{
get { return textBox2; }
set { textBox2 = value; }
}
Last, set the TextBox2
vaule by clicking button in FirstUC
.最后,通过单击
FirstUC
中的按钮设置TextBox2
值。
private void button1_Click(object sender, EventArgs e)
{
Form1 form1 = (Form1)this.FindForm();
form1.UC2.TB2.Text = "test string";
}
Test result,测试结果,
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.