日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > C# >内容正文

C#

winform利用委托传值到datagridview_C# Winform 跨多级窗体/控件传值

發布時間:2024/10/14 C# 118 豆豆
生活随笔 收集整理的這篇文章主要介紹了 winform利用委托传值到datagridview_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?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 賦值。所以,這里的綁定要放在子窗體構造完畢之后。

總結

以上是生活随笔為你收集整理的winform利用委托传值到datagridview_C# Winform 跨多级窗体/控件传值的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。