C# 将方法传递给新表单';s控制
所以我有两个表格:C# 将方法传递给新表单';s控制,c#,winforms,methods,C#,Winforms,Methods,所以我有两个表格:Form1和Form2 在表格1中我有以下代码: pubilc class Form1 : Form { public Form1() { InitializeComponent(); } public void Button1_Click(object sender, EventArgs e) { Form2 newForm = new Form2(); //Here i want to pass My
Form1
和Form2
在表格1中
我有以下代码:
pubilc class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public void Button1_Click(object sender, EventArgs e)
{
Form2 newForm = new Form2(); //Here i want to pass MyFunction
}
public void MyFunction()
{
MessageBox.Show("This is passed function from " + this.Text);
}
}
在表格2中
我有:
public class Form2 : Form
{
public Form2() //Here i want to receive Form1.MyFunction
{
InitializeComponent();
this.Button1.Click += new System.EventHandler(passedFunction);
}
}
在上面的代码中,你可以理解我的观点。在创建窗体时,我将使用传递的函数将EventHandler分配给带有控件的窗体
我没有尝试过任何东西,因为我不知道如何将方法传递到表单。尝试以下方法:
public class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public void Button1_Click(object sender, EventArgs e)
{
Form2 newForm = new Form2(MyFunction); //Here i want to pass MyFunction
}
public void MyFunction(object sender, EventArgs e)
{
MessageBox.Show("This is passed function from " + this.Text);
}
}
public class Form2 : Form
{
public Form2(EventHandler passedFunction) //Here i want to receive Form1.MyFunction
{
InitializeComponent();
this.Button1.Click += passedFunction;
}
}
试试这个:
public class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public void Button1_Click(object sender, EventArgs e)
{
Form2 newForm = new Form2(MyFunction); //Here i want to pass MyFunction
}
public void MyFunction(object sender, EventArgs e)
{
MessageBox.Show("This is passed function from " + this.Text);
}
}
public class Form2 : Form
{
public Form2(EventHandler passedFunction) //Here i want to receive Form1.MyFunction
{
InitializeComponent();
this.Button1.Click += passedFunction;
}
}
可能的解决办法:可能的解决办法:真不敢相信这么容易。非常感谢。真不敢相信这么容易。非常感谢你。