Example of passing parameters to a parent form in C # one child form

Source: Internet
Author: User

First, the idea of coding

Use the lambda expression custom event in the parent form to assign events defined in the child's body, by invoking the event implementation in the child window

Second, the parameters passed

Create a file SomeUtility.cs, declare the structure in class 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;             }        }    }}

Iii. child form Related


Form Formtip, with three textbox in it, click OK to pass the value of three TextBox to the parent form

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;             }&Nbsp;           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;         }        //Constructor 1: No parameters          public formtip ()         {             initializecomponent ();         }        //Constructor 2: There is a returnstruct type parameter          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;         }        //Click "OK" Button: Call Event Eventtest        private void btnok_click (object  sender, eventargs e)         {             //Invoke Event 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));             }             //Exit Window             this. Close ();         }        //click Cancel button: Exit directly        private void btncancel_click (Object sender,  eventargs e)         {             this. Close ();         }    }}

Iv. parent Form Related


Form FormMain, click the button btntest open the subform and update the text content in the Txtresult based on the value set in the child window after clicking the OK button on the subform

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}",             &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;RSTEST.SDATA1,&NBSP;RSTEST.SDATA2,&NBSP;RSTEST.SDATA3);                 messagebox.show (" Assignment succeeded ");            };             frmtiP.showdialog ();         }    }} 

V. Running the sample

1) Open the parent form FormMain

2) Open Subform Formtip, enter three values

3) pass three values back to the parent form FormMain

END

Example of passing parameters to a parent form in C # one child form

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.