Solved

vb.net read multiple selection is listbox

Posted on 2004-08-27
5
250 Views
Last Modified: 2010-04-17
I have a listbox that allows the user to make multiple selections. How can I loop through just the selections and display in a msgbox


0
Comment
Question by:peanut1010
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 11

Expert Comment

by:coopzz
ID: 11914596
by using the

ListBox1.SelectedItems     collection

do ya need some code on how to go throught a collection?
0
 

Author Comment

by:peanut1010
ID: 11915238
yes please
0
 
LVL 11

Expert Comment

by:coopzz
ID: 11915300
there ya go in your event that you want to see the selected items you can do it this way.

        Dim oItem As Object
        Dim s As String
        For Each oItem In ListBox1.SelectedItems
            s &= CType(oItem, String) & vbCrLf
        Next

        MsgBox("Results: " & vbcrlf & vbcrlf & s)
0
 

Author Comment

by:peanut1010
ID: 11918360
ok, thanks

I also have been trying to make it compare the text to what is hardcoded

example

in my listbox1 I have

Dog
Cat
Bird

I am trying to have a code that only does something when the current selection is Cat

I was tryingto do

if listbox1.items.item(o) = "Cat"

but nothing I also tried

if listbox1.selecteditem("Cat") = "Cat"

but it does work right

I need something that will only work if the current selection is hit

in vb6

I used
if listbox.text = "Cat" then

and it worked

any ideas of what to do
0
 
LVL 11

Accepted Solution

by:
coopzz earned 35 total points
ID: 11927863
       Dim oItem As Object
        Dim s As String
        For Each oItem In ListBox1.SelectedItems
            if (CType(oItem, String) = "Cat") then
                   'Run something
            end if
        Next

if you don't like the for each solution you can do it by a count as well

        Dim i As Integer
        For i = 0 To ListBox1.SelectedItems.Count - 1
            If (CType(ListBox1.SelectedItems.Item(i), String) = "Cat") Then
                MsgBox("Run something")
            End If
        Next

the reason your's wasn't working is that you were most probably comparing to an object which is what the selected item returns so you have to cast it to a string then make the comparison.

Cheers
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
What do responsible coders do? They don't take detrimental shortcuts. They do take reasonable security precautions, create important automation, implement sufficient logging, fix things they break, and care about users.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Six Sigma Control Plans

631 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question