標籤:
一、編碼思路
在父表單使用Lambda運算式自訂事件賦給子表單內定義的事件,通過在子表單內呼叫事件實現傳參
二、傳遞的參數
建立檔案SomeUtility.cs,在類SomeUtility中聲明結構ReturnStruct
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace EventTest{ public class SomeUtility { public struct ReturnStruct { public string sData1; public string sData2; public string sData3; public ReturnStruct(string s1, string s2, string s3) { sData1 = s1; sData2 = s2; sData3 = s3; } } }}
三、子表單相關
表單FormTip,裡面有三個TextBox,單擊確定會將三個TextBox的值傳遞到父表單中
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 EventTest{ public partial class FormTip : Form { public class TEventArgs<T> : EventArgs { public T Args { get; private set; } public TEventArgs(T args) { this.Args = args; } } [Browsable(false)] internal event EventHandler<TEventArgs<SomeUtility.ReturnStruct>> EventTest; SomeUtility.ReturnStruct rs; public SomeUtility.ReturnStruct GetReturnStruct() { return rs; } //建構函式1:無參數 public FormTip() { InitializeComponent(); } //建構函式2:有一個ReturnStruct型別參數 public FormTip(SomeUtility.ReturnStruct r) { InitializeComponent(); this.rs.sData1 = r.sData1; this.rs.sData2 = r.sData2; this.rs.sData3 = r.sData3; } private void FormTip_Load(object sender, EventArgs e) { this.txtData1.Text = rs.sData1; this.txtData2.Text = rs.sData2; this.txtData3.Text = rs.sData3; } //單擊“確定”按鈕:呼叫事件EventTest private void btnOK_Click(object sender, EventArgs e) { //呼叫事件EventTest if (this.EventTest != null) { SomeUtility.ReturnStruct rs = new SomeUtility.ReturnStruct( this.txtData1.Text, this.txtData2.Text, this.txtData3.Text); this.EventTest(this, new TEventArgs<SomeUtility.ReturnStruct>(rs)); } //退出視窗 this.Close(); } //單擊“取消”按鈕:直接退出 private void btnCancel_Click(object sender, EventArgs e) { this.Close(); } }}
四、父表單相關
表單FormMain,單擊按鈕btnTest開啟子表單,並在單擊子表單的“確定”按鈕後根據子表單內設定的值更新txtResult中的常值內容
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 EventTest{ public partial class FormMain : Form { public FormMain() { InitializeComponent(); } private void FormMain_Load(object sender, EventArgs e) { this.txtResult.Text = "1:- 2:- 3:-"; } private void btnTest_Click(object sender, EventArgs e) { SomeUtility.ReturnStruct rsTest = new SomeUtility.ReturnStruct("Data1", "Data2", "Data3"); FormTip frmTip = new FormTip(rsTest); frmTip.EventTest += (obj, args) => { SomeUtility.ReturnStruct rsTemp = frmTip.GetReturnStruct(); rsTest.sData1 = args.Args.sData1; rsTest.sData2 = args.Args.sData2; rsTest.sData3 = args.Args.sData3; this.txtResult.Text = string.Format("1:{0} 2:{1} 3:{2}", rsTest.sData1, rsTest.sData2, rsTest.sData3); MessageBox.Show("賦值成功"); }; frmTip.ShowDialog(); } }}
五、運行樣本
1)開啟父表單FormMain
2)開啟子表單FormTip,輸入三個值
3)將三個值傳回父表單FormMain
END
C# 一個子表單向父表單傳遞參數的例子