Win32 Controls: The Up-Down Button |
|
Introduction to the Up-Down Control |
Description |
A spin button, also called up-down, is a Windows control equipped with two opposite arrow buttons or . The user clicks one of the arrow buttons at one time to increase or decrease the current value of the control. The value held by the control is also called its position. |
The values of a spin button range from a minimum to a maximum. When the up or right arrow is clicked, the value of the control increases. If the user clicks and holds the mouse on the up or right pointing arrow button, the value of the control keeps increasing until it reaches its maximum and then stops. The opposite behavior applies when the user clicks or holds the mouse on the down or left-pointing arrow button. Because a spin button is only equipped with arrows, it does not inherently show its value. Therefore, this control is usually accompanied by another, text-based, control, usually an edit box, that indicates its position .. |
To suport up-down controls, the VCL provides a class named TUpDown. The TUpDown class implements the TCustomUpDown class that is derived from TWinControl:
To visually create an up-down button, in the Win32 section of the Tool Palette, click the TUpDown icon and click the form or container. To programmatically get an up-down control, create a pointer to TUpDown. Call its constructor to initialize the control and make sure you specify its parent. Here is an example: //---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
TUpDown *Counter = new TUpDown(this);
Counter->Parent = this;
}
//---------------------------------------------------------------------------
Although not required, an up-down control should be accompanied by another object that would display the current value of the up-down control. The most commonly used accompanying object is an edit control but you can use any control you judge appropriate.
After adding the up-down control on a container such as a form, use the Object Inspector to control its properties. You can also specify the control's characteristics programmatically by assigning the desired values to its properties. At the least, you can use some of the properties that the TUpDown class inherits from TWinControl or from TControl. Here are examples: //---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
TUpDown *Counter = new TUpDown(this);
Counter->Parent = this;
Counter->Left = Displayer->Left + Displayer->Width;
Counter->Top = Displayer->Top;
}
//---------------------------------------------------------------------------
Although its name suggests an up and a down directions, an up-down control can be made horizontal:
The direction is controlled by a property named Orientation, which is based on the TUDOrientation enumeration: __property Comctrls::TUDOrientation Orientation = { read=FOrientation, write=SetOrientation }; The TUDOrienation enumeration has two members: enum TUDOrientation{ udHorizontal, udVertical }; The default appearance of an up-down button is to show a up-pointing button and a down-pointing arrow. In this case, the value of the Orientation property is udVertical. You can visually change it in the Orientation field of the Object Inspector or programmatically by assigning the desired value to this property.
As mentioned already, an up-down control does not visually display its value. Therefore, you can add a text-based or other control to it. This accompanying object is specified using the Associate property: __property Controls::TWinControl * Associate = {read=FAssociate,write=SetAssociate}; The associated control would display the current value of the up-down control. To associate a control to the up-down control, first create or add the desired control to your application. Then, at design time on the Object Inspector, you can click the Associate field to display its combo box. Click the arrow and select the desired control:
You can also associate a control programmatically using code such as this: //---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
TUpDown *Counter = new TUpDown(this);
Counter->Parent = this;
TEdit *Displayer = new TEdit(this);
Displayer->Parent = this;
Displayer->Left = 16;
Displayer->Top = 16;
Displayer->Width = 32;
Counter->Left = Displayer->Left + Displayer->Width;
Counter->Top = Displayer->Top;
Counter->Associate = Displayer;
}//---------------------------------------------------------------------------
The UpDown control usually has its associated control on the left side. This is controlled by the AlignButton property. This property is based on the TUDAlignButton enumeration: __property Comctrls::TUDAlignButton AlignButton = {read=FAlignButton,write=SetAlignButton}; The TUDAlignButton enumeration has two members: enum TUDAlignButton{ udLeft, udRight }; You can ask the up-down control to have the accompanying object on its right side by setting the AlignButton property to udRight. At design time, both controls still display as they are designed. If you change the AlignButton value, the control would apply the value only at runtime.
One of the nicest features that make the up-down button easy to use is that the user can change its values by pressing the up and down arrow keys of the keyboard. This ability is by default set to true from the Boolean ArrowKeys property: __property bool ArrowKeys = {read=FArrowKeys,write=SetArrowKeys}; If you want to prevent the user from using the keyboard to increase or decrease the value of the UpDown control, set the ArrowKeys property to false.
|
Probably the most important piece of information you would need from an up-down control is the value it is holding at a particular time. As mentioned already, the up-down control navigates from a minimum to a maximum values. The values of the control are short integer numbers. These numbers range from a minimum controlled by the Min property to a maximum value controlled by the Max property: __property int Min = {read=FMin,write=SetMin}; __property int Max = {read=FMax,write=SetMax}; By default, a freshly added UpDown control on a form has its Min and Max values set to 0 and 100 respectively. You can set the minimum value of the control to –32768 and the maximum to 32767. These values are set using the Min and Max fields of the Object Inspector. You can change them programmatically as follows: //---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
TUpDown *Counter = new TUpDown(this);
Counter->Parent = this;
TEdit *Displayer = new TEdit(this);
Displayer->Parent = this;
Displayer->Left = 16;
Displayer->Top = 16;
Displayer->Width = 32;
Counter->Left = Displayer->Left + Displayer->Width;
Counter->Top = Displayer->Top;
Counter->Min = 12;
Counter->Max = 248;
Counter->Associate = Displayer;
}
//---------------------------------------------------------------------------
If you use numbers in the thousands, the control that accompanies the up-down (such as the edit control) will display the values using the comma to separate the thousands. This is because the up-down control is configured, by default, to separate the thousands. To support this, the TCustomUpDown class provides the Thousands Boolean property: __property bool Thousands = {read=FThousands,write=SetThousands}; If you do not want this feature, change the value of the Thousands property from true to false.
When using the UpDown button, the user can click one of the arrows of the control to increase or decrease the value. By default, the value increases or decreases by 1. If you want the value to augment by more than 1, set an integer value using the Increment property: __property int Increment = {read=FIncrement,write=SetIncrement}; To set the Increment value programmatically, you can use code as follows: //---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
TUpDown *Counter = new TUpDown(this);
Counter->Parent = this;
TEdit *Displayer = new TEdit(this);
Displayer->Parent = this;
Displayer->Left = 16;
Displayer->Top = 16;
Displayer->Width = 32;
Counter->Left = Displayer->Left + Displayer->Width;
Counter->Top = Displayer->Top;
Counter->Min = 12;
Counter->Max = 248;
Counter->Increment = 2;
Counter->Associate = Displayer;
}
//---------------------------------------------------------------------------
When an up-down control is accessed, the value it holds can be set by its Position property: __property int Position = {read=GetPosition,write=SetPosition}; You can use this property to specify what value the control would use at startup. It should be an integer between the Min and the Max values. You can also set it programmatically as follows: //---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
TUpDown *Counter = new TUpDown(this);
Counter->Parent = this;
TEdit *Displayer = new TEdit(this);
Displayer->Parent = this;
Displayer->Left = 16;
Displayer->Top = 16;
Displayer->Width = 32;
Counter->Left = Displayer->Left + Displayer->Width;
Counter->Top = Displayer->Top;
Counter->Min = 12;
Counter->Max = 248;
Counter->Increment = 2;
Counter->Position = 36;
Counter->Associate = Displayer;
}
//---------------------------------------------------------------------------
The Position property also allows you to find out the value of the up-down control at any time.
After setting the Increment value, when the user clicks the arrow buttons, the value would increase accordingly. When the maximum value is reached, the control would use the Wrap property: __property bool Wrap = {read=FWrap,write=SetWrap}; To find out what to do:
The main event of the UpDown control occurs when the user clicks one of the arrows. Whether the user clicks the up pointing arrow to increase or the down pointing arrow to decrease the value or position of the control, the OnClick() event is fired. The pointing arrows are represented using the TUDBtnType enumerator that has two values. The up or right pointing arrow is recognized as btNext while the down or left pointing arrow is referred to as btPrev. When the user clicks one of the arrows, you can write code to perform an action depending on the button that was clicked. Using this OnClick() event, you do not have to associate the UpDown control with an edit box to display integers; you can use the event to format the value or configure any other behavior you see fit. For example, instead of displaying an integer, you can display a floating number, a string, anything, that is traditionally not allowed. When the user clicks one of the arrows of the UpDown control, the operating system is notified just before this action occurs. This notification is done through the OnChanging() event. This allows you to perform a last minute configuration before the value or position of the control changes. You can also use this event to deny changing the value of the control. The OnChangingEx() event also fires just before the value of the UpDown control changes. This time, you can set a new value for the control if the change is successful. It is important and professionally convenient to make sure that the user can use the up and down arrow keys of the keyboard to increase or decrease the value of the UpDown control. If the user presses and holds the up arrow key, the UpDown control would be incrementing its value until either the user releases the key or the control reaches its maximum limit. Here is an example of how to track the OnMouseUp mouse event of the UpDown control: //--------------------------------------------------------------------------- void __fastcall TForm1::UpDown1MouseUp(TObject *Sender, TMouseButton Button, TShiftState Shift, int X, int Y) { ShowMessage(L"You stopped spinning at " + AnsiString(edtSpin->Text)); } //---------------------------------------------------------------------------
|
|
|||||||||||||||
|
|
||
Home | Copyright © 2010-2016, FunctionX | |
|