你建立一个winform程序,出来一个默认窗体Form1,再添加一个UserControl,默认名字为UserControl1;
在Form1的窗口里写如下的代码:
public partial class Form1 : Form
{
//寂义一个UserControl1对象
UserControl1 obj = new UserControl1();
public Form1()
{
InitializeComponent();
//把obj的父窗体指定为Form1
obj.Parent = this;
}
//在UserControl1要调用的函数
public void ShowMess()
{
MessageBox.Show("sssss");
}
}
在UserControl1 里面添加一个Button1,在Button1的Click事件里写上
private void button1_Click(object sender, EventArgs e)
{
//调用父窗口里的函数ShowMess()
((Form1)this.ParentForm).ShowMess();
}'
摘自http://bbs.csdn.net/topics/320181298,存记。