先有两个类于手参数传递
窗体1
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
public void ReceiveParameterFromAnotherForm(me parameter)
{
MessageBox.Show(parameter.bb);
}
Dbc dbc= new Dbc();
private void button1_Click(object sender, EventArgs e)
{
dbc.aa = "来自窗体1";
Form2 fr= new Form2(dbc);
fr.OnFormClosedWithAnotherClass += Fr_OnFormClosedWithAnotherClass;
fr.ShowDialog();
}
private void Fr_OnFormClosedWithAnotherClass(object sender, EventArgs e)
{
throw new NotImplementedException();
}
}
//用于传递的参数
public class Dbc
{
public string aa;
}
public class me
{
public string bb;
}
窗体2
public partial class Form2 : Form
{
public event EventHandler OnFormClosedWithAnotherClass;
public Form2()
{
InitializeComponent();
}
me messsage = new me();
public Form2(Dbc dbc)
{
InitializeComponent();
messsage.bb = dbc.aa+ "+进入窗体2";
}
private void Form2_Load(object sender, EventArgs e)
{
}
private void Form2_FormClosed(object sender, FormClosedEventArgs e)
{
Form1 mainWindow = (Form1)Application.OpenForms["Form1"];
if (mainWindow != null)
{
mainWindow.ReceiveParameterFromAnotherForm(messsage);
}
}
}
窗体1将dbc参数传递给窗体2,窗体2执行完成,关闭后在窗体1上会调用窗体2关闭的事件