假设有三个窗体,
Form1-主窗体
Form2-导航窗体 //作为桥梁,接收子窗体事件,并在它的父窗体显示出来
Form3-子窗体
代码如下:
Form1-主窗体,有一个button和一个lable,并且lable的Modifiers属性设置为public
程序代码:
using System;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 form2 = new Form2(this);
form2.Show();
}
}
}
Form2-导航窗体 至少有一个按钮
程序代码:
using System;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form2 : Form
{
private Form1 form1;
public Form2(Form1 form1)
{
InitializeComponent();
this.form1 = form1;
}
private void button1_Click(object sender, EventArgs e)
{
Form3 form3 = new Form3();
form3.showNameEvent += new EventHandler(form3_showNameEvent);
form3.Show();
}
void form3_showNameEvent(object sender, EventArgs e)
{
form1.label1.Text = sender.ToString();
}
}
}
Form3-子窗体 可以什么都没有,随便设置一下Text属性用来显示
程序代码:
using System;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form3 : Form
{
public Form3()
{
InitializeComponent();
}
public event EventHandler showNameEvent;
private void Form3_Load(object sender, EventArgs e)
{
showNameEvent.Invoke(this.Text,null);
}
}
}