c# winform 用子窗体刷新父窗体,子窗体改变父窗体控件的值

第一种方法:

用委托,Form2和Form3是同一组

Form2

C#代码 复制代码 收藏代码
  1. using System; 
  2. using System.Collections.Generic; 
  3. using System.ComponentModel; 
  4. using System.Data; 
  5. using System.Drawing; 
  6. using System.Text; 
  7. using System.Windows.Forms; 
  8.  
  9. namespace TestMouseMove 
  10.     public delegate void SetVisiableHandler(); 
  11.  
  12.     public partial class Form2 : Form 
  13.     { 
  14.         public Form2() 
  15.         { 
  16.             InitializeComponent(); 
  17.         } 
  18.         private void button1_Click(object sender, EventArgs e) 
  19.         { 
  20.             Form3 frm = new Form3(new SetVisiableHandler(SetVisiable)); 
  21.             frm.Show(); 
  22.         } 
  23.  
  24.         private void SetVisiable() 
  25.         { 
  26.             SetVisiable(this.label1, !this.label1.Visible); 
  27.         } 
  28.  
  29.         private void SetVisiable(Control control, bool visiable) 
  30.         { 
  31.             if (this.Controls.Contains(control)) 
  32.             { 
  33.                 control.Visible = visiable; 
  34.             } 
  35.         } 
  36.  
  37.     } 


Form3

C#代码 复制代码 收藏代码
  1. using System; 
  2. using System.Collections.Generic; 
  3. using System.ComponentModel; 
  4. using System.Data; 
  5. using System.Drawing; 
  6. using System.Text; 
  7. using System.Windows.Forms; 
  8.  
  9. namespace TestMouseMove 
  10.     public partial class Form3 : Form 
  11.     { 
  12.         private SetVisiableHandler m_setVisible; 
  13.  
  14.         public Form3(SetVisiableHandler setvisible) 
  15.         { 
  16.             InitializeComponent(); 
  17.             this.m_setVisible = setvisible; 
  18.         } 
  19.         private void btnVisible_Click(object sender, EventArgs e) 
  20.         { 
  21.             if (this.m_setVisible != null
  22.             { 
  23.                 this.m_setVisible(); 
  24.             } 
  25.         } 
  26.  
  27.     } 


第二种方法:

用变量,Form4和Form5是同一组

Form4

C#代码 复制代码 收藏代码
  1. using System; 
  2. using System.Collections.Generic; 
  3. using System.ComponentModel; 
  4. using System.Data; 
  5. using System.Drawing; 
  6. using System.Text; 
  7. using System.Windows.Forms; 
  8.  
  9. namespace TestMouseMove 
  10.     public partial class Form4 : Form 
  11.     { 
  12.         public Form4() 
  13.         { 
  14.             InitializeComponent(); 
  15.         } 
  16.         #region 子窗口刷新父窗口的值 
  17.  
  18.         private string strLabel1 = ""
  19.  
  20.         public string StrLabel1 
  21.         { 
  22.             get 
  23.             { 
  24.                 return strLabel1; 
  25.             } 
  26.             set 
  27.             { 
  28.                 strLabel1 = value; 
  29.                 this.label1.Text = strLabel1; 
  30.             } 
  31.         } 
  32.         #endregion 
  33.  
  34.         private void button1_Click(object sender, EventArgs e) 
  35.         { 
  36.             Form5 form5 = new Form5(this);//这里注意传个this 
  37.             form5.Show(); 
  38.         } 
  39.     } 


Form5

C#代码 复制代码 收藏代码
  1. using System; 
  2. using System.Collections.Generic; 
  3. using System.ComponentModel; 
  4. using System.Data; 
  5. using System.Drawing; 
  6. using System.Text; 
  7. using System.Windows.Forms; 
  8.  
  9. namespace TestMouseMove 
  10.     public partial class Form5 : Form 
  11.     { 
  12.         Form4 form4 = new Form4(); 
  13.  
  14.         public Form5(Form4 formFrm)//这个构造方法里有参数 
  15.         { 
  16.             form4 = formFrm; //这个必须要有 
  17.             InitializeComponent(); 
  18.         } 
  19.  
  20.         
  21.         private void button1_Click(object sender, EventArgs e) 
  22.         { 
  23.             form4.StrLabel1 = this.textBox1.Text; 
  24.         } 
  25.     } 


黑色头发:http://heisetoufa.iteye.com/ 
posted @ 2011-08-28 14:39  冰封的心  阅读(309)  评论(0)    收藏  举报