Sie sind auf Seite 1von 9

Car Inventory

Page | 1

Introduction

This application explores the characteristics of a track bar, also called a slider control. It uses a
class that holds various pieces of information about cars. One of the track bars allows the user
to navigate from one car to another. When the position of that track bar changes, the compiler
refers to an array of car to locate the object to display. When this happens, the default picture
of the car also displays.

A vertical track bar allows the user to show other pictures of a car, in case other pictures are
available.

Practical Learning: Creating the Application


1. Start a new Windows Application named CarInventory1
2. Copy the pictures of the cars and paste them in the
CarInventory1\CarInventory1\bin\debug folder of the current project
3. On the main menu, click Project -> Add Class...
4. Set the Name to Car and click Add
5. Create the following members of the class:

Public Class Car


Public Make As String
Public Model As String
Public Year As Integer
Public Price As Double
Public Picture As String
End Class

6. In the Solution Explorer, right-click Form1.vb and click Rename


7. Set the name to CarInventory.vb and press Enter twice Design the form as follows:
Page | 2

Control Text Name Additional Properties


GroupBox Car Description
Label Make:
TextBox Honda txtMake
Label Model:
TextBox txtModel
Label Year:
TextBox txtYear TextAlign: Right
Label Price:
TextBox txtPrice TextAlign: Right
PictureBox pbxCarImage SizeMode: CenterImage
TrackBar tbrImages Orientation: Vertical
TrackBar tbrReview
Button Close btnClose

8. Execute the application to test the form


9. Close the form and return to your programming environment
10. On the form, click the right track bar
11. In the Properties window, click TickStyle and select Both from its combo box
12. On the form, click the bottom track bar
13. In the Properties window, click TickStyle and select TopLeft from its combo box

Page | 3

14. Save all


15. Right-click the form and click View Code
16. Declare the following variables:

Public Class CarInventory


Private ReviewPosition As Integer

Private Cars(15) As Car

' We will not use the commented arrays.


' They are only mentioned here for reference
Private Pictures0() As String
Private Pictures1(5) As String
Private Pictures2(3) As String
' Private pictures3(0) As String
Private Pictures4(3) As String
Private Pictures5(3) As String
Private Pictures6(3) As String
' Private pictures7(0) As String
' Private pictures8(0) As String
' Private pictures9(0) As String
Private Pictures10(3) As String
Private Pictures11(4) As String
' Private pictures12(0) As String
Private Pictures13(7) As String
Private Pictures14(2) As String

End Class

17. In the Class Name combo box, select tbrReview


18. In the Method Name combo box, select Scroll and implement the event as follows:
Private Sub tbrReviewScroll(ByVal sender As Object,
ByVal e As System.EventArgs)
Handles tbrReview.Scroll
' Get the index of the current value of the track bar
ReviewPosition = tbrReview.Value

' Based on the current index, retrieve the values of the Page | 4
' current car and assign each to the corresponding control
txtMake.Text = Cars(ReviewPosition).Make
txtModel.Text = Cars(ReviewPosition).Model
txtYear.Text = Cars(ReviewPosition).Year.ToString()
txtPrice.Text = Cars(ReviewPosition).Price.ToString()
pbxCarImage.Image = Image.FromFile(Cars(ReviewPosition).Picture)
' To make the right track bar aware of the number
' of pictures of each car, set its maximum property
' according to the number of pictures (- 1)
Select Case ReviewPosition
Case 0
tbrImages.Maximum = 8
Case 1
tbrImages.Maximum = 5
Case 2
tbrImages.Maximum = 3
Case 3
tbrImages.Maximum = 0
Case 4
tbrImages.Maximum = 3
Case 5
tbrImages.Maximum = 3
Case 6
tbrImages.Maximum = 3
Case 7
tbrImages.Maximum = 0
Case 8
tbrImages.Maximum = 0
Case 9
tbrImages.Maximum = 0
Case 10
tbrImages.Maximum = 3
Case 11
tbrImages.Maximum = 4
Case 12
tbrImages.Maximum = 0
Case 13
tbrImages.Maximum = 7
Case 14
tbrImages.Maximum = 1
Case Else
End Select
tbrImages.Value = 0
End Sub

19. In the Class Name combo box, select tbrImages


20. In the Method Name combo box, select Scroll and implement the event as follows:
Private Sub tbrImagesScroll(ByVal sender As Object,
ByVal e As System.EventArgs)
Handles tbrImages.Scroll
' Check the position of the bottom track bar
' Then, use the value of the right track bar
' to locate the right picture
Select Case ReviewPosition Page | 5
Case 0
pbxCarImage.Image = Image.FromFile(Pictures0(tbrImages.Value))
Case 1
pbxCarImage.Image = Image.FromFile(Pictures1(tbrImages.Value))
Case 2
pbxCarImage.Image = Image.FromFile(Pictures2(tbrImages.Value))
Case 4
pbxCarImage.Image = Image.FromFile(Pictures4(tbrImages.Value))
Case 5
pbxCarImage.Image = Image.FromFile(Pictures5(tbrImages.Value))
Case 6
pbxCarImage.Image = Image.FromFile(Pictures6(tbrImages.Value))
Case 10
pbxCarImage.Image = Image.FromFile(Pictures10(tbrImages.Value))
Case 11
pbxCarImage.Image = Image.FromFile(Pictures11(tbrImages.Value))
Case 13
pbxCarImage.Image = Image.FromFile(Pictures13(tbrImages.Value))
Case 14
pbxCarImage.Image = Image.FromFile(Pictures14(tbrImages.Value))
Case Else
End Select
End Sub

21. In the Class Name combo box, select (CarInventory Events)

