110,534
社区成员
发帖
与我相关
我的任务
分享
public partial class CarManeger : Form
{
public delegate void MyDelegate(string text);
public event EventHandler MyEvent;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
public void ShowCarNume(string txt)
{
MessageBox.Show(txt);
}
private void button2_Click(object sender, EventArgs e)
{
string str="测试数据";
ShowCarNume(str);
}
}
public partial class Add : Form
{
public Add()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
//怎么在这里用委托调用form1里面的ShowCarNume方法 要用委托
}
}
namespace WindowsApplication2
{
public delegate void SetVisiableHandler();
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 frm = new Form2(new SetVisiableHandler(SetVisiable));
frm.Show();
}
private void SetVisiable()
{
SetVisiable(this.label1, !this.label1.Visible);
}
private void SetVisiable(Control control, bool visiable)
{
if (this.Controls.Contains(control))
{
control.Visible = visiable;
}
}
}
}
namespace WindowsApplication2
{
public partial class Form2 : Form
{
private SetVisiableHandler m_setVisible;
public Form2(SetVisiableHandler setvisible)
{
InitializeComponent();
this.m_setVisible = setvisible;
}
private void btnVisible_Click(object sender, EventArgs e)
{
if (this.m_setVisible != null)
{
this.m_setVisible();
}
}
}
}
// form1
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 frm = new Form2();
frm.Handle += new DelegateHandle(frm_Handle);
frm.Show();
}
void frm_Handle(string st)
{
MessageBox.Show(st);
}
}
// form2
public delegate void DelegateHandle(string st );
public partial class Form2 : Form
{
public event DelegateHandle Handle;
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (Handle != null)
Handle(textBox1.Text);
}
}
private void button1_Click(object sender, EventArgs e)//Form2中的Button1_Click
{
Form1 fm = new Form1();
Button btn = fm.Controls["button1"] as Button;
if (btn != null)
{
PropertyInfo pi = btn.GetType().GetProperty("Events", BindingFlags.Instance | BindingFlags.NonPublic);
if (pi != null)
{
EventHandlerList ehl = (EventHandlerList)pi.GetValue(btn, null);
if (ehl != null)
{
FieldInfo fi = (typeof(Control)).GetField("EventClick", BindingFlags.Static | BindingFlags.NonPublic);
if (fi != null)
{
Delegate d = ehl[fi.GetValue(null)];
if (d != null)
{
d.DynamicInvoke(btn, e);
}
}
}
}
}
}
private void button1_Click(object sender, EventArgs e)
{
CarManeger form1 = new CarManeger();
string str="测试数据";
form1.ShowCarNume(str);
}