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
Solved

vb.net read multiple selection is listbox

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Filename to be appended with DateTimeof Download 3 62
ejb stateless example 2 23
maven disable workspace resolution 1 21
AutoIncrement column based of FK 11 55
A short article about a problem I had getting the GPS LocationListener working.
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

856 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