1. Handling OK and cancel
For modal form, if the dialogresult attribute is explicitly set by the developer to a value other than none, the dialog box will be closed automatically (the close () method will be called automatically ).
If you want to press enter and ESC, it is equivalent to pressing OK and cancel (in line with general operation habits ), to set the acceptbutton and cancelbutton attributes of the dialog box (you can directly set them in the attribute bar of the corresponding form, in the MISC category ).
Void initializecomponent (){
This. acceptbutton = This. okbutton;
This. cancelbutton = This. cancelbutton;
}
Generally, if the acceptbutton and cancelbutton attributes are set, the envent handler of the two buttons is not required.
Note that you must write a statement similar to the following in writing it after initializecomponent () in the constructor of form:
This. okbutton. dialogresult = dialogresult. OK;
This. cancelbutton. dialogresult = dialogresult. Cancel;
It cannot be written in initializecomponent () because this method is automatically generated by IDE.
2. For modeless form data
How can I bring the corresponding information back to the form that calls the modeless form when it is disabled? The answer is the event in. net.
Class propertiesdialog: FORM {
// Event when the accept button is pressed
Public event eventhandler accept;
Void acceptbutton_click (Object sender, eventargs e ){
// How to handle the accept button
If (accept! = NULL) accept (this, eventargs. Empty );
}
Void closebutton_click (Object sender, eventargs e ){
This. Close ();
}
}
// The following Code specifies the CS file corresponding to the main window.
Void showproperties_click (Object sender, eventargs e ){
Propertiesdialog DLG = new propertiesdialog ();
DLG. Accept + = new eventhandler (properties_accept );
DLG. Show ();
}
// Client handles event from form to access accepted values
Void properties_accept (Object sender, eventargs e ){
Propertiesdialog DLG = (propertiesdialog) sender;
This. Text = DLG. text;
}