欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C#实现winform用子窗体刷新父窗体及子窗体改变父窗体控件值的方法

程序员文章站 2023-12-12 20:42:04
本文实例讲述了c#实现winform用子窗体刷新父窗体及子窗体改变父窗体控件值的方法。分享给大家供大家参考。具体如下: 第一种方法: 用委托,form2和form3是同...

本文实例讲述了c#实现winform用子窗体刷新父窗体及子窗体改变父窗体控件值的方法。分享给大家供大家参考。具体如下:

第一种方法:

用委托,form2和form3是同一组

form2

using system; 
using system.collections.generic; 
using system.componentmodel; 
using system.data; 
using system.drawing; 
using system.text; 
using system.windows.forms; 
namespace testmousemove 
{ 
  public delegate void setvisiablehandler(); 
  public partial class form2 : form 
  { 
    public form2() 
    { 
      initializecomponent(); 
    } 
    private void button1_click(object sender, eventargs e) 
    { 
      form3 frm = new form3(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; 
      } 
    } 
  } 
} 

form3

using system; 
using system.collections.generic; 
using system.componentmodel; 
using system.data; 
using system.drawing; 
using system.text; 
using system.windows.forms; 
namespace testmousemove 
{ 
  public partial class form3 : form 
  { 
    private setvisiablehandler m_setvisible; 
    public form3(setvisiablehandler setvisible) 
    { 
      initializecomponent(); 
      this.m_setvisible = setvisible; 
    } 
    private void btnvisible_click(object sender, eventargs e) 
    { 
      if (this.m_setvisible != null) 
      { 
        this.m_setvisible(); 
      } 
    } 
  } 
} 

第二种方法:

用变量,form4和form5是同一组

form4

using system; 
using system.collections.generic; 
using system.componentmodel; 
using system.data; 
using system.drawing; 
using system.text; 
using system.windows.forms; 
namespace testmousemove 
{ 
  public partial class form4 : form 
  { 
    public form4() 
    { 
      initializecomponent(); 
    } 
    #region 子窗口刷新父窗口的值 
    private string strlabel1 = ""; 
    public string strlabel1 
    { 
      get 
      { 
        return strlabel1; 
      } 
      set 
      { 
        strlabel1 = value; 
        this.label1.text = strlabel1; 
      } 
    } 
    #endregion 
    private void button1_click(object sender, eventargs e) 
    { 
      form5 form5 = new form5(this);//这里注意传个this 
      form5.show(); 
    } 
  } 
} 

form5

using system; 
using system.collections.generic; 
using system.componentmodel; 
using system.data; 
using system.drawing; 
using system.text; 
using system.windows.forms; 
namespace testmousemove 
{ 
  public partial class form5 : form 
  { 
    form4 form4 = new form4(); 
    public form5(form4 formfrm)//这个构造方法里有参数 
    { 
      form4 = formfrm; //这个必须要有 
      initializecomponent(); 
    } 
    private void button1_click(object sender, eventargs e) 
    { 
      form4.strlabel1 = this.textbox1.text; 
    } 
  } 
}

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

上一篇:

下一篇: