简体   繁体   English

我如何将值从用户控件传递到另一个用户控件

[英]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.

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