WinForm development, form display and form value passing knowledge, winform knowledge
In the past, I did not know much about WinForm display and passing values between forms.
Recently, some WinForm development has been done, and the related knowledge used is organized as follows:
A. display the form in WinForm
You can use either of the following methods to display a form:
Form. ShowDialog method (the Form is displayed as a mode Form)
Form. Show method (the Form is displayed as a Form without mode)
The specific differences between the two are as follows:
1. After the Form. Show method is called, the code after the Show method is executed immediately.
2. After the Form. ShowDialog method is called, the code after this method is executed until the dialog box is closed.
3. When the form is displayed as a mode form, click the close button to hide the form and set DialogResult to DialogResult. Cancel.
Unlike the mode-free form, when you click the Close form button in the dialog box or set the value of the DialogResult attribute, the Close method of the form is not called.
In fact, the Visible attribute of the form is assigned to false to hide the form.
In this way, the hidden form can be re-displayed without creating a new instance in the dialog box.
Because the form is not closed, call the Dispose method of the form when the application no longer needs the form.
How can I determine whether a form is a mode form?
Use the Form. Modal attribute. If the Form is displayed in the mode, the value is true; otherwise, the value is false.
The Modal attributes of the form displayed through Show and ShowDialog correspond to false and true respectively.
Note:
Since the display mode cannot be known before the form is created, in the form constructor, the Modal attribute always corresponds to false, so we can only use the Modal attribute value in the Load event or later.
How to determine the Owner Relationship between forms?
Owner attribute of Form class: Form Owner
When a form falls into another form, it is minimized and closed with the owner form.
For example, if Form2 is owned by the form Form1, when Form1 is disabled or minimized, Form2 is also disabled or minimized.
For example, in Form Form1
Form2 f2 = new Form2 ();
F2.ShowDialog (this );
// Or
F2.Show (this );
// Or
F2.Owner = this;
F2.ShowDialog ();
In this way, the f2 owner is Form1.
B. input values in WinForm
I learned about the display of the form, and then summarized the method of passing values in the form:
1. Constructor
Features: it is easy to transmit values in one way (values cannot be transferred to each other ).
The implementation code is as follows:
In 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 (); // in this way, 111, "222", these two values are sent to Form2
2. Use static variables
Features: The value transfer function is bidirectional and easy to implement.
The implementation code is as follows:
Define a static member value in an app class
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 (); // display Form2
In form Form2
This. Text = app. value; // retrieves the value of app. value.
App. value = "Form2"; // assign a value to app. value so that other forms can call
3. Public attribute values of the form
Features: easy to implement
The implementation code is as follows:
Define a public attribute Form2Value in the form Form2 to 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 OK to textBox1 of Form2
F2.ShowDialog ();
4. Public attribute values and Owner attributes of the form
Features: simple and flexible
The implementation code is as follows:
In form Form1
Public int Form1Value = 1;
Form2 f2 = new Form2 ();
F2.ShowDialog (this); // pass Form1 as the Form2 owner to Form2
In form Form2
// Form2 is owned by Form1
Form1 f1 = (Form1) this. Owner;
// The value of Form1 is 1.
MessageBox. Show (f1.Form1Value. ToString ());
// Assign a value of 222 to Form1Value of Form1
F1.Form1Value = 222;
5. Public attribute values of the form and Application. OpenForms attributes
Description: Application. OpenForms attributes: Get the set of open forms of the Application. (This attribute is in. NET Framework2.0)
The implementation code is as follows:
In form Form1
Public int Form1Value = 1;
Form2 f2 = new Form2 ();
F2.Show ();
In form Form2
String formName = "Form1 ";
Form fr = Application. OpenForms [formName];
If (fr! = Null)
{
Form1 f1 = (Form1) fr;
// The value of Form1 is 1.
MessageBox. Show (f1.Form1Value. ToString ());
// Assign a value of 222 to Form1Value of Form1
F1.Form1Value = 222;
}
6. Events
The implementation code is as follows:
Define the public attribute Form2Value in the form Form2 to get and set the text value of textBox1
It also defines an accept event.
Public string Form2Value
{
Get
{
Return this. textBox1.Text;
}
Set
{
This. textBox1.Text = value;
}
}
Public event EventHandler accept;
Private void button#click (object sender, EventArgs e)
{
If (accept! = Null)
{
Accept (this, EventArgs. Empty); // when the form triggers an event, pass its own reference
}
}
In form Form1
Form2 f2 = new Form2 ();
F2.accept + = new EventHandler (f2_accept );
F2.Show ();
Void f2_accept (object sender, EventArgs e)
{
// The Event receiver obtains the reference of Form2 through a simple type conversion.
Form2 f2 = (Form2) sender;
// Receives the textBox1.Text of Form2.
This. textBox1.Text = f2.Form2Value;
}