天天看点

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

我们知道 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();  
        }  
    }  
}      

要注意的地方: