This small application uses a dialog box on which the
user can review a list of cars.
Prerequisites:
|
This application was used to study or review the Track
Bar control. We simply expand it here to include other controls that can
provide more information about the car that is displaying.
|
Practical Learning: Starting the Exercise |
|
- Start Microsoft Visual C++ 2005 and create a new Windows Forms
Application named CarInventory1
- Save the following pictures, with their default names, in the folder
that contains the current project (if you are using Microsoft Visual
C++ .Net 2003, after you have created the CarInventory1
project, another folder with the same name, CarInventory1, is
created; therefore, save these pictures in the CarInventory1
folder inside of the primary CarInventory1 folder):
- Design the form as follows:
|
Control |
Text |
Name |
Additional Properties |
Group Box |
Car Description |
|
|
Label |
Make: |
|
|
TextBox |
Honda |
txtMake |
|
Label |
Model: |
|
|
TextBox |
Civic |
txtModel |
|
Label |
Year: |
|
|
TextBox |
1998 |
txtYear |
|
Label |
Doors: |
|
|
TextBox |
4 |
txtDoors |
|
PictureBox |
|
pctCarPicture |
Image: Select Civic1.bmp |
TrackBar |
|
trbSlider |
Minimum: 1
TickStyle: TopLeft |
|
- Right-click the form and click View Code
- To store the values for the controls of the form, in the top section
of the file, create a value structure named CLisOfCars as follows:
#pragma once
namespace CarInventory1 {
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
public value struct CListOfCars
{
String ^ Make;
String ^ Model;
unsigned int CarYear;
unsigned int Doors;
String ^ CarPicture;
};
/// <summary>
. . .
|
- Before using the cars, declare an array of CListOfCars
just under InitializeComponents. Name the array variable Car as
follows:
#pragma once
namespace CarInventory1 {
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
public value struct CListOfCars
{
String ^ Make;
String ^ Model;
unsigned int CarYear;
unsigned int Doors;
String ^ CarPicture;
};
. . .
public ref class Form1 : public System::Windows::Forms::Form
{
public:
Form1(void)
{
InitializeComponent();
//
//TODO: Add the constructor code here
//
}
protected:
/// <summary>
/// Clean up any resources being used.
/// </summary>
~Form1()
{
if (components)
{
delete components;
}
}
. . .
private:
/// <summary>
/// Required designer variable.
/// </summary>
System::ComponentModel::Container ^components;
static array<CListOfCars ^> ^ Cars = gcnew array<CListOfCars ^>(10);
|
- Return to the form and double-click an empty area of its body
- To use the array and the slider, implement the OnLoad() event as follows:
System::Void Form1_Load(System::Object^ sender,
System::EventArgs^ e)
{
Cars[0] = gcnew CListOfCars;
Cars[0]->Make = L"Honda";
Cars[0]->Model = L"Civic";
Cars[0]->CarYear = 1998;
Cars[0]->Doors = 4;
Cars[0]->CarPicture = L"Civic1.bmp";
Cars[1] = gcnew CListOfCars;
Cars[1]->Make = L"Hyundai";
Cars[1]->Model = L"Elantra";
Cars[1]->CarYear = 1996;
Cars[1]->Doors = 4;
Cars[1]->CarPicture = L"Elantra.bmp";
Cars[2] = gcnew CListOfCars;
Cars[2]->Make = L"Ford";
Cars[2]->Model = L"Escape";
Cars[2]->CarYear = 2003;
Cars[2]->Doors = 5;
Cars[2]->CarPicture = L"FordEscape1.bmp";
Cars[3] = gcnew CListOfCars;
Cars[3]->Make = L"Ford";
Cars[3]->Model = L"Escort";
Cars[3]->CarYear = 1997;
Cars[3]->Doors = 2;
Cars[3]->CarPicture = L"FordEscort1.bmp";
Cars[4] = gcnew CListOfCars;
Cars[4]->Make = L"Mercury";
Cars[4]->Model = L"Grand Marquis";
Cars[4]->CarYear = 2001;
Cars[4]->Doors = 4;
Cars[4]->CarPicture = L"GrandMarquis.bmp";
Cars[5] = gcnew CListOfCars;
Cars[5]->Make = L"Mercury";
Cars[5]->Model = L"Mystique";
Cars[5]->CarYear = 2000;
Cars[5]->Doors = 4;
Cars[5]->CarPicture = L"Mystique.bmp";
Cars[6] = gcnew CListOfCars;
Cars[6]->Make = L"Lincoln";
Cars[6]->Model = L"Navigator";
Cars[6]->CarYear = 2003;
Cars[6]->Doors = 5;
Cars[6]->CarPicture = L"Navigator1.bmp";
Cars[7] = gcnew CListOfCars;
Cars[7]->Make = L"Nissan";
Cars[7]->Model = L"Sentra";
Cars[7]->CarYear = 1997;
Cars[7]->Doors = 2;
Cars[7]->CarPicture = L"Sentra.bmp";
Cars[8] = gcnew CListOfCars;
Cars[8]->Make = L"Ford";
Cars[8]->Model = L"Focus";
Cars[8]->CarYear = 2002;
Cars[8]->Doors = 4;
Cars[8]->CarPicture = L"Focus.bmp";
Cars[9] = gcnew CListOfCars;
Cars[9]->Make = L"Kia";
Cars[9]->Model = L"Sephia";
Cars[9]->CarYear = 2003;
Cars[9]->Doors = 4;
Cars[9]->CarPicture = L"Sephia.bmp";
}
|
- When the user slides the track bar, we will update the values of the
selected car and we will call the OnPaint() event of the form
to change the picture of the car.
Get back to the form and double-click the TrackBar control to access
its OnScroll event
- Implement it as follows:
System::Void trbSlider_Scroll(System::Object^ sender,
System::EventArgs^ e)
{
// Get the index of the current value of the track bar - 1
int CurPos = this->trbSlider->Value - 1;
// Based on the current index, retrieve the values of the
// current car and assign each to the corresponding control
this->txtMake->Text = this->Cars[CurPos]->Make;
this->txtModel->Text = this->Cars[CurPos]->Model;
this->txtYear->Text = this->Cars[CurPos]->CarYear.ToString();
this->txtDoors->Text = this->Cars[CurPos]->Doors.ToString();
this->pctCarPicture->Image =
Drawing::Image::FromFile(Cars[CurPos]->CarPicture);
}
|
- Return to the form
- Double-click the Close button and implement its event as follows:
System::Void btnClose_Click(System::Object^ sender,
System::EventArgs^ e)
{
Close();
}
|
- Test the application
- Close the form
|
|