zoukankan      html  css  js  c++  java
  • 多窗体之间方法调用 z

    C# Code:

    /// <summary>
    /// 主窗体接口
    /// </summary>
    public interface IMdiParent
    {
       void ParentFoo();
    }


    /// <summary>
    /// 子窗体接口
    /// </summary>
    public interface IMyChildForm
    {
       void Foo();
    }



    主窗体的代码:


    C# Code:

    /// <summary>
    /// 主窗体,实现IMdiParent接口
    /// </summary>
    public partial class frmParent : Form, IMdiParent
    {
       public frmParent()
       {
          InitializeComponent();
       }
       
       private void form1ToolStripMenuItem_Click(object sender, EventArgs e)
       {
          //打开子窗体
          frmChildA child = new frmChildA();
          child.MdiParent = this;
          child.Show();
       }
       
       private void menuCallFoo_Click(object sender, EventArgs e)
       {
          //调用子窗体的Foo()方法
          Form activedChild = this.ActiveMdiChild;
          if ((activedChild != null) && (activedChild is IMyChildForm))
             (activedChild as IMyChildForm).Foo();
       }
       
       #region IMdiParent 成员
       
       public void ParentFoo()
       {
          MessageBox.Show("调用" this.GetType().FullName ".ParentFoo()方法!");
       }
       
       #endregion
    }

    //来源:C/S框架网(www.csframework.com) QQ:1980854898



    子窗体的代码:


    C# Code:

    /// <summary>
    /// 子窗体,实现IMyChildForm接口
    /// </summary>
    public partial class frmChildA : Form, IMyChildForm
    {
       public frmChildA()
       {
          InitializeComponent();
       }
       
       #region IMyChildForm 成员
       
       public void Foo()
       {
          MessageBox.Show("调用" this.GetType().FullName ".Foo()方法!");
       }
       
       #endregion
       
       private void btnParentFoo_Click(object sender, EventArgs e)
       {
          //调用父窗体的ParentFoo()方法
          if ((this.MdiParent != null) && (this.MdiParent is IMdiParent))
          (this.MdiParent as IMdiParent).ParentFoo();
       }
       
       private void btnErrCall_Click(object sender, EventArgs e)
       {
          //错误的调用
          (this.MdiParent as frmParent).ParentFoo();
       }
       
       //来源:C/S框架网(www.csframework.com) QQ:1980854898
       




    贴图图片


    实现思路:

    frmParent 窗体所在的模块依赖frmChildA所在模块,而frmChildA只依赖IMdiParent接口,这正是《敏捷软件开发》中所讲的依赖倒置原则。最 后,我们把IMdiParent接口部署在一个Common模块内,实际上frmParent与frmChildA只需要依赖Common模块。

  • 相关阅读:
    hdu 2151 Worm (DP)
    .NET中使用switch和java不一样的地方。
    逻辑运算符||和| 、&&和&的区别
    赋值表达式也有值
    交换两个变量的值(面试题)
    .NET中的 枚举
    .NET FrameWork 中的 CTS
    .NET中变量的类型问题
    .NET中的标识符、关键字 以及 .NET中的命名规范
    .NET中的注释种类,单行注释、多行注释、文档注释。。。
  • 原文地址:https://www.cnblogs.com/zeroone/p/4890212.html
Copyright © 2011-2022 走看看