In the Method Name combo box, select Load and implement the event as follows:
Private Sub CarInventoryLoad(ByVal sender As Object,
ByVal e As System.EventArgs)
Handles Me.Load
' Create the list of cars
Cars(0) = New Car()
Cars(0).Make = "BMW"
Cars(0).Model = "335i"
Cars(0).Year = 2007
Cars(0).Price = 42580
Cars(0).Picture = "335iA.gif"

Cars(1) = New Car()


Cars(1).Make = "Honda"
Cars(1).Model = "Accord"
Cars(1).Year = 2007
Cars(1).Price = 24550
Cars(1).Picture = "Accord1.gif"

Cars(2) = New Car()


Cars(2).Make = "Chevrolet"
Cars(2).Model = "Avalanche"
Cars(2).Year = 2007
Cars(2).Price = 36880
Cars(2).Picture = "Avalanche1.gif"

Cars(3) = New Car()


Cars(3).Make = "Volvo"
Cars(3).Model = "C70"
Cars(3).Year = 1997
Cars(3).Price = 42320 Page | 6
Cars(3).Picture = "C70.gif"

Cars(4) = New Car()


Cars(4).Make = "Land Rover"
Cars(4).Model = "LR3"
Cars(4).Year = 2007
Cars(4).Price = 46245
Cars(4).Picture = "LR3a.gif"

Cars(5) = New Car()


Cars(5).Make = "Honda"
Cars(5).Model = "Civic"
Cars(5).Year = 2000
Cars(5).Price = 22665
Cars(5).Picture = "Civic1.gif"

Cars(6) = New Car()


Cars(6).Make = "Mazda"
Cars(6).Model = "Mazda5"
Cars(6).Year = 2007
Cars(6).Price = 20845
Cars(6).Picture = "Mazda5a.gif"

Cars(7) = New Car()


Cars(7).Make = "Ford"
Cars(7).Model = "Escape"
Cars(7).Year = 1997
Cars(7).Price = 22445
Cars(7).Picture = "Escape.gif"

Cars(8) = New Car()


Cars(8).Make = "Acura"
Cars(8).Model = "TSX"
Cars(8).Year = 2006
Cars(8).Price = 26445
Cars(8).Picture = "TSX.gif"

Cars(9) = New Car()


Cars(9).Make = "Mazda"
Cars(9).Model = "Miata"
Cars(9).Year = 2008
Cars(9).Price = 24180
Cars(9).Picture = "Miata.gif"

Cars(10) = New Car()


Cars(10).Make = "Ford"
Cars(10).Model = "F-150"
Cars(10).Year = 2006
Cars(10).Price = 20475
Cars(10).Picture = "F150a.gif"

Cars(11) = New Car()


Cars(11).Make = "Volvo"
Cars(11).Model = "S40"
Cars(11).Year = 2008
Cars(11).Price = 25285 Page | 7
Cars(11).Picture = "S40a.gif"

Cars(12) = New Car()


Cars(12).Make = "BMW"
Cars(12).Model = "750i"
Cars(12).Year = 2007
Cars(12).Price = 88925
Cars(12).Picture = "750Li.gif"

Cars(13) = New Car()


Cars(13).Make = "Buick"
Cars(13).Model = "LaCrosse"
Cars(13).Year = 2002
Cars(13).Price = 28685
Cars(13).Picture = "LaCrosse1.gif"

Cars(14) = New Car()


Cars(14).Make = "Ford"
Cars(14).Model = "E-150 XL"
Cars(14).Year = 2002
Cars(14).Price = 26660
Cars(14).Picture = "E150XLa.gif"

' Create the list of pictures for each car


Pictures0 = New String() {
"335iA.gif", "335iB.gif", "335iC.gif",
"335iD.gif", "335iE.gif", "335iF.gif",
"335iG.gif", "335iH.gif", "335iI.gif"}
Pictures1 = New String() {
"Accord1.gif", "Accord2.gif", "Accord3.gif",
"Accord4.gif", "Accord5.gif", "Accord6.gif"}
Pictures2 = New String() {
"Avalanche1.gif", "Avalanche2.gif",
"Avalanche3.gif", "Avalanche4.gif"}
Pictures4 = New String() {
"LR3a.gif", "LR3b.gif",
"LR3b.gif", "LR3d.gif"}
Pictures5 = New String() {
"Civic1.gif", "Civic2.gif",
"Civic3.gif", "Civic4.gif"}
Pictures6 = New String() {
"Mazda5a.gif", "Mazda5b.gif",
"Mazda5c.gif", "Mazda5d.gif"}
Pictures10 = New String() {
"F150a.gif", "F150b.gif",
"F150c.gif", "F150d.gif"}
Pictures11 = New String() {
"S40a.gif", "S40b.gif",
"S40c.gif", "S40d.gif", "S40e.gif"}
Pictures13 = New String() {
"LaCrosse1.gif", "LaCrosse2.gif", "LaCrosse3.gif",
"LaCrosse4.gif", "LaCrosse5.gif", "LaCrosse6.gif",
"LaCrosse7.gif", "LaCrosse8.gif"}
Pictures14 = New String() {
"E150XLa.gif", "E150XLb.gif"}
Page | 8
' Call the Scroll event of the bottom track bar
' as if it had been scrolled
tbrReviewScroll(sender, e)
End Sub

22. In the Class Name combo box, select btnClose


23. In the Method Name combo box, select Click and implement the event as follows:

Private Sub btnCloseClick(ByVal sender As Object,


ByVal e As System.EventArgs)
Handles btnClose.Click
End
End Sub

24. Execute the application and test the track bar:


Page | 9

25. Close the form and return to your programming environment