This small application uses a dialog box on which the
user can review a list of cars.
Prerequisites:
Text
Boxes
Labels
Buttons
Group Box
TrackBar |
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# and create a new Windows Application named CarInventory1
- Save the following
pictures, with their default names, in a folder you can recognize,
such as C:\Programs
- Test the application
- Close it and return to Visual Studio
|
Designing the Application |
|
- 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 |
|
- Save everything
|
- 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 managed structure named CLisOfCars as follows:
namespace CarInventory1
{
/// <summary>
/// Object that stores information about each car
/// </summary>
struct CListOfCars
{
public String Make;
public String Model;
public int CarYear;
public int Doors;
public String CarPicture;
};
|
- Before using the cars, declare a managed array of CListOfCars
just under InitializeComponents. Name the array variable Car as
follows:
namespace CarInventory1
{
/// <summary>
/// Object that stores information about each car
/// </summary>
struct CListOfCars
{
public String Make;
public String Model;
public int CarYear;
public int Doors;
public String CarPicture;
};
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
. . .
private System.ComponentModel.Container components = null;
CListOfCars[] Car = new CListOfCars[10];
public Form1()
{
|
- To initialize the array and the slider, return to the form.
Double-click an empty area of the form to access its OnLoad() event
and implement it as follows:
private void Form1_Load(object sender, System.EventArgs e)
{
Car[0].Make = "Honda";
Car[0].Model = "Civic";
Car[0].CarYear = 1998;
Car[0].Doors = 4;
Car[0].CarPicture = "C:\\Programs\\Civic1.bmp";
Car[1].Make = "Hyundai";
Car[1].Model = "Elantra";
Car[1].CarYear = 1996;
Car[1].Doors = 4;
Car[1].CarPicture = "C:\\Programs\\Elantra.bmp";
Car[2].Make = "Ford";
Car[2].Model = "Escape";
Car[2].CarYear = 2003;
Car[2].Doors = 5;
Car[2].CarPicture = "C:\\Programs\\FordEscape1.bmp";
Car[3].Make = "Ford";
Car[3].Model = "Escort";
Car[3].CarYear = 1997;
Car[3].Doors = 2;
Car[3].CarPicture = "C:\\Programs\\FordEscort1.bmp";
Car[4].Make = "Mercury";
Car[4].Model = "Grand Marquis";
Car[4].CarYear = 2001;
Car[4].Doors = 4;
Car[4].CarPicture = "C:\\Programs\\GrandMarquis.bmp";
Car[5].Make = "Mercury";
Car[5].Model = "Mystique";
Car[5].CarYear = 2000;
Car[5].Doors = 4;
Car[5].CarPicture = "C:\\Programs\\Mystique.bmp";
Car[6].Make = "Lincoln";
Car[6].Model = "Navigator";
Car[6].CarYear = 2003;
Car[6].Doors = 5;
Car[6].CarPicture = "C:\\Programs\\Navigator1.bmp";
Car[7].Make = "Nissan";
Car[7].Model = "Sentra";
Car[7].CarYear = 1997;
Car[7].Doors = 2;
Car[7].CarPicture = "C:\\Programs\\Sentra.bmp";
Car[8].Make = "Ford";
Car[8].Model = "Focus";
Car[8].CarYear = 2002;
Car[8].Doors = 4;
Car[8].CarPicture = "C:\\Programs\\Focus.bmp";
Car[9].Make = "Kia";
Car[9].Model = "Sephia";
Car[9].CarYear = 2003;
Car[9].Doors = 4;
Car[9].CarPicture = "C:\\Programs\\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:
private void trbSlider_Scroll(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.Car[CurPos].Make;
this.txtModel.Text = this.Car[CurPos].Model;
this.txtYear.Text = this.Car[CurPos].CarYear.ToString();
this.txtDoors.Text = this.Car[CurPos].Doors.ToString();
this.pctCarPicture.Image = System.Drawing.Image.FromFile(Car[CurPos].CarPicture);
}
|
- Double-click the Close button and implement its Click event as
follows:
private void btnClose_Click(object sender, System.EventArgs e)
{
Close();
}
|
- Test the application
|
|
|