C# 一個子表單向父表單傳遞參數的例子

來源:互聯網
上載者:User

標籤:

一、編碼思路

在父表單使用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# 一個子表單向父表單傳遞參數的例子

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.