May 4, 2011, 7:35 a.m.
posted by seele
You already have an OK button on the form. Typically, an OK button accepts the user's values and closes the form. Later in the coding section you'll make your OK button do just that. When you have an OK button, it's also a good idea to create a Cancel button, which unloads the form but doesn't save the user's values.
Add a new button to the form by double-clicking the Button item in the toolbox. Set the button's properties as follows:
There's no point in having a button that doesn't do anything, so double-click the button now to access its Click event, and then add the following statement:
Recall from Hour 5, "Building FormsThe Basics," that this statement closes the current form. Right now, the Cancel button does the same thing as the OK button, but you'll change that soon.
Did you Know?
You can programmatically trigger a button's Click event, just as though a user clicked it, by calling the button's PerformClick method.
Accept and Cancel Buttons
When creating dialog boxes, it's common to assign one button as the default button (called the Accept button). If a form has an Accept button, that button's Click event is fired when the user presses Enter, regardless of which control has the focus. This is great for dialog boxes in which the user enters some text and presses Enter to commit the data and close the form.
Follow these steps to designate the OK button as the Accept button:
Generally, when you create an Accept button for a form, you should also create a Cancel button. A Cancel button fires its Click event when the user presses the Esc key (as opposed to the Enter key), regardless of which control has the focus. Generally, you place code in a Cancel button to shut down the form without committing any changes made by the user. Make your Cancel button an official Cancel button by following these steps: