Mode forms in Development Based on. NET platform

Source: Internet
Author: User

Abstract:

This article describes howProgramMany aspects of the mode form used in development extend some content to the application of the general form.
Click here to download the example of this ArticleCode.

Overview
What is a mode form? It can be simply understood as a form dialog box. You must complete the operation on the form or close the form before returning the form to open the form. This article does not discuss the definition, features, and functions of pattern forms in detail, but mainly focuses on how to apply them.. Net form, which solves common problems encountered in the format.

Mode form attribute settings
In.. net. windows. forms. the Form class represents a form. You can directly add a form through the Visual Studio 2005 designer and switch to the design mode. The properties and events of the form are displayed in the Properties window. Refer to the standard mode form. Take the option dialog box opened by the menu tool> Option in Visual Studio 2005 program as an example. You still need to set the format initialized by the designer to be professional. Fortunately, these settings can all be implemented through attribute settings in the designer mode. The author will use the code to implement the corresponding functions and describe them in detail.
Form. startposition attribute to determine the position when the form appears for the first time. It is set to display in the middle of the parent form.
This . Startposition =Formstartposition . Centerparent;
Form. helpbutton attribute to determine whether the "help" button is in the Form title bar. Set display, which looks more user-friendly, but does not necessarily implement the help function.
This . Helpbutton = True ;
Form. maximizebox attribute to determine whether there is a maximize box in the upper right corner of the Form title bar. Set to not display her.
This . Maximizebox = False ;
Form. minimizebox attribute, and determine whether there is a minimization box in the upper right corner of the Form title bar. Set to prevent display.
This . Minimizebox = False ;
Form. showicon attribute, indicating whether to display the icon in the Form title bar. The settings are not displayed.
This . Showicon = False ;
Form. showintaskbar attribute to determine whether the form appears in the Windows taskbar. Of course, this will save valuable space on the taskbar.
This . Showintaskbar = False ;
Form. formborderstyle attribute, indicating the appearance and behavior of the border and title bar of the form. Setting this attribute will not allow you to drag and adjust the size of the Form. At the same time, the icon attribute will be invalid and the Form title bar image will not be displayed.
This . Formborderstyle = Formborderstyle . Fixeddialog;
Form. controlbox attribute to determine whether the form has a "control/system" menu box. You can use this setting to hide the control button of the title bar. In some cases, it is necessary to set it to false so that there will be no control buttons in the title bar.
This . Controlbox = False ;
By setting the above attributes, the static function of the mode form is basically implemented. You can determine whether to adjust the size of the form based on the actual situation.

Button in the mode form
In the mode form (for example, the "options" dialog box in Visual Studio 2005), there are two basic buttons: one [OK] button for submission and the other [cancel] button for uncommitting, sometimes a [application] button is added, but the "about" mode form in the "help" menu may only have a [OK] button. Windows Forms provide good support for user friendliness. The acceptbutton and cancelbutton attributes can be found in the attribute settings of the form design interface. The default value is null, indicating that the acceptbutton and cancelbutton attributes are displayed (none ). In properties, you can set the value by selecting the button on the form. The code generated by modifying the property is as follows.
Define two buttons first,
Private System. Windows. forms. Button Buttonok;
Private System. Windows. forms. Button Buttoncancel;
The "accept" button of the form. If this button is set, each time you press enter, it is equivalent to clicking this button.
This . Acceptbutton = This. buttonok;
The "cancel" button of the form. If this button is set, each time you press the ESC key, it is equivalent to clicking this button.
This . Cancelbutton = This. buttoncancel;
It can be seen that you can easily access a specific button through a shortcut key, but there are some exceptions, such as the form focus is on buttoncancel, when you press {enter}, the actually pressed key will be buttoncancel instead of buttonok. If the focus is on the third button, the {enter} press will be equivalent to clicking this button. Another detail is that the behavior of clicking the button with the mouse and the shortcut key operation button is different. The shortcut key operation button does not display the Display Effect of the button being pressed, and it seems that nothing has happened.

Mode form opening and closing
When it comes to opening a mode form, we generally use form. the showdialog () method or an overloaded form. showdialog (iwin32window). The last method displays the form as a mode dialog box with the specified owner. The following code shows,
OptionformForm =New Optionform();
// Form. showdialog ();
Form. showdialog (This);

For a mode form opened in the specified owner mode, you can obtain the reference of the main form within the mode form,
// Access the corresponding form within the mode form
MainformForm =This. OwnerAs Mainform;
NOTE: If it is opened in form. showdialog () mode, the form. Owner attribute will be blank reference.

