form display in A.winform
There are 2 ways to display a form:
Form.ShowDialog method (form is displayed as a modal form)
Form.show method (form is shown as modeless form)
2 specific differences are as follows:
1. After the Form.show method is invoked, the code following the Show method executes immediately
2. After the Form.ShowDialog method is invoked, 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, the Close method of the form is not invoked when the user clicks the Closed Form button of the dialog box or sets the value of the DialogResult property.
is actually assigning the Visible property of the form to false, hiding the form
This hidden form is a new instance that can be displayed again without creating the dialog box
Because the form is not closed, call the Dispose method of the form when the form is no longer needed by your application
How can you tell if a form is a modal form?
True if the form is modal, or false, using the Form.modal property
False and True according to the modal property of the form displayed by show and ShowDialog
Special attention:
Because the display is not known until the form is created, the modal property always corresponds to False in the form constructor, so we can only use the modal property value in the Load event or later
How do I determine the owner relationship between forms?
Owner property of Form class: Owners of Forms
When a form is owned by another form, it is minimized and closed with the owner form.
For example, if Form2 is owned by the form Form1, the Form2 is also closed or minimized when Form1 is turned off or minimized.
For example, in the form Form1
Form2 F2 = new Form2 ();
F2. ShowDialog (this);
Or
F2. Show (this);
Or
F2. Owner = this;
F2. ShowDialog ();
So F2 's owner is Form1.
B.winform Form Pass Value
Learn more about the display of the form, and then summarize the method of the form's passing value:
1. Through the constructor
Features: The transfer value is one-way (can not be transmitted to each other), simple to achieve
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 the form Form1
New Form2 (111, "222"). Show (); This sends 111, "222", and these 2 values to Form2.
2. Through static variables
Features: Transfer value is bidirectional, simple to achieve
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 the form Form1
App.value = "F2"; Assigning values to static members
New Form2 (). Show (); Show Form2
In the form Form2
This. Text = App.value; Retrieve the value of App.value
App.value = "Form2"; Assign values to App.value so that other forms call
3. Public property values through the form
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 the form Form1
Form2 F2 = new Form2 ();
F2. Form2value = "OK"; Assign value to Form2 textBox1 OK
F2. ShowDialog ();
4. The public property value and owner property of the form
Features: Simple to implement, flexible
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
Form2 's owner is Form1.
Form1 f1 = (Form1) this. Owner;
The value of the FORM1 is 1.
MessageBox.Show (F1. Form1value. ToString ());
Assign a value of Form1value to Form1 222
F1. Form1value = 222;
5. The public property values and Application.openforms properties of the form
Description: Application.openforms property: Gets the collection of open forms that belong to the application. (This property is in. NET Framework2.0 Edition)
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 of the FORM1 is 1.
MessageBox.Show (F1. Form1value.tostring ());
Assign a value of Form1value to Form1 222
F1. Form1value = 222;
}
6. Adoption of the event
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 a 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, passing its own 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 the Form2 reference through a simple type conversion
Form2 F2 = (Form2) sender;
Received the TextBox1.Text of Form2
This.textBox1.Text = F2. Form2value;
}
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.