Windows Forms programming in C # Reading Notes-Chapter 3 dialogs

Source: Internet
Author: User

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;

}

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.