zoukankan      html  css  js  c++  java
  • Winform(C#)——子窗口调用父窗口

    引用自博客:http://blog.csdn.net/lz00728/article/details/7545809 

    方法1: 所有权法(已经验证可用)

           //FormFather(父窗口)
           //需要有一个公共的刷新方法
           public void Refresh_Method()
           {
                   //...
           }
           //在调用FormSub时,要把FormSub的所有者设为FormFather(父窗口)
           FormSub f2 = new FormSub() ;
           f2.Owner = this;
           f2.ShowDialog() ;


           //FormSub(子窗口)
           //在需要对其调用者(父)刷新时
           FormFather f1 ;
           f1 = (FormFather)this.Owner;
           f1.Refresh_Method() ;

     

    方法2:自身传递法
           //FormFather:
           //需要有一个公共的刷新方法
           public void Refresh_Method()
           {
                   //...
           }
           FormSub f2 = new FormSub() ;
           f2.ShowDialog(this) ;


           //FormSub:
           private FormFather p_f1;
           public FormSub(FormFather f1)
           {
                   InitializeComponent();
                   p_f1 = f1;
           }
           //刷新时
           p_f1.Refresh_Method() ;

     

    方法3:属性法
           //FormFather:
           //需要有一个公共的刷新方法
           public void Refresh_Method()
           {
                   //...
           }
           //调用时
           FormSub f2 = new FormSub() ;
           f2.P_F1 = this; //重点,赋值到子窗体对应属性
           f2.Show() ;

           //FormSub:
           private FormFather p_f1;
           public FormFather P_F1
           {
                   get{return p_f1;}
                   set{p_f1 = value;}
           }
           //刷新时
           p_f1.Refresh_Method() ;


    方法4:委托法
           //FormFather:
           //需要有一个公共的刷新方法
           public void Refresh_Method()
           {
                   //...
           }
           //调用时
           FormSub f2 = new FormSub() ;
           f2.ShowUpdate += new DisplayUpdate(Refresh_Method) ;
           f2.Show() ;


           //FormSub:
           //声明一个委托
           public delegate void DisplayUpdate();
           //声明事件
           public event DisplayUpdate ShowUpdate;
           //刷新时,放在需要执行刷新的事件里

           if(ShowUpdate!=null)   ShowUpdate();

     

           //子窗体提交后
           private void btnOK_Click(object sender, EventArgs e)
           {
                   this.DialogResult = DialogResult.OK;
                   this.Close();
           }


     

          //判断子窗体
           if(form.ShowDialog() == DialogResult.OK)
           {
                    刷新父窗体中的DataGRIDVIEW数据
           }

  • 相关阅读:
    堆重启_uaf_hacknote
    记一次Spring表达式注入
    绕过waf的另类木马文件攻击方法
    西湖论剑 Flagshop 分析复现
    【测开基础之计算机网络】二: 物理层_网络_TesterAllen的博客CSDN博客
    测试面试 | 某 BAT 大厂测试开发面试真题与重点解析
    谁懂这篇文,玩游戏还会卡顿?
    移动自动化测试入门,你必须了解的背景知识和工具特性
    Python 自动化测试(三): pytest 参数化测试用例构建
    接口自动化测试分层设计与实践总结
  • 原文地址:https://www.cnblogs.com/bluewhy/p/5276323.html
Copyright © 2011-2022 走看看