Data Interaction Between winforms

Source: Internet
Author: User

C # three methods for implementing Data Interaction Between winforms

1. Modify the constructor of the subform:

To put it simply, modify the constructor of the subform Form, as shown below:

Public Frm_Child (string Para1, ArrayList List1, TextBox textBox1, Form. Frm_Main) {InitializeComponent ();}

In the preceding example, four parameters are added for the child form Frm_Child, namely: String Para1, character array List1, text control textBox1 and parent form Frm_Main, the following is a simple distinction between their respective features:

<1> string: one-way value transfer. Data in the parent form cannot be indirectly changed by modifying the value of this parameter in the child form.

<2> character array: it refers to the transfer of reference type. You can change the member variables in the parent and child forms to the same array, and update the data of the other party at the same time.

<3> Control: transfer of reference type. You can modify the data of the control to directly control the data of the control in the parent form.

<4> form: This is the most direct method. The parent form is directly transmitted to the child form. We can easily modify all data authorized as public in the parent form in the child form.

2. Add attributes or methods to the form:

Obtain or set a form that owns the form. To make a form belong to another form, assign a reference to the form that will become the Owner for its Owner attribute. When a form falls into another form, it is minimized and closed with the owner form. For example, if Frm_Child belongs to the form Frm_Main, Frm_Child is also disabled or minimized when Frm_Main is disabled or minimized. And the affiliated form is never displayed behind the owner form. The affiliated forms can be used to find and replace windows and other windows. When the owner form is selected, these windows should not disappear. To determine the form owned by a parent form, use the OwnedForms attribute.

We can use the following method to determine the subordinate form relationship:

Frm_Child form. = new Frm_Child ();
Form. Owner = this;
Form. ShowDialog ();

Or

Frm_Child form. = new Frm_Child ();
Form. ShowDialog (this );

After the subordination is set, we can modify data in the Form load event or by defining public attributes or methods. The simple operation is as follows:

Frm_Main pareForm. = (Frm_Main) this. Owner;

3. Delegate:

Compared with the above two methods, this method may be more complicated, but the basic idea is the same. The points to be modified are as follows:

<1> Add the delegate function definition in Frm_Child.cs: public delegate void SendFun (string str );

<2> Add a delegation example in Frm_Child.cs: public event SendFun SendToParent;

<3> Add a button in Frm_Child.cs and add its event as follows:

Private void button#click (object sender, EventArgs e)
{
If (Send! = Null)
{
Send (this. textBox1.Text );
}
}

<4> Add the RecvInfo () method to Frm_Main.cs as follows:

Private void RecvInfo (string str)
{
TextBox1.Text = str;
}

<5> Add the delegate instance definition in the InitializeComponent () method in Frm_Main.Designer.cs:

This. myForm. Send + = new Example3_Frm_Child. SendFun (RecvInfo );

The preceding five steps allow communication between forms.

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.