国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

C#實現winform用子窗體刷新父窗體及子窗體改變父窗體控件值的方法

2020-01-24 01:35:39
字體:
來源:轉載
供稿:網友

本文實例講述了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#程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 泰宁县| 瑞金市| 淮安市| 晋城| 铜川市| 吉安县| 东乡族自治县| 巴南区| 调兵山市| 师宗县| 江孜县| 万全县| 揭西县| 赤水市| 永定县| 五华县| 延寿县| 资源县| 临猗县| 临猗县| 顺平县| 洪江市| 衢州市| 屯门区| 永州市| 湖口县| 建始县| 紫阳县| 民权县| 都兰县| 神农架林区| 郓城县| 台中市| 新乡市| 大埔区| 大冶市| 沂水县| 灵寿县| 宝鸡市| 通化市| 周宁县|