Practical
Learning: Introducing the Spin Button
|
|
- Start a new project with its default form
- Save it in a new folder named CDPublisher1
- Save the unit as Exercise and save the project as CDPublisher
- Change the Caption of the form Compact Disc Publisher
- Change its Name to frmMain and set its BorderStyle to bsDialog
- Design the form by adding the following controls:
|
Control |
Caption/Text |
Name |
Bevel |
|
|
Label |
Number of Items: |
|
Edit |
0 |
edtQuantity |
Label |
Unit Price: |
|
Edit |
20.00 |
edtUnitPrice |
Label |
Total Price: |
|
Edit |
0.00 |
EdtTotal |
Button |
Kind = bkClose |
|
|
- Save all
- On the Component Palette, click the Samples tab
- Click CSpinButton
and flick on right side of the Number of Items edit box on the form
- On the Object Inspector, change its name to spnQuantity
- Display the header file of the form. In the private section of the header file, declare a Value variable of type int. Also, declare a method named
EvaluatePrice() of type void that uses __fastcall:
private:
int Value;
void __fastcall EvaluatePrice(); // User declarations
|
- In the form’s source file, initialize the value to 0 and implement the new method as follows:
//---------------------------------------------------------------------------
__fastcall TfrmMain::TfrmMain(TComponent* Owner)
: TForm(Owner)
{
Value = 0;
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::EvaluatePrice()
{
int Quantity;
double UnitPrice, TotalPrice;
Quantity = edtQuantity->Text.ToInt();
if( Quantity < 20 )
UnitPrice = 20;
else if( Quantity < 50 )
UnitPrice = 15;
else if( Quantity < 100 )
UnitPrice = 12;
else if( Quantity < 500 )
UnitPrice = 8;
else
UnitPrice = 5;
TotalPrice = Quantity * UnitPrice;
edtUnitPrice->Text = edtUnitPrice->Text.FormatFloat("#,##0.00", UnitPrice);
edtTotal->Text = edtTotal->Text.FormatFloat("#,##0.00", TotalPrice);
}
//---------------------------------------------------------------------------
|
- On the form, click the SpinButton control to select it. On the Object Inspector, click the Events tab
- Double-click the empty field on the right side of OnUpClick and implement the event as follows:
//---------------------------------------------------------------------------
void __fastcall TfrmMain::spnQuantityUpClick(TObject *Sender)
{
edtQuantity->Text = Value++;
EvaluatePrice();
}
//---------------------------------------------------------------------------
|
- On the Object Inspector, double-click the field of the OnDownClick event and implement it as follows:
//---------------------------------------------------------------------------
void __fastcall TfrmMain::spnQuantityDownClick(TObject *Sender)
{
if( Value > 0 )
edtQuantity->Text = Value--;
else
edtQuantity->Text = 0;
EvaluatePrice();
}
//---------------------------------------------------------------------------
|
- To test the form, on the main menu, click Run -> Run
- After experimenting with the spin button, close the form
|
|