简体   繁体   中英

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. How do i do?

You can define custom property to achieve it.

First, define property UC2 in Form1.cs to access the 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 .

public TextBox TB2
{
    get { return textBox2; }
    set { textBox2 = value; }
}

Last, set the TextBox2 vaule by clicking button in FirstUC .

private void button1_Click(object sender, EventArgs e)
{
    Form1 form1 = (Form1)this.FindForm();
    form1.UC2.TB2.Text = "test string";
}

Test result,

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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