Car Inventory Using a Slider

Car Inventory

Introduction

This small application uses a dialog box on which the user can review a list of cars.

Prerequisites:

  • Dialog Boxes
  • Group Box
  • Label
  • Edit Box
  • Picture Box
  • TrackBar

 

Creating the Application

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

  1. Start Microsoft Visual Basic .NET and create a new Windows Application named CarInventory1
  2. Save the following pictures, with their default names, in the folder that contains the current project (if you are using Microsoft Visual Basic .NET 2003, after you have created the CarInventory1 project, two other folders with name bin and obj are created; therefore, save these pictures in the bin folder inside of the CarInventory1 folder):
     
  3. Test the application
  4. Close it and return to Visual Studio .NET

Designing the Application

  1. 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
  2. Save everything
 

Coding the Application

  1. On the main menu, click Project -> Add Class
  2. Set the name of the class to ListOfCars and press Enter
  3. Create the class as follows:
     
    Public Class ListOfCars
        Public Make As String
        Public Model As String
        Public CarYear As Int16
        Public Doors As Byte
        Public CarPicture As String
    End Class
  4. Double-click an empty area of the form
  5. Before using the cars, declare a global array of ListOfCars just under the Inherits line. Name the array variable Car as follows:
     
    Public Class Form1
        Inherits System.Windows.Forms.Form
    
        Dim Car(10) As ListOfCars
    
  6. To initialize the array and the slider, implement the Load event as follows:
     
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Car(0) = New ListOfCars
    
            Car(0).Make = "Honda"
            Car(0).Model = "Civic"
            Car(0).CarYear = 1998
            Car(0).Doors = 4
            Car(0).CarPicture = "Civic1.bmp"
    
            Car(1) = New ListOfCars
            Car(1).Make = "Hyundai"
            Car(1).Model = "Elantra"
            Car(1).CarYear = 1996
            Car(1).Doors = 4
            Car(1).CarPicture = "Elantra.bmp"
    
            Car(2) = New ListOfCars
            Car(2).Make = "Ford"
            Car(2).Model = "Escape"
            Car(2).CarYear = 2003
            Car(2).Doors = 5
            Car(2).CarPicture = "FordEscape1.bmp"
    
            Car(3) = New ListOfCars
            Car(3).Make = "Ford"
            Car(3).Model = "Escort"
            Car(3).CarYear = 1997
            Car(3).Doors = 2
            Car(3).CarPicture = "FordEscort1.bmp"
    
            Car(4) = New ListOfCars
            Car(4).Make = "Mercury"
            Car(4).Model = "Grand Marquis"
            Car(4).CarYear = 2001
            Car(4).Doors = 4
            Car(4).CarPicture = "GrandMarquis.bmp"
    
            Car(5) = New ListOfCars
            Car(5).Make = "Mercury"
            Car(5).Model = "Mystique"
            Car(5).CarYear = 2000
            Car(5).Doors = 4
            Car(5).CarPicture = "Mystique.bmp"
    
            Car(6) = New ListOfCars
            Car(6).Make = "Lincoln"
            Car(6).Model = "Navigator"
            Car(6).CarYear = 2003
            Car(6).Doors = 5
            Car(6).CarPicture = "Navigator1.bmp"
    
            Car(7) = New ListOfCars
            Car(7).Make = "Nissan"
            Car(7).Model = "Sentra"
            Car(7).CarYear = 1997
            Car(7).Doors = 2
            Car(7).CarPicture = "Sentra.bmp"
    
            Car(8) = New ListOfCars
            Car(8).Make = "Ford"
            Car(8).Model = "Focus"
            Car(8).CarYear = 2002
            Car(8).Doors = 4
            Car(8).CarPicture = "Focus.bmp"
    
            Car(9) = New ListOfCars
            Car(9).Make = "Ki"
            Car(9).Model = "Sephia"
            Car(9).CarYear = 2003
            Car(9).Doors = 4
            Car(9).CarPicture = "Sephia.bmp"
    
        End Sub
  7. When the user slides the track bar, we will update the values of the controls depending on the selected car.
    Get back to the form and double-click the TrackBar control to access its OnScroll event
  8. Implement it as follows:
     
    Private Sub trbSlider_Scroll(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles trbSlider.Scroll
            ' Get the index of the current value of the track bar - 1
            Dim CurPos As Int16 = trbSlider.Value
    
            ' Based on the current index, retrieve the values of the
            ' current car and assign each to the corresponding control 
            txtMake.Text = Car(CurPos).Make
            txtModel.Text = Car(CurPos).Model
            txtYear.Text = Car(CurPos).CarYear.ToString()
            txtDoors.Text = Car(CurPos).Doors.ToString()
            pctCarPicture.Image = Drawing.Image.FromFile(Car(CurPos).CarPicture)
    
        End Sub
    
        Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
            Close()
        End Sub
  9. Test the application
 

Home Copyright 2004 FunctionX, Inc.