灵活的办法,定义一个公共类,将其中的成员变量设置为static作为传值缓存:public class Common{ public static Type val; }然后两个窗口类都 using 公共类所在命名空间即可。
这个很简单的!下面这个Form2就可以看做一个自定义的类,其他的一样。 Form2: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms;namespace DelegateDemo { public partial class Form2 : Form { public delegate void dlgOnFormLoad(Form2 loadedForm); public event dlgOnFormLoad OnForm2Loaded; public Form2() { InitializeComponent(); } private void Form2_Load(object sender, EventArgs e) { if (OnForm2Loaded != null) { OnForm2Loaded(this); } } } }Form1: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms;namespace DelegateDemo { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { Form2 frm2 = new Form2(); frm2.OnForm2Loaded += new Form2.dlgOnFormLoad(frm2_OnForm2Loaded); frm2.Show(); } void frm2_OnForm2Loaded(Form2 loadedForm) { MessageBox.Show(loadedForm.Text + "已经加载完毕"); } } }
public static Type val;
}然后两个窗口类都 using 公共类所在命名空间即可。
Form2:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;namespace DelegateDemo
{
public partial class Form2 : Form
{
public delegate void dlgOnFormLoad(Form2 loadedForm);
public event dlgOnFormLoad OnForm2Loaded; public Form2()
{
InitializeComponent();
} private void Form2_Load(object sender, EventArgs e)
{
if (OnForm2Loaded != null)
{
OnForm2Loaded(this);
}
}
}
}Form1:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;namespace DelegateDemo
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void button1_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2();
frm2.OnForm2Loaded += new Form2.dlgOnFormLoad(frm2_OnForm2Loaded);
frm2.Show();
} void frm2_OnForm2Loaded(Form2 loadedForm)
{
MessageBox.Show(loadedForm.Text + "已经加载完毕");
}
}
}