Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
Hi there,
 
I've got a Listbox where SelectionMode = MultiSimple and I'd like to check the strings they contain to follow up with according actions (see Snippet) for each.
 
What I've tried so far:
For Each selecteditem As [Object] In ListBox1.SelectedItems
    If ListBox1.SelectedItem.ToString = "Text 1" Then
        Action 1
    ElseIf ListBox1.SelectedItem.ToString = "Text 2" Then
        Action 2
    ElseIf ListBox1.SelectedItem.ToString = "Text 3" Then
        Action 3
    End If
Next
 
The problem: While the For...Next Statement is executed for each item that is selected (If I select 3 items it's executed 3 times, if I select 2 it's executed 2 times, etc) but I think iBlade.ListBox2.SelectedItem.ToString only returns the first item I select.
 
If I select "Text 1", "Text 2" and "Text 3" the Action ("Action 1") for "Text 1" is executed three times. If I select "Text 2" and "Text 3" the Action ("Action 2") for "Text 2" is repeated two times and so on...
 
What I'd like to happen is for "Action 2" and "Action 3" each to be executed once. But I can't really figure it out.
Posted 2-May-12 23:38pm
Edited 2-May-12 23:45pm
v2
Comments
Sandeep Mewara at 3-May-12 4:42am
   
1. What is iBlade
2. You loop through multiple ListBox1.SelectedItems but you never user the loop item. Instead, you use one item for comparison. Your logic for the code looks messy. Resolve it and you should be good to go.
losmac at 3-May-12 4:48am
   
Exactly!
Saufkumpel at 3-May-12 4:48am
   
1. The name of the main form, I removed it to not confuse people and forgot that one... (The Sub for checking Listbox items is inside a module)
 
2. Now that you mention it, I realize how stupid (clumsy?) it was, to try it that way. I did it differently and I think that's the way I should have done it all along. (See solution)
 
Thank you for making me realize flawed logic...
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Sandeep Mewara was trying to tell you to use selecteditem object, not ListBox1.SelectedItems(LBitmCounter). In my example i replaced it with oItem
For Each oItem As [Object] In ListBox1.SelectedItems
    If oItem.ToString = "Text 1" Then
        Action 1
    ElseIf oItem.ToString = "Text 2" Then
        Action 2
    ElseIf oItem.ToString = "Text 3" Then
        Action 3
    End If
Next
 
Learn about SELECT CASE[^] statement.
  Permalink  
Comments
VJ Reddy at 4-May-12 12:45pm
   
+5
losmac at 4-May-12 13:25pm
   
Thank you, VJ ;)
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

For LBitmCounter = 0 To ListBox1.SelectedItems.Count - 1
    If ListBox1.SelectedItems(LBitmCounter).ToString = "Text 1" Then
        Action 1
    ElseIf ListBox1.SelectedItems(LBitmCounter).ToString = "Text 2" Then
        Action 2
    ElseIf ListBox1.SelectedItems(LBitmCounter).ToString = "Text 3" Then
        Action 3
    End If
Next
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 George Jonsson 215
1 Kornfeld Eliyahu Peter 169
2 OriginalGriff 120
3 PIEBALDconsult 110
4 BillWoodruff 85
0 OriginalGriff 6,165
1 DamithSL 4,658
2 Maciej Los 4,087
3 Kornfeld Eliyahu Peter 3,649
4 Sergey Alexandrovich Kryukov 3,294


Advertise | Privacy | Mobile
Web03 | 2.8.141220.1 | Last Updated 3 May 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100