The show method of the form, without any notification to the calling code. If you need a notification, using showdialog is a good choice.
After the show method is called, the code after the show method is executed immediately. After the showdialog method is called, The called code is paused. After the showdialog method is called, the Code continues to be executed. In addition, the form can return a dialogresult value, which describes the reason for closing the form, such as OK, cancel, Yes, and no. In order for the form to return a dialogresult, you must set the dialogresult value of the form or set the dialogresult attribute on a button of the form.
Example:
The following is the subform code. You must enter the phone and return it to the parent form. Using system;
Using system. Collections. Generic;
Using system. componentmodel;
Using system. Data;
Using system. drawing;
Using system. text;
Using system. Windows. forms;
Namespace windowsapplication1
{
Public partial class phone: Form
{
Public phone ()
{
Initializecomponent ();
Btnok. dialogresult = dialogresult. OK;
Btnok. dialogresult = dialogresult. Cancel;
}
Public String phonenumber
{
Get {return textbox1.text ;}
Set {textbox1.text = value ;}
}
Private void phone_load (Object sender, eventargs E)
{
}
}
}
It does not contain any code for processing button click events. Because the dialogresult attribute of each button is set, the form disappears after you click OK or cancel. The following code shows how to call the phone dialog box in the parent form. Using system;
Using system. Collections. Generic;
Using system. componentmodel;
Using system. Data;
Using system. drawing;
Using system. text;
Using system. Windows. forms;
Namespace windowsapplication1
{
Public partial class form7: Form
{
Public form7 ()
{
Initializecomponent ();
}
Private void button#click (Object sender, eventargs E)
{
Phone FRM = new phone ();
FRM. showdialog ();
If (FRM. dialogresult = dialogresult. OK)
{
Label1.text = "phone number is" + frm. phonenumber;
}
Else if (FRM. dialogresult = dialogresult. Cancel)
{
Label1.text = "form was canceled ";
}
FRM. Close ();
}
}
}
It looks very simple. Create a new phone object frm and call frm. the showdialog method is to stop the code, wait for the phone form to return, and then check the dialogresult attribute of the phone form. Because the form has not been released and is invisible, you can still access the public attribute phonenumber, once the required data is obtained, you can use the form close method.
Everything is normal, but what if the returned format is incorrect? Put the showdialog method in the loop and you can call it again, so that the user can re-enter the method to get the correct value.
The above code is changed to the following.
Using system;
Using system. Collections. Generic;
Using system. componentmodel;
Using system. Data;
Using system. drawing;
Using system. text;
Using system. Windows. forms;
Namespace windowsapplication1
{
Public partial class form7: Form
{
Public form7 ()
{
Initializecomponent ();
}
Private void button#click (Object sender, eventargs E)
{
Phone FRM = new phone ();
While (true)
{
FRM. showdialog ();
If (FRM. dialogresult = dialogresult. OK)
{
Label1.text = "phone number is" + frm. phonenumber;
If (FRM. phonenumber. Length = 8 | frm. phonenumber. Length = 12)
{
Break;
}
Else
{
MessageBox. Show ("");
}
}
Else if (FRM. dialogresult = dialogresult. Cancel)
{
Label1.text = "form was canceled ";
Break;
}
}
FRM. Close ();
}
}
}