Visual Basic Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
 
User Name:
Password:
Remember me
Go Back   ASP Free ForumsProgrammingVisual Basic Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread ASP Free Forums Sponsor:
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  
Old July 30th, 2004, 10:40 AM
QueenVBA QueenVBA is offline
Registered User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Jul 2004
Posts: 22 QueenVBA User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 17 m 43 sec
Reputation Power: 0
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?

Reply With Quote
  #2  
Old July 30th, 2004, 05:06 PM
spak111 spak111 is offline
Contributing User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Jun 2004
Posts: 349 spak111 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 29 m 25 sec
Reputation Power: 5
Send a message via AIM to spak111
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!

Reply With Quote
  #3  
Old August 3rd, 2004, 08:34 AM
QueenVBA QueenVBA is offline
Registered User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Jul 2004
Posts: 22 QueenVBA User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 17 m 43 sec
Reputation Power: 0
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

Reply With Quote
  #4  
Old August 10th, 2004, 01:45 PM
Mythomep Mythomep is offline
Contributing User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Aug 2004
Location: Zaandam, The Netherlands
Posts: 70 Mythomep User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 11 m 37 sec
Reputation Power: 4
Send a message via MSN to Mythomep
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:
  1. Define constants that represent the selected items. It is easier to understand the code when you come back in a few months. Like Public Const OPT_DRAW_AMBULANCE = 0. Then use Case OPT_DRAW_AMBULANCE in your code. Much easier and you don't have to change your mindset from reading code to reading comments. It is easier on the person next in line that inherits your code. They can see in one glance that selected item 0 draws ambulances, should you forget to mention that in the comments.
  2. Don't use While Wend structures. They are depricated and are only there for backwards compatebility. They are also slower then do .. loop constructs.
  3. Try to avoid using Exit sub or Exit Do/For. Using that creates an exit point for a routine, which makes it harder to debug. Whenever you find yourself coding an exit condition, stop and take a closer look at what you are trying to do. In 99.99 percent of the cases there is no need to exit a sub. There are some extreme rare cases, but you are not very likely to stumble acros them. Remember, using goto or exit often depicts poor logic and makes complexity higher and understanding harder (McCabe method).
Any questions, feel free to ask.

Grtz.©

M.

Reply With Quote
  #5  
Old August 10th, 2004, 01:47 PM
Mythomep Mythomep is offline
Contributing User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Aug 2004
Location: Zaandam, The Netherlands
Posts: 70 Mythomep User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 11 m 37 sec
Reputation Power: 4
Send a message via MSN to Mythomep
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.

Reply With Quote
  #6  
Old August 18th, 2004, 11:07 AM
QueenVBA QueenVBA is offline
Registered User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Jul 2004
Posts: 22 QueenVBA User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 17 m 43 sec
Reputation Power: 0
Thanks Mythomep.

Reply With Quote
Reply

Viewing: ASP Free ForumsProgrammingVisual Basic Programming > Multiselect List box


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway