[Zz] C # form dialog box mode

Source: Internet
Author: User

Form



This chapter describes how to create a simple windows application.Program. This example contains a class derived from system. Windows. Forms. form. According to the. NET Framework instructions, "forms are the representation of windows in applications. "If you have a visual basic background, you will be familiar with the term" form ". If you are a C ++ programmer using MFC, you may get used to calling a form a window, dialog box, or frame. In any case, forms are the basic way to interact with users. We have introduced some common attributes, methods, and events in the control class, and the Form class is derived from the control class. Therefore, these attributes, methods, and events exist in the Form class. The Form class also adds a large number of functions based on the control class. This section describes these functions.

Form class

Windows client applications can contain one or hundreds of forms. They can be applications based on SDI (Single Document Interface) or MDI (Multi Document Interface, multiple document interface. However, the system. Windows. Forms. Form class is the core of Windows client applications. The Form class is derived from containercontrol, and containercontrol is also derived from scrollablecontrol, while scrollablecontrol is derived from the control class. Therefore, it can be assumed that the form can be the container of other controls. When the contained controls cannot be displayed in the customer area, they can be displayed in a scroll manner, the form can have the same attributes, methods, and events as other controls. Therefore, the Form class is quite complex. This section describes these features.

1. Form instantiation and release

It is important to understand the process of creating a form. The work to be done depends on the initialization.Code. For instances, events occur in the following order:

● Constructor

● Load

● Activated

● Closing

● Closed

● Deactivate

The first three events occur during initialization. You can determine the event to be associated Based on the initialization type. The constructor of this class is executed during Object Instantiation. After the object is instantiated, the load event occurs before the form is visible. It differs from constructor in form visibility. When a load event is triggered, the form already exists but is invisible. During the execution of the constructor, the form does not exist and is in the instantiation process. Activated events occur when the form is visible and in the current state.

In one case, the event execution sequence is slightly changed. If the visible attribute is set to true during the execution of the form constructor, or the show method is called (it sets the visible attribute to true), the load event is immediately triggered. This also makes the form visible and in the current state, so it also triggers the activate event. If there is code after setting the visible attribute, execute the code. The execution sequence of the startup event is as follows:

● Constructor, run until visible = true

● Load

● Activate

● Constructor, run the code after visible = true

This will produce some unexpected results. It is best to perform as many initialization as possible in the constructor.

When the form is closed, the closing event can be canceled. It uses canceleventargs as a parameter. If the cancel attribute is set to true, the event is canceled and the form is still open. The closing event occurs when the form is closed, while the closed event occurs after the form is closed. Both events allow necessary cleanup. Note that the Deactivate event occurs after the form is closed, which is another source that may cause hard-to-find errors. Make sure that you do not perform operations in the Deactivate event to prevent the form from being normally garbage collected. For example, setting a reference to another object will make the form unreleased.

If you call the application. Exit () method and one or more forms are currently open, closing and closed events are not triggered. If you open the file or database connection to be cleaned up, this is a problem that needs to be considered. At this time, you should call the dispose method, so another better way is to put most of the cleanup code in the dispose method.

Some attributes related to form startup include startposition, showintaskbar, and topmost. Startposition can be a value in formstartposition enumeration:

● Centerparent: the form is located in the customer region center of the parent form.

● Centerscreen: the form is located in the center of the current screen.

● Manual: the location of the form is determined based on the location attribute value.

● Windowsdefabobounds: the form is located in the default Windows location and uses the default size.

● Windowsdefaultlocation: the window is located in the default Windows location, but its size is determined by the size attribute.

The showintaskbar attribute determines whether the form should be visible on the taskbar. If the form is a child form and you only want the parent form to be displayed on the taskbar, this attribute is used. The topmost attribute specifies that the form is located at the top when the application is started, even if the form does not obtain the focus immediately, it is located at the top.

To allow the user to interact with the application, the user must be able to see the form. You can achieve this by using the show and showdialog methods. The show method only makes the form visible to the user. The following code demonstrates how to create a form and display it to the user. Assume that the form to be displayed is called myformclass:

Myformclass myform = new myformclass ();
Myform. Show ();

This is very simple. However, one drawback of myform is that it does not send any notifications to the calling code, indicating that myform has been processed and exited. Sometimes this is not important. The show method works well. To provide a notification, using the showdialog method is a good choice.

After the show method is called, the code after the show method is executed immediately.Before callingShowdialogAfter the method is called, the call code is paused.ShowdialogMethod. Not only is the call code suspended, but the form can return a dialogresult value. The dialogresult enumeration is a group of identifiers that describe why the dialog box is closed, including OK, cancel, yes, no, and several other identifiers. In order for the form to return a dialogresult value, you must set the dialogresult attribute of the form or set the dialogresult attribute on a button of the form.

For example, assume that part of the application requires the customer's phone number. The form contains a text box for entering the phone number, and two buttons "OK" and "cancel. If you set the dialogresult attribute of the OK button to dialogresult. OK. Set the dialogresult attribute of the cancel button to dialogresult. cancel, the form is invisible when one of the buttons is selected, and the corresponding dialogresult value is returned for the form that calls it. Now note that the form is not released, but set the visible attribute to false. This is because the value must still be obtained from the form. In this example, we need a telephone number. Create an attribute for the phone number on the form so that the parent form can obtain the value and call the close method on the form. The following is the code of the subform:

Namespace formssample. dialogsample
{
Partial class phone: Form
{
Public phone ()
{
Initializecomponent ();

Btnok. dialogresult = dialogresult. OK;

Btncancel. dialogresult = dialogresult. Cancel;

}



Public String phonenumber

{

Get {return textbox1.text ;}

Set {textbox1.text = value ;}

}

}

}

Note that the code for processing the event clicked by the button is not included. Because the dialogresult attribute of each button is set, the form disappears after you click OK or cancel. The only property added is phonenumber. The following code shows how to call the phone dialog box in the parent form:

 
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 ();

This looks very simple. Create a new phone object frm. When you call the frm. showdialog () method, the code in this method stops execution and waits for the phone form to return. Then, check the dialogresult attribute of the phone form. Because the form has not been released and is invisible, you can still access public properties. One of the public properties is phonenumber. Once the required data is obtained, you can call the close method of the form.

Related Article

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.