Table lists the Modal Result constants that VCL defines Table Vcl Modal Result Constants

Constant

Value

mrNone

0

mrOk

1

mrCancel

2

mrAbort

3

mrRetry

4

mrlgnore

5

mrYes

6

mrNo

7

mrAll

8

mrNoToAll

9

mrYesToAll

10

NOTE: You don't have to use one of the predefined ModalResult constants for your buttons; you can use any value you like. Let's say, for example, you have a custom dialog box that could be closed by using a variety of buttons. You could assign a different ModalResult value to each button (100, 150, and 200, for example), and you would then know which button closed the dialog box. Any nonzero number is valid, up to the maximum value of an Integer.

The book's code contains a program called ButtnTst that demonstrates the use of ModalResult. The program enables you to execute a form containing several buttons. When you click a button, the ModalResult will be reported on the main form.

The Default Property The Default property is another key property of buttons. Windows has a standard mechanism for dealing with dialog boxes. One of the features of this mechanism is as follows: If a control other than a button has keyboard focus and the user presses the Enter key on the keyboard, the dialog box will behave as if the user had clicked the default button.

The default button is the button that has the BS_DEFPUSHBUTTON style set (usually the OK button). This feature has been the bane of programmers and the curse of data-entry personnel for years. The Default property is used to set a button as the default button for a form. The default value for this property is False. To make a button the default button, set its Default property to True. If you don't specifically set any button's Default property to True, the form will not close when the user presses the Enter key.

NOTE: When the user closes the form by pressing the Enter key, the OnClick handler of the default button (if one exists) will be called before the form closes.

The Cancel Property

The Cancel property works with the Esc key in much the same way as the Default property works with the Enter key. When the user presses the Esc key to close a form, the return value from ShowModal will be the ModalResult value of the button whose Cancel property is set to True. If no button has its Cancel property set to True, mrCancel will be returned if the user uses the Esc key to close the form (mrCancel is equal to 2; see Table 7.7).

NOTE: Closing a form by clicking the system close box or by pressing Alt+F4 will result in mrCancel being returned from ShowModal, as you would expect. Pressing the Esc key, however, will result in a return value of the ModalResult property being set to whatever button has the Cancel property set to True. The OnClick handler for the Cancel button will be called before the form closes. No OnClick handler is called if the user uses the system close box or Alt+F4 to close the form. Be sure to anticipate the different ways users might use (or abuse) your forms.

NOTE: You can have more than one button with a Default property set to True. Likewise, you can have more than one button with the Cancel property set to True. However, when the user presses Enter on the keyboard, the first button in the tab order that has its Default property set to True will be invoked. Similarly, when the user presses the Esc key to close the form, the return value from ShowModal will be the ModalResult value of the first button in the tab order that has its Cancel property set to True.

The Enabled Property Earlier I discussed the Enabled property when I discussed components in general. This property is used often with buttons to enable or disable the button depending on the current state of the program or of a particular form. When a button is disabled (its Enabled property is set to False), its text is grayed out and the button does not function. In the case of buttons with bitmaps on them (BitBtn and SpeedButton), the bitmap will also be grayed out automatically.

Button components have only one method of interest: the Click method, which simulates a mouse click. When you call Click for a button, the OnClick event of the button is executed just as if the user had clicked the button. As for events, typically only the OnClick event is used.

Now let's take a look at the different button components Delphi provides.

Was this article helpful?

0 0
Project Management Made Easy

Project Management Made Easy

What you need to know about… Project Management Made Easy! Project management consists of more than just a large building project and can encompass small projects as well. No matter what the size of your project, you need to have some sort of project management. How you manage your project has everything to do with its outcome.

Get My Free Ebook


Post a comment