C#实现子窗体与父窗体通信方法实例总结

2019-12-26 13:19:52于丽
  • {MessageBox.Show( "主窗体已收到消息: " + Message);}  ?

    子窗体测试: 

    复制代码 if (this.SendTo != null) this.SendTo( "主窗体收到了吗? ");

     

    【第四种方法:】

    通过引用:

    下例演示怎样通过引用类型实现你的功能: 
    子窗体中定义: 

    复制代码 protected MainForm ParentFrom = null;//主窗体

     

    新构造函数:
     

    1. public ChildForm(MainForm parent)   {  
    2. InitializeComponent();  this.ParentFrom = parent;//引用  
    ?

    主窗体中某Click:
     

    1. ChildForm frm = new ChildForm(this);   frm.ShowDialog(this); 
    ?

    子窗体测试:
     

    1. void ...Click(....)   {  
    2. this.Text = "测试引用 ";   if (this.ParentFrom != null) this.ParentFrom.Text += "- " + this.Text;//.......  
    ?

    希望本文所述对大家的C#程序设计有所帮助。