zoukankan      html  css  js  c++  java
  • C# 子窗体调用父窗体的方法

    网络上有几种方法,先总结如下:
    调用窗体(父):FormFather,被调用窗体(子):FormSub
    
    
    方法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数据
           }
  • 相关阅读:
    mysql 查询结果中增加序号
    mycat配置文件备份
    解决Python安装模块出错 ImportError: No module named setuptools
    sed 详解【转】
    CentOS下配置SFTP操作日志
    解决redis aof文件过大的问题
    mysql主从复制搭建中几种log和pos详解
    Linux下使用命令行配置IPMI
    Zabbix笔记
    zabbix_agentd.conf配置文件详解
  • 原文地址:https://www.cnblogs.com/niyl/p/15720942.html
Copyright © 2011-2022 走看看