Click here to Skip to main content
15,886,666 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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:
VB
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
Updated 2-May-12 22:45pm
v2
Comments
Sandeep Mewara 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.
Maciej Los 3-May-12 4:48am    
Exactly!
[no name] 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...

Sandeep Mewara was trying to tell you to use selecteditem object, not ListBox1.SelectedItems(LBitmCounter). In my example i replaced it with oItem
VB
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.
 
Share this answer
 
Comments
VJ Reddy 4-May-12 12:45pm    
+5
Maciej Los 4-May-12 13:25pm    
Thank you, VJ ;)
VB
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
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900