Practical
Learning: Introducing the Spin Edit Control
|
|
- To start a new project, on the main menu, click File -> New -> VCL
Forms Application - C++Builder
- In the Object Inspector, change the properties of the form as
follows:
Caption: Movie Reviewer Name:
frmReviewer Position: poScreenCenter
- To save the project, on the Standard toolbar, click the Save All
button
- Click the New Folder button
- Set the name to MovieReviewer1 and press Enter
twice
- Set the name of the unit to Reviewer and click Save
- Set the name of the project to MovieReviewer and
click Save
- To test the application, press F9 (that will also create a folder
named Debug)
- After viewing the form, close it and return to your programming
environment
- From the resources that accompany these lessons, copy the annie.bmp,
bhc.bmp, dave.bmp, disgent.bmp, fatal.bmp, housesitter.bmp, newjack.bmp,
and showgirls.bmp files
- Paste them in the Debug sub-folder of the current project
A spin button is based on the TSpinButton
class that is derived from TWinControl:
To add a spin button to your application, in the Samples
section of the Tool Palette, click the TSpinButton icon
and click the form or container.
The spin button is equipped with a constructor. The
TSpinButton class’s constructor is typically used to
dynamically create a spin button.
Practical
Learning: Creating a Spin Button
|
|
- Design the form as follows:
|
Control |
Caption |
Kind |
Name |
TLabel |
|
Title: |
|
|
TEdit |
|
|
|
edtTitle |
TSpinButton |
|
|
|
sbtPosition |
TLabel |
|
Director: |
|
|
TEdit |
|
|
|
edtDirector |
TLabel |
|
(c) Year: |
|
|
TEdit |
|
|
|
edtCopyrightYear |
TLabel |
|
Rating: |
|
|
TEdit |
|
|
|
edtRating |
TLabel |
|
Length: |
|
|
TEdit |
|
|
|
edtLength |
TImage |
|
|
|
imgMovie |
TBitBtn |
|
|
bkClose |
|
|
- Save all
- Under the Code Editor, click Reviewer.h and change it as follows:
private: // User declarations
UnicodeString Titles[8];
UnicodeString Directors[8];
UnicodeString CopyrightYears[8];
UnicodeString Ratings[8];
UnicodeString Lengths[8];
UnicodeString Pictures[8];
int Position;
void __fastcall ShowStatistics();
public: // User declarations
__fastcall TfrmReviewer(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TfrmReviewer *frmReviewer;
//---------------------------------------------------------------------------
#endif
- Press F12 to display the form
- Double-click an unoccupied area of the form to generate its OnCreate
event
- Change the Reviewer.cpp file as follows:
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Reviewer.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "Spin"
#pragma resource "*.dfm"
TfrmReviewer *frmReviewer;
//---------------------------------------------------------------------------
__fastcall TfrmReviewer::TfrmReviewer(TComponent* Owner)
: TForm(Owner)
{
Titles[0] = L"Distinguished Gentleman (The)";
Directors[0] = L"Jonathan Lynn";
CopyrightYears[0] = L"1992";
Ratings[0] = L"R";
Lengths[0] = L"112 Minutes";
Pictures[0] = L"distgent.bmp";
Titles[1] = L"Fatal Attraction";
Directors[1] = L"Adrian Lyne";
CopyrightYears[1] = L"1987";
Ratings[1] = L"R";
Lengths[1] = L"119 Minutes";
Pictures[1] = L"fatal.bmp";
Titles[2] = L"New Jack City";
Directors[2] = L"Mario Van Peebles";
CopyrightYears[2] = L"1991";
Ratings[2] = L"R";
Lengths[2] = L"97 Minutes";
Pictures[2] = L"newjack.bmp";
Titles[3] = L"Showgirls";
Directors[3] = L"Paul Verhoeven";
CopyrightYears[3] = L"1995";
Ratings[3] = L"NC-17";
Lengths[3] = L"128 Minutes";
Pictures[3] = L"showgirls.bmp";
Titles[4] = L"Annie";
Directors[4] = L"John Huston";
CopyrightYears[4] = L"1982";
Ratings[4] = L"PG";
Lengths[4] = L"126 Minutes";
Pictures[4] = L"annie.bmp";
Titles[5] = L"Dave";
Directors[5] = L"Ivan Reitman";
CopyrightYears[5] = L"1993";
Ratings[5] = L"R";
Lengths[5] = L"110 Minutes";
Pictures[5] = L"Dave.bmp";
Titles[6] = L"Housesitter";
Directors[6] = L"Frank Oz";
CopyrightYears[6] = L"1992";
Ratings[6] = L"PG";
Lengths[6] = L"110 Minutes";
Pictures[6] = L"housesitter.bmp";
Titles[7] = L"Beverly Hills Cop";
Directors[7] = L"Martin Brest";
CopyrightYears[7] = L"1984";
Ratings[7] = L"R";
Lengths[7] = L"105 Minutes";
Pictures[7] = L"bhc.bmp";
}
//---------------------------------------------------------------------------
void __fastcall TfrmReviewer::ShowStatistics()
{
edtTitle->Text = Titles[Position];
edtDirector->Text = Directors[Position];
edtCopyrightYear->Text = CopyrightYears[Position];
edtRating->Text = Ratings[Position];
edtLength->Text = Lengths[Position];
imgMovie->Picture->LoadFromFile(Pictures[Position]);
}
//---------------------------------------------------------------------------
void __fastcall TfrmReviewer::FormCreate(TObject *Sender)
{
Position = 0;
ShowStatistics();
}
//---------------------------------------------------------------------------
- Press F12 to return to the form
- Save all
|
|