点击UserControl中的按钮将值传给winform页面
![](http://images0.cnblogs.com/i/638664/201406/231714025493189.jpg)
2.点击确定按钮,将值传给winform页面中的textbox
3.UserControl1的代码:
public partial class UserControl1 : UserControl
{
public UserControl1()
{
InitializeComponent();
}
public delegate void deleset(string n);
public event deleset delevent;
private void button1_Click(object sender, EventArgs e)
{
delevent(textBox1.Text.ToString());
textBox1.Text = "";
}
}
4.Form12中的代码:
private void button1_Click(object sender, EventArgs e)
{
this.panel1.Visible = true;
UserControl1 us = new UserControl1();
us.delevent += new UserControl1.deleset(set);
this.panel1.Controls.Add(us);
}
private void Form12_Load(object sender, EventArgs e)
{
this.panel1.Visible = false;
}
public void set(string m)
{
txt_1.Text = m;
this.panel1.Visible = false;
}
5.代码分析:
首先在UserControl中定义了委托和事件,然后在winform中将赋值方法绑定到事件中( us.delevent += new UserControl1.deleset(set);)。最后在UserControl中的按钮点击事件中触发事件(delevent(textBox1.Text.ToString());)。
通过以上三个步骤将用户控件中的值,传给winform页面上的控件。