惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

WordPress大学
WordPress大学
B
Blog RSS Feed
The GitHub Blog
The GitHub Blog
爱范儿
爱范儿
博客园 - 司徒正美
J
Java Code Geeks
酷 壳 – CoolShell
酷 壳 – CoolShell
Engineering at Meta
Engineering at Meta
大猫的无限游戏
大猫的无限游戏
D
Docker
Blog — PlanetScale
Blog — PlanetScale
Recent Announcements
Recent Announcements
罗磊的独立博客
Microsoft Azure Blog
Microsoft Azure Blog
博客园 - 聂微东
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
人人都是产品经理
人人都是产品经理
Stack Overflow Blog
Stack Overflow Blog
M
MIT News - Artificial intelligence
腾讯CDC
T
The Blog of Author Tim Ferriss
小众软件
小众软件
U
Unit 42
T
Tailwind CSS Blog

博客园 - love/coder

Android studio环境配置 phpstorm/clion设置文件头部版权信息 php7.4需要升级vc++版本问题 SqlServer查询所有表名及其数据行数 图解 CSS 选择器 Winform控件命名规则 JQuery UI实现浮动客服并拨号 C#中的Timer定时器 winform中的多线程与异步加载 base64的使用 编程命名规范(网文) 常浏览编程技术资源 接口控制器层(Controller层)设计(网文) SpringBoot编写WebApi~(1)idea创建项目并打包 使用FileBrowser搭建文件服务 VS新建文件时在头部附加版权信息 记录一次Apache2.4启动PHP的Curl扩展不成功的问题 FileZilla Server搭建ftp服务 VS安装插件,按CTRL+鼠标左键进入函数 .NET8的AOT技术
C# 子窗体中调用父窗体中的方法(或多窗体之间方法调用)
love/coder · 2025-03-13 · via 博客园 - love/coder

看似一个简单的功能需求,其实很多初学者处理不好的,很多朋友会这么写:

C# Code:

//父窗体是是frmParent,子窗体是frmChildA
//在父窗体中打开子窗体
frmChildA child = new frmChildA();
child.MdiParent = this;
child.Show();


//子窗体调父窗体方法:
//错误的调用!!!!!!!!
(this.MdiParent as frmParent).ParentFoo();

知道错在那里吗?错在强依赖!如果父窗体与子窗体在同一个模块内看似没有错,因为这种反向引用在同一个模块内是可行的,但程序不能这么写,你把它写死了!固化了!假设我们的项目不断在扩展,需要将父窗体与子窗体分开在不同的模块,这段代码就完了!因为父窗体模块必须引用子窗体模块,而子窗体需要用到frmParent的类,又要引用父窗体的模块!这时构成了双向引用,编译不能通过,所以讲程序写死了!

有什么办法解除这种依赖关系呢?办法是有的,就是使用接口解除依赖关系!

我们把程序改下:

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# 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();
   }

实现思路:

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

只需要依赖Common模块。

https://www.csframework.com/archive/2/arc-2-20110805-1771.htm

注:本文很好的利用了面向对象(封装,继承,多态)的特性。在看到本文之前,我是使用父类抽象解决的,即多个窗体实现同一个顶级父类,在父类定义虚拟方法,在子类具体实现该方法。父类指针指向实际子类对象。将用到的方法公共抽象到父类虚方法。