Practical
Learning: Selecting an Item
|
|
- On the main menu, click File -> New -> Unit - C++Builder
- To save it, on the Standard toolbar, click the Save All button
- Set the name to PartDescription
- Click Save
- Change the PartDescription.h header file as follows:
//---------------------------------------------------------------------------
#ifndef PartDescriptionH
#define PartDescriptionH
#define <vcl.h>
//---------------------------------------------------------------------------
class TPartDescription
{
public:
TPartDescription(long code = 0,
int year = 1960,
UnicodeString make = L"Unknown",
UnicodeString model = L"Unknown",
UnicodeString type = L"Miscellaneous",
UnicodeString desc = L"Anything",
double uPrice = 0.00);
~TPartDescription();
public:
long PartNumber;
int CarYear;
UnicodeString Make;
UnicodeString Model;
UnicodeString Category;
UnicodeString PartName;
double UnitPrice;
};
//---------------------------------------------------------------------------
#endif
- Access the PartDescription.cpp source file and change it class as
follows:
//---------------------------------------------------------------------------
#pragma hdrstop
#include "PartDescription.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
//---------------------------------------------------------------------------
__fastcall TPartDescription::TPartDescription(long code,
int year,
UnicodeString make,
UnicodeString model,
UnicodeString type,
UnicodeString desc,
double uPrice)
{
PartNumber = code;
CarYear = year;
Make = make;
Model = model;
Category = type;
PartName = desc;
UnitPrice = uPrice;
}
//---------------------------------------------------------------------------
__fastcall TPartDescription::~TPartDescription()
{
}
//---------------------------------------------------------------------------
- On the main menu, click File -> New -> Other...
- In the right list of the New Items dialog box, click Unit and click
OK
- To save the unit, on the Standard toolbar, click the Save All button
- Set the name to PartSelected
- Click Save
- Change the header file as follows:
//---------------------------------------------------------------------------
#ifndef PartSelectedH
#define PartSelectedH
#include <vcl.h>
//---------------------------------------------------------------------------
class TPartSelected
{
public:
__fastcall TPartSelected(long code = 0,
UnicodeString desc = L"Unknown",
double uPrice = 0.00);
__fastcall ~TPartSelected();
long PartNumber;
UnicodeString PartName;
double UnitPrice;
};
//---------------------------------------------------------------------------
#endif
- Access the source file and change it as follows:
//---------------------------------------------------------------------------
#pragma hdrstop
#include "PartSelected.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
__fastcall TPartSelected::TPartSelected(long code,
UnicodeString desc,
double uPrice)
{
PartNumber = code;
PartName = desc;
UnitPrice = uPrice;
}
//---------------------------------------------------------------------------
__fastcall TPartSelected::~TPartSelected()
{
}
//---------------------------------------------------------------------------
- Display the form
- On the main menu, click File -> Use Unit...
- In the list, click one of the items
- Press and hold Shift
- Click the other items
- Release Shift
- Make sure the Header option is selected and click OK
- Under the Code Editor, click Inventory.h to access the header file
- Declare a private TList pointer named
ListOfParts
private: // User declarations
TList * ListOfParts;
public: // User declarations
__fastcall TfrmInventory(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TfrmInventory *frmInventory;
//---------------------------------------------------------------------------
#endif
- Click the Inventory.cpp tab and change the OnLoad event as follows:
//---------------------------------------------------------------------------
void __fastcall TfrmInventory::FormCreate(TObject *Sender)
{
TDateTime Today = Date();
unsigned short Year, Month, Day;
Today.DecodeDate(&Year, &Month, &Day);
for (int i = Year; i >= 1960; i--)
cbxYears->Items->Add(i);
grdAvailableParts->ColWidths[0] = 60;
grdAvailableParts->ColWidths[1] = 260;
grdAvailableParts->ColWidths[2] = 70;
grdAvailableParts->Cells[0][0] = L"Item #";
grdAvailableParts->Cells[1][0] = L"Item Name";
grdAvailableParts->Cells[2][0] = L"Unit Price";
ListOfParts = new TList;
TPartDescription * part = NULL;
part = new TPartDescription(447093, 2002, L"Ford",
L"Escort SE L4 2.0", L"Engine Electrical",
L"Alternator 75amp Remanufactured; w/ 75 Amp",
205.05);
ListOfParts->Add(part);
part = new TPartDescription(203815, 2006, L"Dodge",
L"Caravan SE L4 2.4", L"Cooling System",
L"Radiator Cap", 6.65);
ListOfParts->Add(part);
part = new TPartDescription(293047, 2000, L"Toyota",
L"RAV4 2WD/4-DOOR", L"Cooling System",
L"Thermostat Gasket", 4.95);
ListOfParts->Add(part);
part = new TPartDescription(990468, 2002, L"Honda",
L"Civic 1.7 EX 4DR", L"Exhaust",
L"Bolt & Spring Kit (Manifold outlet, Muffler Inlet)",
85.75);
ListOfParts->Add(part);
part = new TPartDescription(304158, 1996, L"Buick",
L"Regal Custom V6 3.8", L"Fuel Injection",
L"Fuel Injector", 82.75);
ListOfParts->Add(part);
part = new TPartDescription(807245, 2004, L"Acura",
L"MDX 3.5 4WD", L"Driveshaft & Axle",
L"CV Boot Clamp 7 x 750mm; 1 Large + 1 Small Clamp",
1.60);
ListOfParts->Add(part);
part = new TPartDescription(203485, 2001, L"Ford",
L"Taurus LX V6 3.0", L"Fuel Injection",
L"Oxygen Sensor OE Style 4Wire; Front; 2 Required",
52.65);
ListOfParts->Add(part);
part = new TPartDescription(248759, 1999, L"Jeep",
L"Wrangler Sahara", L"Air Intake",
L"Air Filter AirSoft Panel", 7.95);
ListOfParts->Add(part);
part = new TPartDescription(202848, 1998, L"Honda",
L"Accord 2.3 LX 4DR", L"Air Intake",
L"Air Filter", 12.55);
ListOfParts->Add(part);
part = new TPartDescription(932759, 2006, L"Kia",
L"Rio 1.6DOHC16V 4-DR", L"Cooling System",
L"Thermostat", 14.45);
ListOfParts->Add(part);
part = new TPartDescription(304975, 2000, L"Honda",
L"Civic 1.6 EX 4DR", L"Suspension",
L"Ball Joint; Front Lower; 2 per car", 40.55);
ListOfParts->Add(part);
part = new TPartDescription(208450, 2003, L"Chevrolet",
L"Monte Carlo LS V6 3.4", L"Fuel Injection",
L"Oxygen Sensor OE connector; Rear", 65.55);
ListOfParts->Add(part);
part = new TPartDescription(209480, 2002, L"Ford",
L"Focus SE DOHC L4 2.0", L"Steering",
L"Steering Rack Remanufactured", 170.85);
ListOfParts->Add(part);
part = new TPartDescription(203495, 2004, L"Honda",
L"Civic 1.7 EX 4DR", L"Climate Control",
L"A/C Clutch; OE compressor = Sanden", 184.95);
ListOfParts->Add(part);
part = new TPartDescription(203480, 2007, L"Toyota",
L"Corolla", L"Air Intake",
L"Air Filter", 12.65);
ListOfParts->Add(part);
part = new TPartDescription(109379, 2005, L"Volvo",
L"S40 2.5L T5 AWD", L"Fuel Delivery",
L"Fuel Filter; Early Design; Outer Diameter = 55mm",
30.95);
ListOfParts->Add(part);
part = new TPartDescription(935794, 2002, L"Ford",
L"Escape XLS 4WD", L"Brake",
L"Brake Caliper Remanufactured; Front Right",
65.55);
ListOfParts->Add(part);
part = new TPartDescription(203485, 2006, L"BMW",
L"325i", L"Climate Control",
L"AC High Pressure Side Switch",
49.95);
ListOfParts->Add(part);
part = new TPartDescription(204875, 1996, L"Chevrolet",
L"Monte Carlo Z34 V6 3.4", L"Fuel Delivery",
L"Fuel Filter", 8.05);
ListOfParts->Add(part);
part = new TPartDescription(937485, 2007, L"Toyota",
L"Camry V6", L"Air Intake", L"Air Filter", 12.95);
ListOfParts->Add(part);
part = new TPartDescription(294759, 2001, L"Ford",
L"Escape XLT 4WD", L"Air Intake",
L"Air Filter Panel", 7.25);
ListOfParts->Add(part);
part = new TPartDescription(297495, 2003, L"Honda",
L"Civic 1.7 EX 4DR", L"Brake",
L"Brake Caliper Reman; w/ ProAct Pads; Front Right",
82.55);
ListOfParts->Add(part);
part = new TPartDescription(794735, 2006, L"BMW",
L"325i", L"Climate Control",
L"Cabin Air/Pollen Filter; With Activated Carbon",
28.05);
ListOfParts->Add(part);
part = new TPartDescription(937485, 2007, L"Toyota",
L"Corolla", L"Body Electrical",
L"Halogen SilverStar; 12V 65W; inner-high beam",
22.85);
ListOfParts->Add(part);
part = new TPartDescription(492745, 2005, L"Ford",
L"Focus ZX3 L4 2.0", L"Air Intake",
L"Fuel Injection Perf Kit", 342.95);
ListOfParts->Add(part);
part = new TPartDescription(937005, 2004, L"Acura",
L"MDX 3.5 4WD", L"Driveshaft & Axle",
L"CV Boot Clamp 7 x 750mm; For Large End of Boot; inner boot",
1.60);
ListOfParts->Add(part);
part = new TPartDescription(293749, 2004, L"Acura",
L"MDX 3.5 4WD", L"Driveshaft & Axle",
L"Axle Nut 24mm x 1;5; rear ",
2.35);
ListOfParts->Add(part);
part = new TPartDescription(920495, 2006, L"BMW",
L"325i", L"Climate Control",
L"Adjustable Telescoping Mirror", 7.95);
ListOfParts->Add(part);
part = new TPartDescription(204075, 2004, L"Acura",
L"MDX 3.5 4WD", L"Driveshaft & Axle",
L"Wheel Bearing; Rear; 1 per wheel",
70.15);
ListOfParts->Add(part);
part = new TPartDescription(979304, 2000, L"Toyota",
L"RAV4 2WD/4-DOOR", L"Cooling System",
L"Thermostat Housing", 20.95);
ListOfParts->Add(part);
part = new TPartDescription(300456, 2004, L"Acura",
L"MDX 3.5 4WD", L"Driveshaft & Axle",
L"Wheel Bearing; Front; 1 per wheel", 66.65);
ListOfParts->Add(part);
part = new TPartDescription(404860, 2001, L"Ford",
L"Taurus LX V6 3.0", L"Suspension",
L"Shock Absorber GR2; Rear; Wagon only",
39.40);
ListOfParts->Add(part);
part = new TPartDescription(585688, 2007, L"Buick",
L"Lacrosse CXS V6 3.6", L"Brake",
L"Climate Control", 10.65);
ListOfParts->Add(part);
part = new TPartDescription(739759, 2001, L"Ford",
L"Taurus LX V6 3.0", L"Suspension",
L"Shock Absorber GasaJust; Rear; Wagon only", 30.95);
ListOfParts->Add(part);
part = new TPartDescription(927495, 2005, L"Volvo",
L"S40 2.5L T5 AWD", L"Engine Mechanical",
L"Timing Belt Idler Pulley Original Equipment INA",
65.55);
ListOfParts->Add(part);
part = new TPartDescription(979374, 2000, L"Toyota",
L"RAV4 2WD/4-DOOR", L"Cooling System",
L"Thermostat Gasket", 4.95);
ListOfParts->Add(part);
part = new TPartDescription(542347, 2007, L"Buick",
L"Lacrosse CXS V6 3.6", L"Brake",
L"Brake Pad Set ProACT Ceramic w/Shims; Front", 80.05);
ListOfParts->Add(part);
part = new TPartDescription(683064, 2000, L"Toyota",
L"RAV4 2WD/4-DOOR", L"Cooling System",
L"Radiator Hose; Upper", 103.75);
ListOfParts->Add(part);
part = new TPartDescription(248759, 1999, L"Jeep",
L"Wrangler Sahara", L"Air Intake",
L"Air Filter", 50.95);
ListOfParts->Add(part);
part = new TPartDescription(973974, 2007, L"Toyota",
L"Corolla", L"Air Intake",
L"Air Mass Meter; W/o Housing; Meter/sensor only",
134.95);
ListOfParts->Add(part);
part = new TPartDescription(285800, 2001, L"Ford",
L"Escape XLT 4WD", L"Transmission", L"AT Filter", 34.95);
ListOfParts->Add(part);
part = new TPartDescription(207495, 2007, L"Toyota",
L"Corolla", L"Body Electrical",
L"Headlight Bulb; 12V 65W; inner-high beam", 9.35);
ListOfParts->Add(part);
part = new TPartDescription(566676, 2000, L"Toyota",
L"RAV4 2WD/4-DOOR", L"Cooling System",
L"Auxiliary Fan Switch", 42.95);
ListOfParts->Add(part);
part = new TPartDescription(304950, 2007, L"Toyota",
L"Corolla", L"Body Electrical",
L"Headlight Bulb; 12V 51W; outer", 7.85);
ListOfParts->Add(part);
part = new TPartDescription(797394, 2000, L"Toyota",
L"RAV4 2WD/4-DOOR", L"Cooling System",
L"Water Flange Gasket", 0.85);
ListOfParts->Add(part);
part = new TPartDescription(910203, 2007, L"Buick",
L"Lacrosse CXS V6 3.6", L"Suspension",
L"Strut Mount Inc; Sleeve; Rear Right", 80.85);
ListOfParts->Add(part);
part = new TPartDescription(790794, 2000, L"Toyota",
L"RAV4 2WD/4-DOOR", L"Cooling System",
L"Radiator Hose; Lower", 9.45);
ListOfParts->Add(part);
part = new TPartDescription(970394, 2007, L"Buick",
L"Lacrosse CXS V6 3.6", L"Suspension",
L"Coil Spring Insulator; Front Lower", 14.55);
ListOfParts->Add(part);
part = new TPartDescription(290840, 2005, L"Volvo",
L"S40 2.5L T5 AWD", L"Engine Mechanical",
L"Rod Bearing Set 1 per Rod; Standard; Reqs. 5-per Engine",
26.95);
ListOfParts->Add(part);
part = new TPartDescription(209704, 2007, L"Toyota",
L"Corolla", L"Body Electrical",
L"Wiper Blade Excel+; Front Right", 7.25);
ListOfParts->Add(part);
part = new TPartDescription(200368, 2000, L"Toyota",
L"RAV4 2WD/4-DOOR", L"Cooling System",
L"Radiator Drain Plug incl; gasket", 3.15);
ListOfParts->Add(part);
part = new TPartDescription(200970, 2005, L"Volvo",
L"S40 2.5L T5 AWD", L"Engine Mechanical",
L"Reference Sensor; Flywheel Engine Speed", 62.05);
ListOfParts->Add(part);
part = new TPartDescription(542347, 2007, L"Buick",
L"Lacrosse CXS V6 3.6", L"Air Intake",
L"Air Filter", 50.25);
ListOfParts->Add(part);
part = new TPartDescription(927045, 2001, L"Ford",
L"Escape XLT 4WD", L"Air Intake",
L"Air Filter", 62.95);
ListOfParts->Add(part);
part = new TPartDescription(990659, 2000, L"Toyota",
L"RAV4 2WD/4-DOOR", L"Cooling System",
L"Radiator OE Plastic tank", 136.85);
ListOfParts->Add(part);
part = new TPartDescription(440574, 2007, L"Buick",
L"Lacrosse CXS V6 3.6", L"Suspension",
L"Strut Mount Inc; Sleeve; Rear Left", 80.80);
}
//---------------------------------------------------------------------------
- Return to the form and double-click the Year combo box
- To display the list of car makes when the user selects a year,
implement the event as follows:
//---------------------------------------------------------------------------
void __fastcall TfrmInventory::cbxYearsChange(TObject *Sender)
{
// Empty all combo boxes
cbxMakes->Text = "";
cbxMakes->Items->Clear();
cbxModels->Text = "";
cbxModels->Items->Clear();
cbxCategories->Text = "";
cbxCategories->Items->Clear();
// Get to each record in the list of parts
for(int i = 0; i < ListOfParts->Count; i++)
{
// Get a reference to the record that is currently being checked
TPartDescription * part =
reinterpret_cast<TPartDescription *>(ListOfParts->Items[i]);
// If year of the record is the same as the year in the combo box,
// then show its corresponding makes
if( part->CarYear == cbxYears->Text.ToInt() )
if( cbxMakes->Items->IndexOf(part->Make) <= -1)
cbxMakes->Items->Add(part->Make);
}
}
//---------------------------------------------------------------------------
- Return to the form and double-click the Make combo box
- To display the list of car models when the user has selected a year
and a make, implement the event as follows:
//---------------------------------------------------------------------------
void __fastcall TfrmInventory::cbxMakesChange(TObject *Sender)
{
cbxModels->Text = "";
cbxModels->Items->Clear();
cbxCategories->Text = "";
cbxCategories->Items->Clear();
for(int i = 0; i < ListOfParts->Count; i++)
{
TPartDescription * part =
reinterpret_cast<TPartDescription *>(ListOfParts->Items[i]);
if( (part->CarYear == cbxYears->Text.ToInt() ) &&
(part->Make == cbxMakes->Text))
if( cbxModels->Items->IndexOf(part->Model) <= -1)
cbxModels->Items->Add(part->Model);
}
}
//---------------------------------------------------------------------------
- Return to the form and double-click the Model combo box
- To display the list of categories after the user has selected the
year, the make, and the model, implement the event as follows:
//---------------------------------------------------------------------------
void __fastcall TfrmInventory::cbxModelsChange(TObject *Sender)
{
for(int i = 0; i < ListOfParts->Count; i++)
{
TPartDescription * part =
reinterpret_cast<TPartDescription *>(ListOfParts->Items[i]);
if( (part->CarYear == cbxYears->Text.ToInt() ) &&
(part->Make == cbxMakes->Text) &&
(part->Model == cbxModels->Text) )
if (cbxCategories->Items->IndexOf(part->Category) <= -1)
cbxCategories->Items->Add(part->Category);
}
}
//---------------------------------------------------------------------------
- Return to the form and double-click the Category combo box
- To display the list of available parts, implement the event and
define a new method as follows:
//---------------------------------------------------------------------------
void __fastcall TfrmInventory::cbxCategoriesChange(TObject *Sender)
{
// This list will hold some items based on the selection
TList * SelectedParts = new TList();
// Check each record in the list of parts
for(int i = 0; i < ListOfParts->Count; i++)
{
// Get a reference to the part that is being checked
TPartDescription * part =
reinterpret_cast<TPartDescription *>(ListOfParts->Items[i]);
// If/When you find a record that matches
// the values of the combo boxes . . .
if( (part->CarYear == cbxYears->Text.ToInt() ) &&
(part->Make == cbxMakes->Text) &&
(part->Model == cbxModels->Text) &&
(part->Category == cbxCategories->Text))
{
// . . . get the values from that part and create a selected part
TPartSelected *selected = new TPartSelected(part->PartNumber,
part->PartName,
part->UnitPrice);
SelectedParts->Add(selected);
}
}
grdAvailableParts->RowCount = SelectedParts->Count + 1;
if( SelectedParts->Count > 0 )
{
for(int i = 1; i <= SelectedParts->Count; i++)
{
TPartSelected * item =
reinterpret_cast<TPartSelected *>(SelectedParts->Items[i - 1]);
grdAvailableParts->Cells[0][i] = item->PartNumber;
grdAvailableParts->Cells[1][i] = item->PartName;
grdAvailableParts->Cells[2][i] = item->UnitPrice;
}
}
}
//---------------------------------------------------------------------------
- Save all
The Styles of a Combo Box
|
|
There are three styles of combo boxes, although all
allow the user to make only one selection. These styles are controlled by
the TComboBoxStyle enumeration:
enum TComboBoxStyle{
csDropDown,
csSimple,
csDropDownList,
csOwnerDrawFixed,
csOwnerDrawVariable
};
The TComboBoxStyle enumeration controls the
Style property:
__property Stdctrls::TComboBoxStyle Style = {read=FStyle,write=SetStyle};
A combo box can be configured to allow the user to add
items to the list. In this case, if the user does not find the desired item
in the list, he can type a new value:
To provide this ability, set the Style
to csDropDown, which is the default style. If you set the
Style to csDropDownList, the user cannot enter a new item in
the list but can still select one from the control:
A combo box with the csSimple Style
permanently displays a combination of an edit box and a list box:
Practical
Learning: Applying a Style to a Combo Box
|
|
- On the form, click the Make combo box
- In the Object Inspector, click Style, then click the arrow of its
combo box and select csDropDownList
- On the form, click the Model combo box
- Press and hold Shift
- Click the Category combo box
- Release Shift
- In the Object Inspector, click Style, then click the arrow of its
combo box and select csDropDownList
- Save all
- To execute the application to test it, on the Standard toolbar,
click the Run button
- After using it, close the form and return to your programming
environment
The Number of Dropped Down Items
|
|
If the combo box has a style other than csSimple,
there is typically a fixed number of items that display when the user clicks
the control’s arrow. You can control the number of items that displays using
the DropDownCount property:
__property int DropDownCount = {read=FDropDownCount,write=SetDropDownCount};
By default, this is set to 8. If the list contains a
number of items less than the DropdownCount integer value, all of the
items would display fine. If the list contains more than the
DropDownCount number of items, when the user clicks the arrow, a scroll
box would appear. The control would display DropDownCount number of
items. To reveal more, the user would have to scroll in the list. The user
can scroll in the list box and select an item. After the selection, the
control would still display the list.
The other two styles, csOwnerDrawFixed and
csOwnerDrawVariable, are typically used to display varying objects such
as pictures or colors.
The text that displays on a non-drawn combo box is a
UnicodeString object. If you want the combo box to display a certain string,
use the Text property. At design time, you can set this
only if the control’s Style is csDropDown or
csSimple. At run time, you can set the text that would display in the
combo box at startup using the ItemIndex property. This property
represents the ordinal integer item of the combo box. The items are counted
from 0, then 1, and so on. The ItemIndex of each item is set
depending on the value to the Sorted property. If the list
is not sorted, the first item entered in the list has an ItemIndex
value of 0. If the list gets sorted, the first item in ascending order has
an ItemIndex property set at 0. You can therefore use this property
to control what item to display in the list:
//---------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
cbxColors->ItemIndex = 0;
}
//---------------------------------------------------------------------------
You can also use the ItemIndex property to find out what
item is selected at a given time.
|
|