The Spin Button

 

Introduction

A spin button, also referred to as an up down control, is an object equipped with two arrows. Clicking one of the arrows changes the value held by the control; that is, it increments or decrements it. Although not required, a spin button is usually accompanied by an edit control that displays the value of the spin button. The spin button can only use integral values and it can increment only in terms of integers. This is not an anomaly from the VCL; this behavior is inherent to the control as it is implemented in the operating system.

The Spin Buttons

The Visual Component Library (VCL) ships with three controls as spin buttons. The default that is present on all other programming environments on Microsoft Windows, is the TUpDown control. To use this control, you can place it on a form or dialog box. It has default properties that completely define its behavior and make it functional. In fact, the only thing you would need to do is to add an accompanying label or edit controls that would display the current value of the spin button. After adding the control that would assist the spin button, on the Object Inspector, you should select it as the Associate property of the UpDown control:

That is all you have to do. Isn't the VCL wonderful?

 

Custom Increment

By default, the UpDown is configured to increment or decrement its value by one integer when the user clicks one of the arrows. The VCL allows you to define your own increment value so the it would add or subtract 1 from the current value. This behavior is applied on the Increment property of the UpDown control.

To use your own custom increment, especially for floating numbers, you may have to write your own behavior. To start, you can add an UpDown control to a form. You should also add an accompanying control that would show you to increment or decrement value. This control can be a label or an edit control and this accompanying doesn't need to be associated with the UpDown control:

When the user clicks one of the arrows on the spin button, it fires an OnClick event. You can use this event to define how the value would be incremented or decremented. You can then send the current value to the control that would display it. In the following example, the value is incremented or decremented by 1/100:

//---------------------------------------------------------------------------
void __fastcall TForm1::UpDown2Click(TObject *Sender, TUDBtnType Button)
{
    static double Incr = 0.00;

    if( Button == btNext )
    {
        Incr = Incr + 0.01;
        Edit2->Text = Edit2->Text.sprintf("%.2f", Incr);
    }
    else
    {
        Incr = Incr - 0.01;
        Edit2->Text = Edit2->Text.sprintf("%.2f", Incr);
    }
}
//---------------------------------------------------------------------------

In the same way, you can display anything in the accompanying control of a spin button.

 

 


Copyright © 2003-2015, FunctionX, Inc.