且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

将值从用户控件传递到表单

更新时间:2023-12-06 08:12:16

我在一个新项目中创建了 UserControl1 并将其引用到包含表单的项目中,而不是直接在表单的项目中添加 UserControl,这就是为什么变得复杂.

I create the UserControl1 in a new project and reference it to my project that contains the form instead of directly adding UserControl in the form´s project, that's why things get complicated.

现在在这里将值从 UserControl 传递给 Form

Here it now to pass value from UserControl to Form

用户控制

public string ID2
{
    get { return textBox1.Text; }
}

private void textBox1_TextChanged(object sender, EventArgs e)
{
    var textBoxContent = this.textBox1.Text;
    var parent = this.Parent as Form1;
    parent.ID2 = ID2;  
}

Form1

public string ID2
{
    set { textBox1.Text = value; }
}