click a button to move to the next item in a listbox

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • erbear
    New Member
    • Feb 2009
    • 1

    click a button to move to the next item in a listbox

    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:
    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
    Last edited by Frinavale; Mar 2 '09, 02:33 PM. Reason: Moved to VB.NET Answers from .NET, pbmods added CODE tags.
  • debasisdas
    Recognized Expert Expert
    • Dec 2006
    • 8119

    #2
    question moved to .net forum.

    Comment

    • tlhintoq
      Recognized Expert Specialist
      • Mar 2008
      • 3532

      #3
      That if...else construct is killer.
      Suggestion: Get the SelectedIndex then just display that indexed image and label text.

      Code:
      int x = ListBox1.SelectedIndex; // C# for make a int named x containing SelectedIndex.  You will have to phrase for VB
      
      If x >= 0 Then 
                  PictureBox1.Image = Image.FromFile(files(x)) 
                  ruleLabel.Text = rules(x).ToString
      Next suggestion: Qualify the math of your next and back buttons. You can't just keep adding and subtracting 1 from them and expect them to work. If someone keeps mashing on the next button they will drive it up to 1000 when you only have 15 items. Now they have to mash it 1000 times down before changes will take place.

      If (x+1) < ListBox.Items.C ount then add one to the index

      Last suggestion: Don't use the next and back buttons to directly change the items in the picture box and label. Use these buttons to change the selected index of the ListBox. This will fire the event of ListBox.Selecte dIndexChanged which causes your picture and label to change because you've already written this. So why write it again, and again in the next and back button behaviors?

      This also makes your UI match your program's thinking because the listbox will show what is selected.

      Most importantly the change behavior takes place in one method only. That is really important. Otherwise you will quickly have a dozen methods that all seem to do the same thing and it will be a monster to update. Any behavior should take place in one method only. Other methods can call it all they like.

      Comment

      Working...