![]() |
VCL Controls: Button Groups |
Introduction to Button Groups |
Description |
A button group is a control that holds a series of rectangular buttons. The buttons are grouped as an array or collection where each has a specific position. Each button can display a caption or a bitmap. To use a button, a user can click it. You as the programmer would have to identify the clicked button and take appropriate action. In reality, the VCL provides tremendous flexibility for the implementation of this scenario. |
|
To support the button group, the VCL provides a class named TButtonGroup. The TButtonGroup class is derived from TCustomControl. The TCustomControl class is based on TWinControl:
The TButtonGroup class and its buttons are defined in the ButtonGroup.pas file that also contains the ButtonGroup package. To visually create a button group, in the Additional
section of the Tool Palette, click the TButtonGroup icon
unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, ButtonGroup, Dialogs; type TForm1 = class(TForm) procedure FormCreate(Sender: TObject); private { Private declarations } public { Public declarations } end; var Form1: TForm1; implementation {$R *.dfm} procedure TForm1.FormCreate(Sender: TObject); var bgpSelection : TButtonGroup; begin bgpSelection := TButtonGroup.Create(Form1); bgpSelection.Parent := Form1; end; end.
|
By default, the button group appears with a 3-D border. This aspect is controlled by the BorderStyle property that is based on the TFormBorderStyle enumeration. property BorderStyle: TBorderStyle read FBorderStyle write SetBorderStyle; If you don't want the button group to show a border, set this property to bsNone instead of its default bsSingle value.
A button group is primarily a normal control. It gets its fundamental characteristics from the TWinControl and the TControl classes. This makes it possible to have a name, a location, a size and visibility. You can set these aspects during design or programmatically. Here is an example: procedure TForm1.FormCreate(Sender: TObject);
var bgpSelection : TButtonGroup;
begin
bgpSelection := TButtonGroup.Create(Self);
bgpSelection.Parent := Self;
bgpSelection.Left := 8;
bgpSelection.Top := 40;
end;
By definition, a button group holds a series of buttons. The buttons are created as a collection. To visually create the buttons:
Any of these actions would open the Editing ButtonGroup Items window:
To support the collection of buttons, the TButtonGroup class is equipped with a property named Items. This property is of type TGrpButtonItems that is a member of the ButtonGroup namespace: property Items: TGrpButtonItems read FButtonItems write SeTGrpButtonItems; The TGrpButtonItems class is based on the TCollection class (a member of the Classes.pas file). Each button of the TGrpButtonItems collection is of type TGrpButtonItem. The TGrpButtonItem class implements the TBaseButtonItem class. TBaseButtonItem is derived from the TBaseItem class and both classes are defined in CategoryButtons.pas.
Some characteristics are shared by all buttons of a button group. Some of the aspects are managed by the ButtonOptions property, which is defined as a set: property ButtonOptions: TGrpButtonOptions read FButtonOptions write SetGrpButtonOptions;
To visually create a button:
To programmatically create a button, you have many options. You can call the Add() method that the TGrpButtonItems class inherits from TCollection. The TGrpButtonItems.Add() method returns a TGrpButtonItem object. When calling the Add() method, you should get its return value. Here is an example: procedure TForm1.FormCreate(Sender: TObject);
var
bgpSelection : TButtonGroup;
gbi : TGrpButtonItem;
begin
bgpSelection := TButtonGroup.Create(Self);
bgpSelection.Parent := Self;
bgpSelection.Left := 8;
bgpSelection.Top := 40;
gbi := bgpSelection.Items.Add
end;
In the same way, you can call the Add() method as many times as necessary to create the buttons. You can use the returned value of the TGrpButtonItems.Add() method as you see fit. For example, you can specify its characteristics using the properties of the TGrpButtonItem class.
When you call the Add() method, the buttons are created in the order you access this method. When the application displays, the buttons will show in that order. If you want, you can allow the user to change the sequence of buttons. To do this, set the gboAllowReorder member of the ButtonOptions value to True. When this is done, the change the position of a button, the user can drag it from one position to another.
One of the characteristics of a button is to show a caption. The TGrpButtonItem class provides this through its Caption property. First, if you want the buttons to show captions, set the gboShowCaptions member of the ButtonOptions value to True. Once this is done, assign the desired string to a button. Here is an example: procedure TForm1.FormCreate(Sender: TObject);
var
bgpSelection : TButtonGroup;
gbi : TGrpButtonItem;
begin
bgpSelection := TButtonGroup.Create(Self);
bgpSelection.Parent := Self;
bgpSelection.Left := 8;
bgpSelection.Top := 40;
gbi := bgpSelection.Items.Add;
gbi.Caption := 'Save'
end;
You don't have to specify a caption for each button. Still, if a button doesn't have a caption, you should use another means to let the user know what the button is used for.
All the buttons of a button group must have the same dimension. For this reason, their size is set by the parent control. You have many options. If you want all buttons to use the total width of the button group, set the gboFullSize member of the ButtonOptions value to True. If this is done, when you create a new button, it spans the whole width of the control. Otherwise, you can specify your own width for all buttons. To start, set the gboFullSize member of the ButtonOptions value to False. To let you specify the common width of all buttons, the TButtonGroup class is equipped with a property named ButtonWidth: property ButtonWidth: Integer read FButtonWidth write SetButtonWidth; The value you assign to this property will be used by all controls. This property has nothing to do with the height. If you want to set the common height of the buttons, the TButtonGroup class provides the ButtonHeight property: property ButtonHeight: Integer read FButtonHeight write SetButtonHeight; Because the width and the height are set independently, the buttons can have the same height with different widths, the same width with different heights, or the same size (width and height).
The items of a group button are normal Windows buttons. To use one of them, the user can click it. This causes the control to fire an OnClick event. From there, you as the programmer can do what you want.
Instead of captions, you can display pictures on buttons. To make this possible, the TButtonGroup class provides the Images property: property Images: TCustomImageList read FImages write SetImages; To start, create an image list and add images to it. If you use the default size of the buttons, you can visually create the list using the TImageList control. If you plan to use pictures whose sizes are not 16x16 pixels, you should programmatically create the list of items and manually specify the sizes of the images using the Width and the Height properties of the image list. After creating the image list, to assign the picture of a button, access its ImageIndex and assign the desired integral value. |
|
|||||||||||||||||||||||||||||||
|
|
||
Home | Copyright © 2010-2016, FunctionX | |
|