Value passing between winform forms

Source: Internet
Author: User

To specify the object to which the values are transmitted between forms, otherwise, it is easy to transmit the error values. For example, if the form object loginFrm is a login form and you want to pass the current Login User information to the main form mainFrm, but a new object loginFrm is added to the mainFrm, therefore, we get an empty value, which is quite appealing. Different memory spaces, such as the wide lake, store different objects and distinguish them clearly. The prerequisite for passing values in the form is that the access modifier of the field to be passed in the class must be public (if passing values in the source form is an exception, see the following common method 2 ).
Common Methods:
1. Static variables: Write A static variable A in Class Form1 in the form and assign values to it. In this project, it can be called through Form1.A. This method is not recommended and is not safe.
2. A safe method is to first create a public attribute B in the form2 form of the value to be passed as the receiver in the Process of value passing. When form2 is an instance in form1, the value A to be passed is assigned to the new instance form2. B, or is passed through the constructor of the Form2 class. There are many methods to pass the value, which is safer (recommended ).
3. If you want to get a set of fixed data, you can write a public method with return values in the source form. Then, in the target form, the instance source form object is implemented and this method is called to obtain data. Although this method is different from static variables, It is similar and does not need to be emphasized. The disadvantage is that the data transmitted in the method body is fixed and cannot be changed.
4. in the query dialog box, you have used notepad to query the selected text. When you open the query dialog box, the selected text is displayed in the query dialog box, the code is implemented as follows:
In Form1: we want to pass a text to the Form2 form, which is assumed to be passText.
Public string passText
{
Get {return textBox1.Text ;}
}
In Form1, there is a button button1 in its click event:
Private void button#click (object sender, EventArgs e)
{
Form2 f2 = new Form2 (); // er .. Not to mention this.
F2.Owner = this; // set the attachment to establish a link ~
F2.Show (); // you can see it.
}
In the Form2 object:
Assume that there is a text box txtBox1.
Private void form2_Load (object sender, EventArgs e)
{
// Find the source form that owns the target form and obtain an object
Object. to convert it to the Form1 type, you can obtain its public attribute passText.
TextBox1.Text = (Form1) this. Owner). passText;
}
In this way, when Form2 is opened, information about the Form1 text box will be displayed in the text box (to display the selected text in the query dialog box, you only need to change the attributes of passText to textBox. selectedText .)
The main advantage of this method is that we can find the object of the source form in the object of the target form. The difference lies in the two cases above.

5. synchronous value transfer between forms is to update the information between two forms in real time. If you are interested, you may find that the source form can be found in the target form, we can also use the source form to control the target form. From this point, we can achieve this. The Code is as follows: In Form1:
Program code:

Public partial class Form1: Form
{
Public Form1 ()
{
InitializeComponent ();
}

Public string passText
{
Get {
Return textBox1.SelectedText. Text;
}
}

Private void button#click (object sender, EventArgs e)
{
Form2 f2 = new Form2 ();
F2.Owner = this;
F2.Show ();
}

Private void Form1_Load (object sender, EventArgs e)
{

}

Private void textBox1_TextChanged (object sender, EventArgs e)
{
If (this. OwnedForms. Length! = 0)
(This. ownedForms [0] as Form2 ). selectText = textBox1.Text;/* OwnedForms is an array that indicates the subform array of the current object. You must also convert the elements obtained by subscript to the Form2 type. */
}
}
}

Namespace WindowsApplication1
{
Public partial class Form2: Form
{
Public string selectText
{
Set
{
TextBox1.Text = value; // set an attribute for the form1 operation
}
}

Public Form2 ()
{
InitializeComponent ();
}

Private void Form2_Load (object sender, EventArgs e)
{
TextBox1.Text = (Form1) this. Owner). passText;
}
}
}

Finally, let's talk about it. When passing values, we can not only pass variable attributes, but also have a good thing. Each form has a Tag attribute, which is a Tag, object type. We can assign a class to it. This provides great convenience for our code. You can use the above four value passing methods.

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.