C # Best Practices for passing parameters in winform

Source: Internet
Author: User

IntoAfter entering vs2005, you can find that the sub-form operation of the parent form cannot follow the method in section 2003: Change the space access attribute of the parent form from private to public. Ide has declared this part of the control.CodeHidden, so only a more object method is used.

The procedure for passing parameters between the parent and child forms is as follows:
1. Declare a static temporary object of the parent form type in the parent form
Public static frmfather frmfathertemp;

2. assign a value to the variable in the parent form constructor.

Public frmfather ()
{
Initializecomponent ();
Frmfathertemp = this;
}

3. Set the parameter to be passed as an attribute of the parent form and set the accessors. In the set method, the parameter is bound to the parent form control.

Private string testvalue;

Public String testvalue
{
Get
{
Return testvalue;
}
Set
{
This. testvalue = value;
This.txt father. Text = value;
}
}

4. assign values to the parameters to be passed in the parameter passing event of the parent form and open the child form. The work of the parent form ends here.

This. testvalue = this.txt father. text;
Frmson FRM = new frmson ();
FRM. showdialog ();

5. In the subform constructor, set the binding operation between passing parameters and the subform control.

Public frmson ()
{
Initializecomponent ();
This.txt son. Text = frmfather. frmfathertemp. testvalue;
}

6. assign a value to the parameter attribute of the temporary object of the parent form in the subform return event.

Public frmson ()
{
Initializecomponent ();
This.txt son. Text = frmfather. frmfathertemp. testvalue;
}

OK. Everything is done!

The Code is as follows:

Frmfather. CS

Using System;
Using System. Collections. Generic;
Using System. componentmodel;
Using System. Data;
Using System. drawing;
Using System. text;
Using System. Windows. forms;

Namespace Winformparameterpass
{
Public Partial Class Frmfather: Form
{
Public   Static Frmfather frmfathertemp;

Private   String Testvalue;

Public   String Testvalue
{
Get  
{
ReturnTestvalue;
}
Set  
{
This. Testvalue=Value;
This. Txtfather. Text=Value;
}
}

Public Frmfather ()
{
Initializecomponent ();
Frmfathertemp= This;
}

Private   Void Btnfather_click ( Object Sender, eventargs E)
{
This. Testvalue= This. Txtfather. text;
Frmson FRM= NewFrmson ();
FRM. showdialog ();
}
}
}

Frmson. CS Using System;
Using System. Collections. Generic;
Using System. componentmodel;
Using System. Data;
Using System. drawing;
Using System. text;
Using System. Windows. forms;

Namespace Winformparameterpass
{
Public Partial Class Frmson: Form
{
Public Frmson ()
{
Initializecomponent ();
This. Txtson. Text=Frmfather. frmfathertemp. testvalue;
}

Private   Void Btnson_click ( Object Sender, eventargs E)
{
Frmfather. frmfathertemp. testvalue= This. Txtson. text;
This. Close ();
}
}
}

ExampleProgram:
/Files/heekui/winformparameterpass.rar

The above is a reference for beginners.

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.