首页 > 编程 > C# > 正文

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

2019-10-29 21:40:07
字体:
来源:转载
供稿:网友

这篇文章主要介绍了C#实现winform用子窗体刷新父窗体及子窗体改变父窗体控件值的方法,涉及C#窗体交互的相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下

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

第一种方法:

用委托,Form2和Form3是同一组

Form2

 

 
  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. namespace TestMouseMove  
  9. {  
  10. public delegate void SetVisiableHandler();  
  11. public partial class Form2 : Form  
  12. {  
  13. public Form2()  
  14. {  
  15. InitializeComponent();  
  16. }  
  17. private void button1_Click(object sender, EventArgs e)  
  18. {  
  19. Form3 frm = new Form3(new SetVisiableHandler(SetVisiable));  
  20. frm.Show();  
  21. }  
  22. private void SetVisiable()  
  23. {  
  24. SetVisiable(this.label1, !this.label1.Visible);  
  25. }  
  26. private void SetVisiable(Control control, bool visiable)  
  27. {  
  28. if (this.Controls.Contains(control))  
  29. {  
  30. control.Visible = visiable;  
  31. }  
  32. }  
  33. }  
  34. }  

Form3

 

 
  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. namespace TestMouseMove  
  9. {  
  10. public partial class Form3 : Form  
  11. {  
  12. private SetVisiableHandler m_setVisible;  
  13. public Form3(SetVisiableHandler setvisible)  
  14. {  
  15. InitializeComponent();  
  16. this.m_setVisible = setvisible;  
  17. }  
  18. private void btnVisible_Click(object sender, EventArgs e)  
  19. {  
  20. if (this.m_setVisible != null)  
  21. {  
  22. this.m_setVisible();  
  23. }  
  24. }  
  25. }  
  26. }  

第二种方法:

用变量,Form4和Form5是同一组

Form4

 

 
  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. namespace TestMouseMove  
  9. {  
  10. public partial class Form4 : Form  
  11. {  
  12. public Form4()  
  13. {  
  14. InitializeComponent();  
  15. }  
  16. #region 子窗口刷新父窗口的值  
  17. private string strLabel1 = "";  
  18. public string StrLabel1  
  19. {  
  20. get 
  21. {  
  22. return strLabel1;  
  23. }  
  24. set 
  25. {  
  26. strLabel1 = value;  
  27. this.label1.Text = strLabel1;  
  28. }  
  29. }  
  30. #endregion  
  31. private void button1_Click(object sender, EventArgs e)  
  32. {  
  33. Form5 form5 = new Form5(this);//这里注意传个this  
  34. form5.Show();  
  35. }  
  36. }  
  37. }  

Form5

 

 
  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. namespace TestMouseMove  
  9. {  
  10. public partial class Form5 : Form  
  11. {  
  12. Form4 form4 = new Form4();  
  13. public Form5(Form4 formFrm)//这个构造方法里有参数  
  14. {  
  15. form4 = formFrm; //这个必须要有  
  16. InitializeComponent();  
  17. }  
  18. private void button1_Click(object sender, EventArgs e)  
  19. {  
  20. form4.StrLabel1 = this.textBox1.Text;  
  21. }  
  22. }  

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

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表