Home

Win32 Controls: The Spin Edit Control

 

Introduction to the Spin Edit Control

 

Description

A spin edit control combines an edit box and an up-down object. This makes it easy to create and use it because all the necessary functionality is already built in the control. There is hardly any configuration to do. The spin edit control is a sample control created using the VCL and provided to you as an example of a custom control.

 

The application we are about to develop is for a CD and DVD publishing small business. This company manufactures compact discs for self-promoting musicians and small business that want to sell their own CDs. When taking an order of a new CD, the company charges:

  • $5/CD or DVD if the customer is ordering less than 20 units
  • $3.5/CD or DVD if the customer is ordering up to 50 units
  • $2.25/CD or DVD if the customer is ordering up to 100 units
  • $1.85/CD or DVD if the customer is ordering up to 500 units
  • $0.95/CD or DVD for any order over 500 units

 

Practical LearningPractical Learning: Introducing the Spin Edit Control

  1. To start a new project, on the main menu, click File -> New ->  VCL Forms Application - C++Builder
  2. To save the project, on the Standard toolbar, click the Save All button Save All
  3. Click the New Folder button
  4. Set the name to OpticalDiscPublisher1 and press Enter twice
  5. Set the name of the unit to Publisher and click Save
  6. Set the name of the project to OpticalDiscPublisher and click Save
  7. Change the Caption of the form CD/DVD Publisher
  8. Change its Name to frmPublisher
  9. Save all

Creating a Spin Edit Control

The spin edit control is created from the TSpinEdit class. This class is derived from TCustomEdit.

To add a spin edit to your application, from the Samples section of the Tool Palette, click the TSpinEdit button TSpinEdit and click the desired location on the form or container.

Like all VCL controls, the spin edit object has a constructor and a destructor, based on the TSpinEdit class. The constructor is typically used to dynamically create the control at runtime. To do this, declare a pointer to a TSpinEdit class. You must specify the owner and the parent of the control. You can create such a control in an event or a function. Here is an example:

//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
	TSpinEdit* Staple = new TSpinEdit(this);
	Staple->Parent = this;
}
//---------------------------------------------------------------------------

You must include the spin.hpp header file to the form or unit that would be using the spin edit control.

Practical LearningPractical Learning: Creating a Spin Edit Control

  1. Design the form by adding the following controls:
     
    CD and DVD Publisher
    Control Alignment Caption Kind Name Text
    TBevel TBevel          
    TLabel TLabel   Number of Items:      
    TSpinEdit TSpinEdit       sedQuantity  
    TLabel TLabel   Unit Price:      
    TEdit TEdit taRightJustify     edtUnitPrice 0.00
    TLabel TLabel   Total Price:      
    TEdit TEdit taRightJustify     edtEdtTotal  
    TBitBtn TBitBtn     bkClose    
  2. Save all

Characteristics of the SpinEdit Control

 

The Minimum and Maximum Values

The spin edit control is used to display integer values in the edit box section of the control. The displaying value is controlled by the MinValue and the MaxValue properties that can visually be set in the Object Inspector. You can also specify their values programmatically as follows:

//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
	SpinEdit1->MinValue = -12;
	SpinEdit1->MaxValue = 228;
}
//---------------------------------------------------------------------------

Incrementing the Value

By default, the values of a spin edit control increase by 1 and decrease by –1. If that value is not appropriate for your application, you can change it using the Increment property in the Object inspector.

When a spin edit control appears at startup, the control displays the value 0 even if its MinValue is set to a value other than 0. To set the default position of the spin edit control, use the Value property. You can control it at design time or at runtime. If you set the Value property to a value that is less than the MinValue, the value would be automatically set to the minimum value. If you try to set it to a value greater than the MaxValue in the Object Inspector, the Value would be reset to the MaxValue.

Practical LearningPractical Learning: Using the Spin Button

  1. On the form, double-click the spin edit control
  2. Implement the event as follows:
    //---------------------------------------------------------------------------
    void __fastcall TfrmPublisher::sedQuantityChange(TObject *Sender)
    {
    	int Quantity;
    	double UnitPrice, TotalPrice;
    
    	Quantity = sedQuantity->Text.ToInt();
    
    	if( Quantity < 20 )
    		UnitPrice = 5.00;
    	else if( Quantity < 50 )
    		UnitPrice = 3.50;
    	else if( Quantity < 100 )
    		UnitPrice = 2.25;
    	else if( Quantity < 500 )
    		UnitPrice = 1.85;
    	else
    		UnitPrice = 0.95;
    
    	TotalPrice = Quantity * UnitPrice;
    
    edtUnitPrice->Text = edtUnitPrice->Text.FormatFloat("#,##0.00", UnitPrice);
    edtTotalPrice->Text = edtTotalPrice->Text.FormatFloat("#,##0.00", TotalPrice);
    }
    //---------------------------------------------------------------------------
  3. Save all
  4. To test the form, on the main menu, click Run -> Run

    CD and DVD Publisher
     
    CD and DVD Publisher
     
    CD and DVD Publisher
     
    CD and DVD Publisher
     
    CD and DVD Publisher
  5. After experimenting with the spin button, close the form and return to your programming environment
 
 
 
 

Home Copyright © 2010-2016, FunctionX