Solved

vb.net read multiple selection is listbox

Posted on 2004-08-27
5
246 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
  • 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

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

831 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