转自:http://heisetoufa.iteye.com/blog/382684

第一种方法:


用委托,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. }
  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. }
  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. }
  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. }
    23. }

[转]- Winform 用子窗体刷新父窗体,子窗体改变父窗体控件的值的更多相关文章

  1. c# winform 在一个窗体中使用另一个窗体中TextBox控件的值——解决办法

    [前提]一个winform应用程序项目中,窗体B,需要使用 窗体A 中一个TextBox控件的值,进行计算等操作. [解决方案] 1.在窗体A中定义:public static double a;// ...

  2. Winform开发框架之客户关系管理系统(CRM)的开发总结系列4-Tab控件页面的动态加载

    在前面介绍的几篇关于CRM系统的开发随笔中,里面都整合了多个页面的功能,包括多文档界面,以及客户相关信息的页面展示,这个模块就是利用DevExpress控件的XtraTabPage控件的动态加载实现的 ...

  3. Winform中实现更改DevExpress的RadioGroup的选项时更改其他控件(TextEdit、ColorPickEdit)的值

    场景 Winform中实现读取xml配置文件并动态配置ZedGraph的RadioGroup的选项: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article ...

  4. WinForm/Silverlight多线程编程中如何更新UI控件的值

    单线程的winfom程序中,设置一个控件的值是很easy的事情,直接 this.TextBox1.value = "Hello World!";就搞定了,但是如果在一个新线程中这么 ...

  5. (转载)c# winform 用子窗体刷新父窗体,子窗体改变父窗体控件的值

    第一种方法: 用委托,Form2和Form3是同一组 Form2 C#代码 using System; using System.Collections.Generic; using System.C ...

  6. C#多线程应用:子线程更新主窗体控件的值(二)

    在上篇文章中,我已经给大家列了一个在主线程中实现的方式,这篇文章来给大家说说使用Invoke的方式的例子: 对于不代理不太熟悉的朋友,建议先查查相关资料: 例子一: 在C#中,直接在子线程中对窗体上的 ...

  7. WinForm用户自定义控件,在主窗体加载时出现闪烁;调用用户控件出现闪烁,需要鼠标才能够显示

    转载自:http://www.dotblogs.com.tw/rainmaker/archive/2012/02/22/69811.aspx 解决方案: 在调用用户控件的窗体里面添加一下代码: pro ...

  8. datagridview随窗体的大小而变,表格填满控件

    在C#winform布局的时候,我们拖一个datagridview到窗体上面,将datagridview调整为适合窗体的大小,但是我们运行之后,点击最大化按钮的时候,却发现datagridview的大 ...

  9. 如何实现能像windows 窗体一样改变大小的控件 Silverlight

    众所周知,我们可以将鼠标放在windows窗体的边框上,按住鼠标左键改变窗体大小.那么,在silverlight上如何实现呢? 1. 需要将改控件放置在canvas上. 2. 判断鼠标位置,然后将Ar ...

随机推荐

  1. dfa最小化,修正了上个版本的一些错误。

    上个版本测试的时候,只用了两个非常简单的测试用例,所以好多情况有问题却没有测试出来 bug1:在生成diff_matrix的时候,循环变量少循环了一次,导致最后一个节点在如果无法与其他点合并的情况下, ...

  2. 剑指Offer18 顺时针打印矩阵

    /************************************************************************* > File Name: 18_PrintM ...

  3. Pascal 语言中约瑟夫问题:幸运观众

    [题目]节目主持人准备从n名学生中挑选一名幸运观众,因为大家都想争当幸运观众,老师只好采取这样的办法:全体同学站成一列,由前面往后面依顺序报数.1,2,1,2……报单数的同学退出队伍,剩下的同学向前靠 ...

  4. Angularjs 使用filter格式化输出href

    工作中,由于是多级菜单,如果上级菜单为空,就会访问Angularjs 默认的state,然后再展开菜单,我找资料之后,才知道是通过filter来格式化输出数据的,格式是{{ expression | ...

  5. 学习Slim Framework for PHP v3 (四)--get()是怎么加进去的?

    看看官网加粗的一句话: At its core, Slim is a dispatcher that receives an HTTP request, invokes an appropriate ...

  6. ViewPager 可左右滑动和缩放的图片浏览

    最近因为要做一个项目,需要使用到图片的浏览.我就自己在网上找了些资料,然后加以修改整理后出来一个demo,希望可以帮助到需要的人.同时这也是我第一个技术博客. 在做之前首先需要了解一下什么是ViewP ...

  7. asp连接SQL数据库的代码

    connstr="driver={SQL Server};server=(local);uid=sa;pwd=sa;database=Your database" 语法介绍: 1. ...

  8. netstat命令[转]

    原文地址:http://www.cnblogs.com/peida/archive/2013/03/08/2949194.html netstat命令用于显示与IP.TCP.UDP和ICMP协议相关的 ...

  9. Angular2中的metadata(元数据)

    @Attrubute() 从host element 中获得普通(不是@Input)属性对应的值 适用于组件嵌套或指令, 从父组件向子组件传递数据 app.component.ts import {C ...

  10. java中的异常处理机制_函数覆盖时的异常特点

    /*注意:异常声明在函数上 异常在子父类覆盖时的体现1.子类在覆盖父类时,如果父类的方法抛出异常,那么子类的覆盖方法,只能抛出父类的异常或者异常的子类2.如果父类方法抛出多个异常,那么子类在覆盖该方法 ...