Introduction to Button Control |
|
|
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. From the
programmer's standpoint, a button needs a host or container. The container
could be a form, a toolbar, etc.
|
The most common 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. The Button control is implemented by the Button
class. The class ancestor of the button of the .NET Framework is called
ButtonBase. Therefore, to programmatically create a button, you can
declare a variable of type Button. Here is an example:
#include <windows.h>
#using <System.dll>
#using <System.Drawing.dll>
#using <System.Windows.Forms.dll>
using namespace System;
using namespace System::Drawing;
using namespace System::Windows::Forms;
public ref class CExercise : public Form
{
private:
Button ^ btnApprove;
public:
CExercise()
{
InitializeComponent();
}
void InitializeComponent()
{
btnApprove = gcnew Button;
btnApprove->Location = Point(32, 20);
Text = L"Button";
Controls->Add(btnApprove);
}
};
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
Application::Run(gcnew CExercise());
return 0;
}
Practical
Learning: Introducing Buttons
|
|
- Start Microsoft Visual C++
- To create a new application, on the main menu, click File -> New
Project...
- In the middle list, click Windows Forms Application
- Set the name to GDCS1
- Click OK
- Design the form as follows:
|
Control |
Name |
Text |
Additional Properties |
GroupBox |
|
|
Order Setup |
|
Label |
|
|
Customer Name: |
|
TextBox |
|
txtCustName |
|
|
Label |
|
|
Order Date: |
|
TextBox |
|
txtOrderDate |
|
|
Label |
|
|
Order Time: |
|
TextBox |
|
txtOrderTime |
|
|
GroupBox |
|
|
Order Processing |
|
Label |
|
|
Item Type |
|
Label |
|
|
Qty |
|
Label |
|
|
Unit Price |
|
Label |
|
|
Sub Total |
|
Label |
|
|
Shirts |
|
TextBox |
|
txtQuantityShirts |
0 |
TextAlign: Right |
TextBox |
|
txtUnitPriceShirts |
0.00 |
TextAlign: Right |
TextBox |
|
txtSubTotalShirts |
0.00 |
TextAlign: Right |
Label |
|
|
Pants |
|
TextBox |
|
txtQuantityPants |
0 |
TextAlign: Right |
TextBox |
|
txtUnitPricePants |
0.00 |
TextAlign: Right |
TextBox |
|
txtSubTotalPants |
0.00 |
TextAlign: Right |
Label |
|
|
Other |
|
TextBox |
|
txtQuantityOther |
0 |
TextAlign: Right |
TextBox |
|
txtUnitPriceOther |
0.00 |
TextAlign: Right |
TextBox |
|
txtSubTotalOther |
0.00 |
TextAlign: Right |
Button |
|
btnCalculate |
|
|
GroupBox |
|
|
Order Summary |
|
Label |
|
|
Order Total: |
|
TextBox |
|
txtOrderTotal |
0.00 |
TextAlign: Right |
Label |
|
|
Tax Rate: |
|
TextBox |
|
txtTaxRate |
5.75 |
TextAlign: Right |
Label |
|
|
% |
|
Label |
|
|
Tax Amount: |
|
TextBox |
|
txtTaxAmount |
0.00 |
TextAlign: Right |
Label |
|
|
Net Price: |
|
TextBox |
|
txtNetPrice |
0.00 |
TextAlign: Right |
Button |
|
btnClose |
|
|
|
The Characteristics of a Button
|
|
For a user, the most important aspects of 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 displays 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 or the
dialog box) 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.
If you create a dialog box with more than one button,
you should designate one of them as the default. A default button is one
that would apply its behavior if the user presses Enter after using it.
Most of the time, a default button has a thicker border. To specify the
button that is the default on a dialog box, set its IsDefault
property to true.
After adding a button to a form (by design or with
code), you can change its caption with code by assigning the desired
string to the Text property. For example, you can change the
caption of a button as follows:
button1->Text = L"Let it Go!";
After specifying the Text of a button, by
default, it's positioned in the middle center of the button:
The position of the text of a button is controlled
through the TextAlign property which is a value of the
ContentAlignment enumerator. The possible values are:
TopLeft |
TopCenter |
TopRight |
|
|
|
MiddleLeft |
MiddleCenter |
MiddleRight |
|
|
|
BottomLeft |
BottomCenter |
BottomRight |
|
|
|
Here is an example:
void InitializeComponent()
{
btnApprove = gcnew Button;
btnApprove->Location = Point(32, 20);
btnApprove->Size = System::Drawing::Size(120, 48);
btnApprove->Text = L"Resume";
btnApprove->TextAlign = ContentAlignment::BottomCenter;
Controls->Add(btnApprove);
Text = L"Button";
Controls->Add(btnApprove);
}
Practical
Learning: Setting the Captions of Buttons
|
|
- Add three buttons inside between the unit prices and the
sub-totals
- Delete their captions
- Complete the design of the form as follows:
|
Control |
Name |
Text |
Button |
|
btnShirts |
|
Button |
|
btnPants |
|
Button |
|
btnOthers |
|
Button |
|
btnCalculate |
Calculate |
Button |
|
btnClose |
Close |
|
Besides, or instead of, a caption, a button can
display a picture on top. If you want a button to display a picture, you
should first create or have a bitmap. Then, in the Properties window, use
the Image field to select a bitmap or an icon. You can also
programmatically assign an Image object to the Image property. Here is an
example:
void InitializeComponent()
{
btnApprove = gcnew Button;
btnApprove->Location = Point(32, 20);
btnApprove->Size = System::Drawing::Size(120, 48);
btnApprove->Text = L"Resume";
btnApprove->TextAlign = ContentAlignment::BottomCenter;
btnApprove->Image = Image::FromFile(L"C:\\Exercises\\PaperClip.ico");
Controls->Add(btnApprove);
}
This would produce:
After assigning a bitmap to the button, by default, it
is positioned in the middle center of the button. If the button is not
equipped with a caption, this position can be ideal. If you are using both
the caption and the picture, you can use the ImageAlign property,
possibly in combination with the TextAlign property, to specify how
the picture would be positioned with regards to the caption. The
ImageAlign property also is based on the ContentAlignment
enumerator. You can do this at design time in the Properties window or
programmatically as follows:
void InitializeComponent()
{
btnApprove = gcnew Button;
btnApprove->Location = Point(32, 20);
btnApprove->Size = System::Drawing::Size(120, 48);
btnApprove->Text = L"Resume";
btnApprove->TextAlign = ContentAlignment::BottomCenter;
btnApprove->Image = Image::FromFile(L"C:\\Exercises\\PaperClip.ico");
btnApprove->ImageAlign = ContentAlignment::TopCenter;
Controls->Add(btnApprove);
}
This would produce:
Instead of using the Image property, 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.