|
|
|||||||||
|
|||||||||
|
|||||||||
| |
||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Ajax Application Generator Generate database and reporting .NET Web apps in minutes. Quickly create visually stunning, feature-rich apps that are easy to customize and ready to deploy. Download Now!
|
|
#1
|
|||
|
|||
|
Multiselect List box
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 'MsgBox lstCategory.List(intCounter) Select Case lstCategory.ListIndex 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 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? ![]() |
|
#2
|
|||
|
|||
|
Try something like this, it should work a little better...I tried it on my system and it worked fine.
Code:
If lstCategory.ListIndex = -1 Then Exit Sub
Dim intCounter As Integer
Dim maxint As Integer
maxint = lstCategory.ListCount
intCounter = 0
While intCounter < maxint
If lstCategory.Selected(intCounter) = True Then
'Your select Statement
End If
intCounter = intCounter + 1
Wend
I hope this helps! |
|
#3
|
|||
|
|||
|
Sorry that did not help!! it still loops through each selected item twice or more depending on the number of items selected.
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 Last edited by QueenVBA : August 3rd, 2004 at 09:57 AM. Reason: Adding Stuff |
|
#4
|
|||
|
|||
|
Hi,
The answer to your question is simple. The listindex property is always the first selected item in the list. But that is not the problem with your code. Removing items from a list to solve a problem is not very handy. Take another good look at it. What are you trying to achieve? You want to take an action based on what item(s) are selected. You use the listindex property to see what is selected. In your loop, you use a counter to see if a item in the list is selected. Selected() takes a listindex when it evaluates. See where I'm heading to? Your code should be: Code:
With lstCategory If .ListIndex <> -1 Then For intCounter = .ListCount - 1 To 0 Step -1 If (.Selected(intCounter)) Then Select Case intSelected Case 0 Call DrawFunction Case 1 Call DrawFunction End Select End If Next End If End With Then three tips:
Grtz.© M. |
|
#5
|
|||
|
|||
|
Hi,
I forgat to mention, coding an if statment that evaluates to true more often than false executes faster in VB (both in the IDE and in the compiled exe). Thus, whenever you code an If statement, try to code it so in 90 percent of the cases it evaluates true, not false. Grtz.© M. |
|
#6
|
|||
|
|||
|
Thanks Mythomep.
|
![]() |
| Viewing: ASP Free Forums > Programming > Visual Basic Programming > Multiselect List box |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|
|
|