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(null, null);
}
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
赋值。所以,这里的绑定要放在子窗体构造完毕之后。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了