Premiere Issue

SpeedButton tricks

by Tim Gooch

With its visual-design toolsand form-based orientation, C++Builder makes it easy for you to create highly interactive applications. The Visual Control Library (VCL), a critical element of C++Builder, lets you maintain a great degree of control over the look and feel of the resulting application. SpeedButton components are a perfect example of this phenomenon.

Commands that really click

If you create a form and place several buttons on it, they will behave in a way similar to the alphanumeric keys on your keyboard. When you click on a button it will stay down, but only as long as you continue to depress the mouse button.

The most common use for SpeedButton components configured this way is to place them on a toolbar on which each button executes a command. C++Builder uses SpeedButton components on the left side of the toolbar (the file, view, and debugging commands), as shown in Figure A.

Figure A: C++Builder uses SpeedButton components to implement the command buttons on the toolbar.
Figure A

Click 'em when they're up...

In some situations, you'll want to create a set of buttons that implement a mutually exclusive relationship. For example, on the right side of the C++Builder toolbar, you'll find the arrow icon and a set of icons comprising the Component Palette, shown in Figure B. If you select a component, the button for that component stays down until you place the component or select a new component (or click the arrow button).

Figure B: The right side of the toolbar displays the Component Palette, which consists of several radio speedbuttons.
Figure B

In effect, these buttons, which we'll call radio speedbuttons, act in the same way as a set of radio buttons: One button must be down initially, but you can't choose more than one at a time. To implement radio speedbuttons with SpeedButton components, place several of them on a form, then change each one's GroupIndex property to the same value.

Unlike standard radio buttons, you can create more than one set of radio speedbuttons within a given form without having them interact. To do so, use a different GroupIndex value for each set of radio speedbuttons.

Click 'em when they're down

For most situations, the previous two styles will be sufficient. However, what if you want to prompt the user for one of a group of items, or none at all?

For example, how would you design a dialog box that asks users which of a set of optional, supplementary dictionaries to use? In other words, you want them to specify one of the supplementary dictionaries to use, but only one--or, if they prefer, no dictionary at all.

Using a set of SpeedButton components, you can define this type of button set, which we'll call exclusive option buttons. To do so, first set the AllowAllUp property to True for each button, then specify the same GroupIndex property for the set.

State of the button

The final variation we'll cover is the state button. This button behaves just like [Caps Lock] on your keyboard: Press it once to turn it on; press it again to turn it off.

To create a state button, first set its AllowAllUp property to True, then set its GroupIndex property to a unique value. As you create additional state buttons, you'll need to use a new value for the GroupIndex property each time to keep them from becoming exclusive option buttons.