C# Winform 跨多级窗体/控件传值

我们知道 C# winform 跨窗体传值,子父窗体交互一般用委托来实现。之前都是子窗体父窗体两级交互,如果子窗体再生一个子子窗体,然后子子窗体调用父窗体函数,这样该如何操作?我想到的实现方式还是用委托变量一级一级的往下传。下面是实现的效果:

▲ Form1 加载 Uc1,在 Uc1 下加载 Uc2,Uc2 下返回 Uc1

Form1.cs

using System;  
using System.Collections.Generic;  
using System.ComponentModel;  
using System.Data;  
using System.Drawing;  
using System.Linq;  
using System.Text;  
using System.Threading.Tasks;  
using System.Windows.Forms;  
  
namespace WindowsFormsApp1  
{  
    public partial class Form1 : Form  
    {  
        public Form1()  
        {  
            InitializeComponent();  
            this.userControl1.LoadUserF2 = this.LoadFrm;  
            this.userControl1.action = () => this.button1_Click(nullnull);  
        }  
  
        private UserControl1 userControl1 = new UserControl1() { Dock = DockStyle.Fill};  
  
        private void button1_Click(object sender, EventArgs e)  
        {  
            this.LoadFrm(this.userControl1);  
        }  
  
        private void LoadFrm(Control control)  
        {  
            this.panel1.Controls.Clear();  
            this.panel1.Controls.Add(control);  
        }  
    }  
}  

UserControl1.cs

using System;  
using System.Collections.Generic;  
using System.ComponentModel;  
using System.Drawing;  
using System.Data;  
using System.Linq;  
using System.Text;  
using System.Threading.Tasks;  
using System.Windows.Forms;  
  
namespace WindowsFormsApp1  
{  
    public partial class UserControl1 : UserControl  
    {  
        public UserControl1()  
        {  
            InitializeComponent();  
            // this.userControl2.backUc1 = this.action; // 放这里,结果都是 null  
            // 这个绑定不能放构造函数。因为构造函数执行的时候 action = null。  
            // 主窗体先构造好子窗体,然后再给子窗体 action 赋值。  
            // 所以,绑定要放在子窗体构造完毕之后。  
        }  
  
        private UserControl2 userControl2 = new UserControl2() { Dock = DockStyle.Fill };  
        public Action<Control> LoadUserF2;  
  
        public Action action;  
  
        private void button1_Click(object sender, EventArgs e)  
        {  
            this.LoadUserF2?.Invoke(this.userControl2);  
            this.userControl2.backUc1 = this.action; // 在这绑定  
        }  
    }  
}  

UserControl2.cs

using System;  
using System.Collections.Generic;  
using System.ComponentModel;  
using System.Drawing;  
using System.Data;  
using System.Linq;  
using System.Text;  
using System.Threading.Tasks;  
using System.Windows.Forms;  
  
namespace WindowsFormsApp1  
{  
    public partial class UserControl2 : UserControl  
    {  
        public UserControl2()  
        {  
            InitializeComponent();  
        }  
  
        public Action backUc1;  
  
        private void button1_Click(object sender, EventArgs e)  
        {  
            backUc1?.Invoke();  
        }  
    }  
}  

要注意的地方:

UserControl1.cs 中的注释中说明。这里的委托绑定不能放在构造函数下,因为构造函数执行的时候 action = null。主窗体先构造好子窗体,然后再给子窗体 action 赋值。所以,这里的绑定要放在子窗体构造完毕之后。

posted @   double64  阅读(546)  评论(0编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
点击右上角即可分享
微信分享提示