WinForm Development, form display and form transfer values

Source: Internet
Author: User
Previous understanding of WinForm form display and form transfer values is not clear
Recently did some WinForm development, the use of relevant knowledge to organize the following

A.winform in the form display
There are 2 ways to display the form:
Form.ShowDialog method (The form is displayed as a modal form)
Form.show method (The form is displayed as a modeless form)

The 2 specific differences are as follows:
1. After calling the Form.show method, the code following the Show method executes immediately
2. After calling the Form.ShowDialog method, the code following this method is not executed until the dialog box is closed
3. When the form is displayed as a modal form, clicking the Close button hides the form and sets the DialogResult property to DialogResult.Cancel
Unlike a modeless form, when a user clicks the Close Form button on a dialog box or sets the value of the DialogResult property, the form's Close method is not called
is actually assigning the Visible property of the form to false, hiding the form
So the hidden form can be re-displayed without creating a new instance of the dialog box
Because the form is not closed, call the Dispose method of the form when the form is no longer needed by the application

How do you tell if a form is a modal form?
Use the Form.modal property to True if the form is a modal display, otherwise false
The modal property of the form that is displayed by show and ShowDialog corresponds to False and true, respectively
Special attention:
Because the display mode is not known until the form is created, the modal property always corresponds to False in the form constructor, so we can only take advantage of the modal property value in the Load event or later

How do I determine the owner relationship between forms?
Owner property of Form class: Owner of the forms
When a form is owned by another form, it is minimized and closed with the owner form.
For example, if Form2 is Form1 all of the form, Form2 is also turned off or minimized when Form1 is closed or minimized.

For example, in form Form1
Form2 F2 = new Form2 ();
F2. ShowDialog (this);
Or
F2. Show (this);
Or
F2. Owner = this;
F2. ShowDialog ();

So the owner of the F2 is Form1.

B.winform form pass-through value
Learn about the display of the form, and then summarize the form's method of passing the value:

1. Through the constructor function
Features: Pass value is unidirectional (can not pass the value of each other), to achieve a simple
The implementation code is as follows:
In the form Form2
int value1;
String value2;

Public Form2 (int value1, string value2)
{
InitializeComponent ();

this.value1 = value1;
This.value2 = value2;
}

This is called in form Form1.
New Form2 (111, "222"). Show (); This gives the 111, "222", 2 values to the Form2

2. Passing Static variables
Features: The pass value is bidirectional, the realization is simple
The implementation code is as follows:
Define a static member in an app class value
public class App
{
public static string value;
}

This is called in form Form1.
App.value = "F2"; Assign a value to a static member
New Form2 (). Show (); Show Form2

In the form Form2
This. Text = App.value; Retrieving the value of the App.value
App.value = "Form2"; Assigns a value to app.value so that other forms call

3. Pass the form's public property value
Features: Simple to implement
The implementation code is as follows:

Define a public property form2value in form Form2, get and set the text value of TextBox1
public string Form2value
{
Get
{
return this.textBox1.Text;
}
Set
{
This.textBox1.Text = value;
}
}

This is called in form Form1.
Form2 F2 = new Form2 ();
F2. Form2value = "OK"; Assign the TextBox1 to Form2 OK
F2. ShowDialog ();

4. Through the form's public property values and the Owner property (recommended)
Features: simple and flexible implementation
The implementation code is as follows:
In the form Form1
public int form1value = 1;

Form2 F2 = new Form2 ();
F2. ShowDialog (this); Pass the Form1 as the owner of the Form2 to Form2

In the form Form2
The owner of the Form2 is Form1
Form1 f1 = (Form1) this. Owner;
The value taken to Form1 is 1.
MessageBox.Show (F1. Form1value. ToString ());
Assign a value of 222 to Form1 's Form1value
F1. Form1value = 222;

5. Through the form's public property values and Application.openforms properties
Description: Application.openforms property: Gets the collection of open forms that belong to the application. (This property is in. NET version Framework2.0)
The implementation code is as follows:
In the form Form1
public int form1value = 1;

Form2 F2 = new Form2 ();
F2. Show ();

In the form Form2
String formName = "Form1";
Form FR = application.openforms [FormName];

if (fr! = null)
{
Form1 f1 = (Form1) fr;
The value taken to Form1 is 1.
MessageBox.Show (F1. Form1value.tostring ());
Assign a value of 222 to Form1 's Form1value
F1. Form1value = 222;
}

6. Passing Events
The implementation code is as follows:
To define a public property form2value in form Form2, get and set the text value of TextBox1
And also defines an accept event
public string Form2value
{
Get
{
return this.textBox1.Text;
}
Set
{
This.textBox1.Text = value;
}
}

public event EventHandler Accept;
private void Button1_Click (object sender, EventArgs e)
{
if (accept! = null)
{
Accept (this, eventargs.empty); When a form triggers an event, it passes itself a reference
}
}

In the form Form1
Form2 F2 = new Form2 ();
F2.accept + = new EventHandler (f2_accept);
F2. Show ();

void F2_accept (object sender, EventArgs e)
{
The receiver of the event gets a Form2 reference by a simple type conversion
Form2 F2 = (Form2) sender;
Received the TextBox1.Text of the Form2
This.textBox1.Text = F2. Form2value;
}

  • Related Article

    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.