Windows showdialog Method

Source: Internet
Author: User
Http://msdn2.microsoft.com/zh-cn/library/system.windows.forms.form.dialogresult (vs.80). aspx

 

The result of the dialog box of the form is the value returned from the form when the form is displayed as a mode dialog box. If the form is displayed as a dialog box, useDialogresultSetting this attribute in enumeration sets the result value of the dialog box for the form, hides the mode dialog box, and returns the control to the called form. This attribute is usually set by the dialogresult attribute of the button control on the form. When you clickButtonControl, assignedButtonOfDialogresultThe attribute value is assigned toDialogresultAttribute.

When the form is displayed as a mode dialog box, click the close button (the button with X in the upper right corner of the form) to hide the form andDialogresultSet propertyDialogresult. Cancel. When you click the "close" button or setDialogresultThe close method is not automatically called when the attribute value is set. Instead of creating a new instance in the dialog box. Because of this behavior, the dispose method of the form must be called when the application no longer needs the form.

You can use this attribute to determine how the dialog box is closed, so as to correctly handle the operations performed in the dialog box.

Public void createmyform ()
{
// Create a new instance of the form.
Form form1 = new form ();
// Create two buttons to use as the accept and cancel buttons.
Button button1 = new button ();
Button button2 = new button ();

// Set the text of button1 to "OK ".
Button1.text = "OK ";
// Set the position of the button on the form.
Button1.location = new point (10, 10 );
// Set the text of button2 to "cancel ".
Button2.text = "cancel ";
// Set the position of the button Based on the location of button1.
Button2.location
= New point (button1.left, button1.height + button1.top + 10 );
// Make button1's dialog result OK.
Button1.dialogresult = dialogresult. OK;
// Make button2's dialog result cancel.
Button2.dialogresult = dialogresult. Cancel;
// Set the caption bar Text of the form.
Form1.text = "My dialog box ";
 
// Define the border style of the form to a dialog box.
Form1.formborderstyle = formborderstyle. fixeddialog;
// Set the accept button of the form to button1.
Form1.acceptbutton = button1;
// Set the cancel button of the form to button2.
Form1.cancelbutton = button2;
// Set the start position of the form to the center of the screen.
Form1.startposition = formstartposition. centerscreen;

// Add button1 to the form.
Form1.controls. Add (button1 );
// Add button2 to the form.
Form1.controls. Add (button2 );

// Display the form as a modal dialog box.
Form1.showdialog ();
 
// Determine if the OK button was clicked on the dialog box.
If (form1.dialogresult = dialogresult. OK)
{
// Display a message box indicating that the OK button was clicked.
MessageBox. Show ("the OK button on the form was clicked .");
// Optional: Call the dispose method when you are finished with the dialog box.
Form1.dispose ();
}
Else
{
// Display a message box indicating that the cancel button was clicked.
MessageBox. Show ("the cancel button on the form was clicked .");
// Optional: Call the dispose method when you are finished with the dialog box.
Form1.dispose ();
}
}

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.