Command Controls

 

Command Buttons

 

Overview

A Button is a Windows control used to initiate an action. From the user’s standpoint, a button is useful when clicked, in which case the user positions the mouse on it and presses one of the mouse’s buttons. In some programming environments, the classic button is called a Command Button. There are other controls that can serve as click controls and initiate the same behavior as if a button were clicked. Such controls include a label or a panel.

From the programmer’s standpoint, a button needs a host or container. The container could be a form, a toolbar, etc.

The most popular button used in Windows applications is a rectangular control that displays a word or a short sentence that directs the user to access, dismiss, initiate an action or a suite of actions. In .Net applications, this control is implemented using the Button control from the Toolbox. Therefore, to add a button to a container, click the Button control and click on the container, which is usually a form. Once you have added the control to your application, you can set its properties using the Properties window.

Properties of a Button

For a user, the only things important about a button are the message it displays and the action it performs. Therefore, the default property of a button is the Text: this is the word or group of words that display(s) on top of the control, showing the message that would direct the user as to what the button is used for.

The most popular strings that buttons display are OK and Cancel. The OK caption is set for a form or a dialog box that informs the user of an error, an intermediary situation, or an acknowledgement of an action that was performed on the dialog that hosts the button. The Cancel caption is useful on a button whose main parent (the form) would ask a question or request a follow-up action from the user. Whenever a dialog box allows the user to dismiss it without continuing the action, you should provide a button with a Cancel caption.

After adding a button to a form (by design or with code), you can change its caption with code by calling the Text property. For example, you can change the caption of a button as follows:

Button1->Text = "Let Me Go!";

A button is referred to as default if pressing Enter while using its parent dialog box would cause the OK button to perform its action. A default button has a thick border. If the user presses Enter any time, the action associated with the button would be executed. This is usually set on a dialog box with a button whose caption displays OK. In the next lesson, when reviewing dialog boxes, we will see how to set a button as default.

The text of the button can be aligned using the TextAlign property.

When the user clicks a button to close a dialog box, you must already know what button was clicked. The .Net environment provides a mechanism to help identify such a button. This is done through the DialogResult property. This property is based on the DialogResult enumerator. To set the desired value, after adding a button to a dialog box and while the button is selected, in the Properties window, click the DialogResult field and select the desired value. The possible values of this property are: None, OK, Cancel, Abort, Retry, Ignore, Yes, and No.

A bitmap button is traditionally a command button that displays a bitmap and possibly a caption on top. The main difference between a bitmap button and a command button is that, by design, the former displays a bitmap. If you want a button to display a bitmap, you should first create or have a bitmap. Then, in the Properties window, use the Image field to select a bitmap or an icon. After specifying the image for the button, use the ImageAlign property to specify the alignment of the image with regards to the Text of the button. Alternatively, you can first create an image list and add some pictures to it. Then, using the ImageList property, assign it to the button. Use the ImageIndex property to specify what picture would be displayed on the button.

A regular button displays with raised borders as originally set by the operating system. To give your button a fancy look and behavior, you can use the FlatStyle property. The FlatStyle property is based on an enumerator of the same name. It provides 4 values that are:

  • Flat: The button appears flat. When the mouse is over it, it becomes highlighted

  • Popup: The button appears flat. When the mouse is over it, the borders of the button are raised (if you have used WordPerfect, you may have seen this effect)

  • Standard: The buttons appears and behave like all regular buttons you have seen

  • System: The appearance of the button depends on the operating system using it

 

Previous Copyright © 2003-2015, FunctionX, Inc. Next