When it comes to mode form closure, let's take a look at the returned values after mode form closure. Whether you call the form. showdialog () method or form. showdialog (iwin32window) method, the system. Windows. Forms. dialogresult enumeration value is returned when the mode form is closed. Refer to msdn. The enumerated values are as follows,
Dialogresult. Abort. the return value of the dialog box is abort (usually sent from the "Abort" button ).
Dialogresult. Cancel. the return value of the dialog box is cancel (usually sent from the button labeled "cancel ).
Dialogresult. Ignore. the return value of the dialog box is ignore (usually sent from the button labeled "Ignore ).
Dialogresult. No. The return value of the dialog box is no (usually sent from the "no" button ).
Dialogresult. None, which returns nothing from the dialog box. This indicates that the mode dialog box continues to run.
Dialogresult. OK. the return value of the dialog box is OK (usually sent from the button labeled "OK ).
Dialogresult. Retry. the return value of the dialog box is retry (usually sent from the button labeled "retry ).
Dialogresult. Yes. The return value of the dialog box is yes (usually sent from the button labeled "yes ).
For some reason, in actual user operations, for example, the option data cannot be saved and the input setting data is incorrect. Click the [OK] button to prevent the form from being closed to adjust the input settings. For some developers Technical Community I don't think it is a good implementation. The following code describes the implementation. Note that three events are used.
// Register the form close event
This . Formclosing + = New System. Windows. forms. Formclosingeventhandler ( This . Optionform_formclosing );
// Register the confirm button event
This . Buttonok. Click + = New System. Eventhandler ( This . Buttonok_click );
// Register the cancel button event
This . Buttoncancel. Click + = New System. Eventhandler ( This . Buttoncancel_click );
The event handler corresponding to the three events is as follows,
// Confirm the button Handler
Private Void Buttonok_click (Object Sender, Eventargs E)
{
// Assume that textboxpath is used to record the directory path. If no directory path exists, you must reset it.
If ( This . Textboxpath. Text. Trim (). Length = 0)
{
MessageBox . Show ( "The entered path is incorrect! " );
This . Textboxpath. Focus ();
}
Else
{
This . Dialogresult = Dialogresult . OK;
}
}
// Cancel the button Handler
Private Void Buttoncancel_click ( Object Sender, Eventargs E)
{
This . Dialogresult = Dialogresult . Cancel;
}
// Close the form handler, which occurs when the form is closed.
Private Void Optionform_formclosing ( Object Sender, Formclosingeventargs E)
{
If ( This . Dialogresult! = Dialogresult . Cancel && This . Dialogresult! =Dialogresult . OK)
E. Cancel = True ;
}

The above code is normal, that is, the event is too much to write, modify the above Code, remove the [cancel] button event and form close event and related event handler. First, you must set the dialogresult attribute of the button in the form constructor to return a specific dialogresult.
This. Buttonok. dialogresult = system. Windows. forms.Dialogresult. OK;
This. Buttoncancel. dialogresult = system. Windows. forms.Dialogresult. Cancel;

Register the confirm button event,
// Register the confirm button event
This . Buttonok. Click + = New System. eventhandler (This . Buttonok_click );
// Confirm the button Handler
Private Void Buttonok_click ( Object Sender, Eventargs E)
{
If ( This . Textboxpath. Text. Trim (). Length = 0)
{
MessageBox . Show ( "The entered path is incorrect! " );
This . Textboxpath. Focus ();
// Set the text box focus
This . Dialogresult = Dialogresult . None;
}
}
It can be seen that the code of the new implementation method is halved.

. format provided by Net Framework
. net Framework provides some commonly used dialog boxes, saving a lot of trouble in the development process. We will introduce them below.
MessageBox. Displays message boxes that contain text, buttons, and symbols (notifications and instructions. Use the MessageBox. Show static method to open the mode dialog box.
Public static dialogresult show (string text);
This method contains multiple overloaded versions. A complicated method is as follows:
Public static dialogresult show (iwin32window owner, string text, string caption, messageboxbuttons buttons, messageboxicon icon, messageboxdefaultbutton ultbutton, messageboxoptions options, string helpfilepath, helpnavigator navigator, object PARAM);

you can customize the dialog box based on different parameters.
some other dialogs provide specific functions.
openfiledialog. The open file dialog box inherits from the filedialog class and prompts the user to open the file. This class cannot be inherited. It is common to open files.
savefiledialog. The Save file dialog box inherits from the filedialog class and prompts you to select the file storage location. This class cannot be inherited.
folderbrowserdialog. The directory browsing dialog box inherits from the commondialog class and prompts you to select a folder. This class cannot be inherited. Fontdialog. The font Setting Dialog Box inherits from the commondialog class, prompting you to select a font from the font installed on your local computer. This class can be inherited.
colordialog. The color Setting dialog box, inherited from the commondialog class, represents a general dialog box that displays available colors and controls that allow users to define custom colors. This class can be inherited.
pagesetupdialog. The print page Setting Dialog Box inherits from the commondialog class and allows you to change the page-related printing settings, including margins and paper directions. This class cannot be inherited.
printdialog. The Print dialog box inherits from the commondialog class. You can select a printer and select the part to be printed in the document. This class cannot be inherited.
printpreviewdialog. Print the preview dialog box, inherited from the form class, indicating the dialog box containing printpreviewcontrol. This class can be inherited. Because this class inherits from the form class, besides
printpreviewdialog. showdialog ();
printpreviewdialog. showdialog (iwin32window);
you can use the printpreviewdialog method to open the window in the mode. show (); or its reloading printpreviewdialog. show (iwin32window); the method is opened in normal non-mode.

The file dialog box abstraction base class filedialog is inherited from the commondialog abstract class. Therefore, all the dialogs inherited from this class can be inherited through commondialog. showdialog (); or its heavy commondialog. showdialog (iwin32window); Method to open the form in mode.

Reposted from 'China IT lab'

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.