form2中定义事件
public event EventHandler button1Event在button1的Click事件中:
private void button1_Click(object sender, EventArgs e)
{
if(null != button1Event)
button1Event(this, EventArgs.empty);//这里的参数看你自己的需要
}form1中
form2.button1Event += new EventHandler(yourHandle)private void yourHandle(object sender, EventArgs e)
{
//执行改变颜色的代码
}
public event EventHandler button1Event在button1的Click事件中:
private void button1_Click(object sender, EventArgs e)
{
if(null != button1Event)
button1Event(this, EventArgs.empty);//这里的参数看你自己的需要
}form1中
form2.button1Event += new EventHandler(yourHandle)private void yourHandle(object sender, EventArgs e)
{
//执行改变颜色的代码
}
比如说,form1里有button1和contextMenuStrip1,当我点击右击快捷菜单中的1时会弹出form2,然后我点击form2里的button1时会让form1里的button1变为红色,或者我点击form2里的button2时会让form1里的button1变为原色(灰色)的话我要怎么写?麻烦你了,告诉我下,如果有高手会也麻烦告诉我下.我先谢谢各位了!
{
private delegate void delmsg(string str);
private static event delmsg evemsg;
public static void domsg(string str)
{
evemsg(str);
}
public Form1()
{
InitializeComponent();
evemsg+=new delmsg(this.btnchangecolor);
}
private void btnchangecolor(string str)
{
if (str == "1")
{
this.button1.BackColor = Color.Red;
}
}
private void showfrm2ToolStripMenuItem_Click(object sender, EventArgs e)
{
Form2 f = new Form2();
f.Show();
}
}
public partial class Form2 : Form
{
private void button1_Click(object sender, EventArgs e)
{
Form1.domsg("1");
}
}