Results 1 to 4 of 4

Thread: URGENT HELP NEEDED! Listbox problem

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jun 2005
    Posts
    19

    Exclamation URGENT HELP NEEDED! Listbox problem

    Why doesn't this code work? i want the program to go through each listbox item and give a value to the lstbox() array so that i know which items are selected. If lstbox() = 0 then that item wasnt selected. If lstbox() = 1 then item was selected.

    Code:
            lstbox(0) = 0 
            lstbox(1) = 0 
            lstbox(2) = 0 
            lstbox(3) = 0 
            lstbox(4) = 0 
            lstbox(5) = 0 
            lstbox(6) = 0 
            lstbox(7) = 0 
            lstbox(8) = 0 
            lstbox(9) = 0 
    
            Dim n As Integer = 0 
           
            If listbox.SelectedIndex = -1 Then 
                elephant += 1 
            Else 
                Do 
                    If listbox.Items(n) = listbox.SelectedIndex Then 'if client is selected then add a value to array 
                        lstbox(n) = 1 
                    Else 
                        lstbox(n) = 0 
                    End If 
                    n += 1 
                Loop Until n = 10 
            End If
    Please help!

  2. #2
    PowerPoster VBDT's Avatar
    Join Date
    Sep 2005
    Location
    CA - USA
    Posts
    2,922

    Re: URGENT HELP NEEDED! Listbox problem

    Why don’t you use
    VB Code:
    1. Dim indexArray(Me.ListBox1.SelectedIndices.Count - 1) As Integer
    2. Me.ListBox1.SelectedIndices.CopyTo(indexArray, 0)

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: URGENT HELP NEEDED! Listbox problem

    Oh, an urgent problem. Better answer this one quick because all the others are so much less important.

    You get the items that are selected from the ListBox's SelectedItems property. If you just want to know whether a specific item is selected then you use the GetSelected property.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4
    PowerPoster VBDT's Avatar
    Join Date
    Sep 2005
    Location
    CA - USA
    Posts
    2,922

    Re: URGENT HELP NEEDED! Listbox problem

    I honestly don’t understand the purpose of having an array to keep a record of selected indexes. You can get the info at any time you call the Me.ListBox1.SelectedIndices method.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width