Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations GregLocock on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

Multiselect Listbox

Status
Not open for further replies.

zimGirl

Geotechnical
Jul 22, 2004
30
I have this code for my multiselect list box. If I select more than one item

the code goes through each of the items twice before it gets out.
Code:
If lstCategory.ListIndex = -1 Then Exit Sub
    For intCounter = lstCategory.ListCount - 1 To 0 Step -1
    If lstCategory.Selected(intCounter) = True Then
        
    Select Case lstCategory.ListIndex
            Case 0          [COLOR=green]''Ambulance Stations[/color]
                Call DrawFunction
            Case 1          [COLOR=green]''Apartments[/color]
                Call DrawFunction
            Case 2         [COLOR=green] ''Arenas/Stadiums[/color]
                Call DrawFunction
            Case 3         [COLOR=green] ''Bingo Halls[/color]
                Call DrawFunction

     End Select   

   End If

Next intCounter

End If



If my list is:

Station

Apartments

Arena/Stadiums

Bingo Halls



If I select

Arena/Stadiums

Bingo Halls

It will loop through Bingo Halls twice before it goes to Arena/Stadiums, which it will do once.

How can I stop this from happening?
 
Replies continue below

Recommended for you

I think one issue may be that you're basing your case statement on the .ListIndex property, which will be the value of the last item that was selected. That is not necessarily the value that you want to check at that point in the loop. Perhaps you could try something like the following:
Code:
With lstCategory
   If (.ListIndex >= 0) Then
      For intCounter = .ListCount - 1 To 0 Step -1
         If (.Selected(intCounter) = True) Then
            Select Case .Column(0, intCounter)
               Case 0
                  DrawFunction
               Case 1
                  DrawFunction
               Case 2
                  DrawFunction
               Case 3
                  DrawFunction
            End Select
         End If
      Next intCounter
   End If
End With
Note that this sample is look at the value in column 0 of the selected item. That may not be the column which contains the numeric value, so you may have to change that value to a different column.
 
When it gets to this part
Code:
Select Case .Column(0, intCounter)
it runs through all the cases without picking the selected case. It is on the right item, Apartments for example, but it does not recognize the case number (0) associated with it.
 
I decide to remove the item after it has been used this seemed to solve the problem for me!!!

Code:
With lstCategory

If .ListIndex = -1 Then Exit Sub
	For intCounter = .ListCount - 1 To 0 Step -1
	If .Selected(intCounter) = True Then
		'MsgBox lstCategory.List(intCounter)
	Select Case .Selected(intCounter)
			Case 0		 ''Ambulance Stations
				Call DrawFunction
			Case 1		 ''Apartments
				Call DrawFunction
			Case 2		 ''Arenas/Stadiums
				Call DrawFunction
			Case 3		 ''Bingo Halls
				Call DrawFunction
	 End Select 

.RemoveItem intCounter
End If
Next intCounter
End If

End With
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor