winform利用委托传值到datagridview_C# Winform 跨多级窗体/控件传值
生活随笔
收集整理的這篇文章主要介紹了
winform利用委托传值到datagridview_C# Winform 跨多级窗体/控件传值
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
我們知道 C# winform 跨窗體傳值,子父窗體交互一般用委托來實現(xiàn)。之前都是子窗體和父窗體兩級交互,如果子窗體再生一個子子窗體,然后子子窗體調(diào)用父窗體函數(shù),這樣該如何操作?
我想到的實現(xiàn)方式還是用委托變量一級一級的往下傳。
下面是實現(xiàn)的效果:
▲ Form1 加載 Uc1,在 Uc1 下加載 Uc2,Uc2 下返回 Uc1Form1.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;?//?放這里,結(jié)果都是?null
????????????//?這個綁定不能放構(gòu)造函數(shù)。因為構(gòu)造函數(shù)執(zhí)行的時候 action = null。
????????????//?主窗體先構(gòu)造好子窗體,然后再給子窗體 action 賦值。
????????????//?所以,綁定要放在子窗體構(gòu)造完畢之后。
????????}
????????private?UserControl2?userControl2?=?new?UserControl2()?{?Dock?=?DockStyle.Fill?};
????????public?Action?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 中的注釋中說明。這里的委托綁定不能放在構(gòu)造函數(shù)下,因為構(gòu)造函數(shù)執(zhí)行的時候 action = null。
主窗體先構(gòu)造好子窗體,然后再給子窗體 action 賦值。所以,這里的綁定要放在子窗體構(gòu)造完畢之后。
總結(jié)
以上是生活随笔為你收集整理的winform利用委托传值到datagridview_C# Winform 跨多级窗体/控件传值的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: adg类似于mysql半同步机制_MyS
- 下一篇: mysql 6.2使用_2. MYSQL