Hi all,
I am new to VB, and I am having some troubles. I have a listbox that when you click on the item it displays a picture in a picture box and a message in a label. It works fine when an item is manually selected, but I want forward and back buttons that will do this. It works once but it doesn't highlight the new item in the listbox, so it gets stuck. I can't use a loop either.
Any help is greatly appreciated. See code below:
I am new to VB, and I am having some troubles. I have a listbox that when you click on the item it displays a picture in a picture box and a message in a label. It works fine when an item is manually selected, but I want forward and back buttons that will do this. It works once but it doesn't highlight the new item in the listbox, so it gets stuck. I can't use a loop either.
Any help is greatly appreciated. See code below:
Code:
Public Class Form1
Dim x As Integer
Dim files() As Object = {"home.jpg", "1.jpg", "2.jpg", "3.jpg", "4.jpg", "5.jpg", "6.jpg", "7.jpg", "8.jpg", "9.jpg", "10.jpg", "11.jpg", "12.jpg", "end.jpg"}
Dim rules() As String = {"The Basic Rules Of Firearm Safety.", _
"Handle all firearms as if they were loaded.", _
"Always keep the firearm pointed in a safe direction.", _
"Keep your finger out of the gun's trigger guard and off the trigger until you have aligned the gun's sights on a safe target and you have made the decision to fire.", _
"Always be certain that your target and the surrounding area are safe before firing.", _
"Whenever you handle a firearm, the first thing you should do (while keeping it pointed in a safe direction with your finger outside the trigger guard) is to open the action to determine whether or not the firearm is loaded.", _
"Thoroughly read the instruction manual supplied with your firearm.", _
"Before firing your weapon, you should routinely make sure that your firearm is in good working order and the barrel is clear of dirt and obstructions.", _
"Only use ammunition recommended by the firearm manufacturer, and always be certain that the ammunition matches the caliber of your gun.", _
"Quality ear and eye protection should always be worn when shooting or observing.", _
"Never use firearms while under the influence of drugs or alcohol.", _
"All firearms should be stored unloaded and secured in a safe storage case, inaccessible to children and untrained adults.", _
"The transportation of firearms is regulated by Federal, State, and local laws. Always transport your firearm in a safe, unloaded condition and in accordance with applicable laws.", _
"Remember-no set of rules can cover all possible situations. The safe and rational use of a firearm depends on the common sense and proper training of the user."}
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
ListBox1.Items.Add("Firearm Rules")
ListBox1.Items.Add("Rule 1")
ListBox1.Items.Add("Rule 2")
ListBox1.Items.Add("Rule 3")
ListBox1.Items.Add("Rule 4")
ListBox1.Items.Add("Rule 5")
ListBox1.Items.Add("Rule 6")
ListBox1.Items.Add("Rule 7")
ListBox1.Items.Add("Rule 8")
ListBox1.Items.Add("Rule 9")
ListBox1.Items.Add("Rule 10")
ListBox1.Items.Add("Rule 11")
ListBox1.Items.Add("Rule 12")
ListBox1.Items.Add("The End")
ListBox1.SelectedIndex = 0
End Sub
Private Sub ListBox1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListBox1.Click
If ListBox1.SelectedIndex = 0 Then
PictureBox1.Image = Image.FromFile(files(0))
ruleLabel.Text = rules(0).ToString
ElseIf ListBox1.SelectedIndex = 1 Then
PictureBox1.Image = Image.FromFile(files(1))
ruleLabel.Text = rules(1).ToString
ElseIf ListBox1.SelectedIndex = 2 Then
PictureBox1.Image = Image.FromFile(files(2))
ruleLabel.Text = rules(2).ToString
ElseIf ListBox1.SelectedIndex = 3 Then
PictureBox1.Image = Image.FromFile(files(3))
ruleLabel.Text = rules(3).ToString
ElseIf ListBox1.SelectedIndex = 4 Then
PictureBox1.Image = Image.FromFile(files(4))
ruleLabel.Text = rules(4).ToString
ElseIf ListBox1.SelectedIndex = 5 Then
PictureBox1.Image = Image.FromFile(files(5))
ruleLabel.Text = rules(5).ToString
ElseIf ListBox1.SelectedIndex = 6 Then
PictureBox1.Image = Image.FromFile(files(6))
ruleLabel.Text = rules(6).ToString
ElseIf ListBox1.SelectedIndex = 7 Then
PictureBox1.Image = Image.FromFile(files(7))
ruleLabel.Text = rules(7).ToString
ElseIf ListBox1.SelectedIndex = 8 Then
PictureBox1.Image = Image.FromFile(files(8))
ruleLabel.Text = rules(8).ToString
ElseIf ListBox1.SelectedIndex = 9 Then
PictureBox1.Image = Image.FromFile(files(9))
ruleLabel.Text = rules(9).ToString
ElseIf ListBox1.SelectedIndex = 10 Then
PictureBox1.Image = Image.FromFile(files(10))
ruleLabel.Text = rules(10).ToString
ElseIf ListBox1.SelectedIndex = 11 Then
PictureBox1.Image = Image.FromFile(files(11))
ruleLabel.Text = rules(11).ToString
ElseIf ListBox1.SelectedIndex = 12 Then
PictureBox1.Image = Image.FromFile(files(12))
ruleLabel.Text = rules(12).ToString
ElseIf ListBox1.SelectedIndex = 13 Then
PictureBox1.Image = Image.FromFile(files(13))
ruleLabel.Text = rules(13).ToString
End If
End Sub
Private Sub fwdButton_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles fwdButton.Click
x = ListBox1.SelectedIndex + 1
PictureBox1.Image = Image.FromFile(files(x))
ruleLabel.Text = rules(x)
End Sub
Private Sub backButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles backButton.Click
x = ListBox1.SelectedIndex - 1
PictureBox1.Image = Image.FromFile(files(x))
ruleLabel.Text = rules(x)
End Sub
End Class
Comment