button is one of the most common and simplest components in the Windows application interface. In addition to the button, Delphi also provides a bitmap button (BITBTN). The difference between the buttons is that the bitmap can be displayed on the button. Let's use a simple example to introduce these two kinds of buttons.
Place two normal buttons and two bitmap buttons on the form Form1 and one edit box edit, then add a form FORM2, place a label and two bitmap buttons on it. The program interface is shown in the following illustration:
Set the properties of each button according to the following table:
|
Form1 |
Form2 |
Property |
Button1 |
Button2 |
BitBtn1 |
BitBtin2 |
BitBtn1 |
BitBtn2 |
Caption |
Button1 |
Button2 |
dialog box |
&close |
Ok |
Cancel |
Kind |
|
|
Bkcustom |
Bkclose |
Bkok |
Bkcancel |
Cancel |
False |
True |
False |
False |
False |
False |
Default |
True |
False |
False |
False |
False |
False |
Modalresult |
MrNone |
MrNone |
MrNone |
MrNone |
Mrok |
Mrcancel |
These are the buttons commonly used in some of the properties, the following simple introduction:
Cancel: |
True to perform the OnClick event of the button whenever the user presses the ESC key. If you have more than one such button on the same form, the OnClick event of the button with the smallest TabOrder value is executed. |
Default: |
This property is similar to the Cancel property. When the user presses the ENTER key, the button's onclick event is executed. With one exception, if the control that currently has the input focus is another button, only the OnClick event of the current button is executed when the ENTER key is pressed. |
Kind: |
This property is unique to the bitmap button, choosing an option for the Kind property, which determines the type of bitmap button, each of which has its own unique function. Bitmap buttons are often used in various dialog boxes or modal windows, depending on the Modalresult property value of the button to determine the operation of the modal window. |
Modalresult: |
Modal value. When we click the Bitmap button, the modal value of the dialog box is set to the same value. It determines how the parent form of the bitmap button is closed. If you click the Mbok button, the dialog box closes, returning the Mrok value. |
After you finish designing the interface, start writing the event handling process. The list of procedures is as follows:
unit Unit1, Interface uses Windows, Messages, Sysutils, Classes, Graphics, Controls, Forms, Dialogs, Stdctrls, Buttons; Type TFORM1 = Class (Tform) Button1:tbutton; Button2:tbutton; BITBTN1:TBITBTN; BITBTN2:TBITBTN; Edit1:tedit; Procedure Button1Click (Sender:tobject); Procedure Button2click (Sender:tobject); Procedure Bitbtn1click (Sender:tobject); Procedure Bitbtn2click (Sender:tobject); Private {Private declarations} public {public declarations} end; var Form1:tform1; implementation uses UNIT2; Call Unit2 's declaration; {$R *. DFM} procedure Tform1.button1click (Sender:tobject); Begin Edit1. Text:= ' Button1 default property is True '; End Procedure Tform1.button2click (Sender:tobject); Begin ShowMessage (' Button2 Cancel property is true '); Displays a message box end; Procedure Tform1.bitbtn1click (Sender:tobject); Begin Form2. ShowModal; Let the Form2 be displayed in a modal window; Procedure Tform1.bitbtn2click (Sender:tobject); Begin end; End.
Because we use two forms in the program, and each form corresponds to one unit in Delphi, there are two units (UNIT1 and UNIT2) in the program. Calls between different units in Delphi are declared at the beginning of the Implementation section (Implementation) of the calling unit:
Implementation
Uses unit2;
When you add Form2, the system automatically adds statements that generate FORM2 in the project file PROJECT.DPR:
Application.createform (TForm2, Form2);
However, when the program runs, it only shows that the Form1,form2 is hidden. To show Form2, we add the following statement to the OnClick event of the BitBtn1 button in the form Form1:
Form2. ShowModal;
Click the BITBTN1 button to display the form as a modal form. A modal form (modalform) means that other forms cannot accept input focus until the form is closed.
Press F9 to compile the program, and then press ESC to eject a message box, as shown in the right image. Because the Button2 Cancel property is set to True, the Button2 onclick event is initiated whenever the ESC key is pressed when the program runs:
Procedure Tform1.button2click (Sender:tobject);
Begin
ShowMessage (' Cancel property of ' Button2 is true '); Display an information box
End
Pressing the ESC key is equivalent to clicking Button2.
Click Edit1, place the input focus in the edit box, and then press the passing key. The onclick event that triggered the Button1 is discovered at this time:
Procedure Tform1.button1click (Sender:tobject);
Begin
Edit1. Text:= ' Button1 default property is True ';
End
Because the default property of the Button1 is set to true, when the passing key is pressed when the input focus is in the edit box, the Button1 onclick event is triggered, as shown in the following figure.
Click the BITBTN1 (titled dialog Box) button to eject the Form2 form. Form2 is a modal dialog box, the dialog box closes automatically when you click the Mbok button or the Mbcancel button. If you do not want the dialog box to close, you can set the bitmap button's Modalresult property to Brnone.
Click the Form1 BitBtn2 button (titled "Close"), and Form1 automatically closes. Form1 is a modeless form, but the BitBtn2 button is the Mbclose button, so it has the ability to close the form.