Solved

vb.net read multiple selection is listbox

Posted on 2004-08-27
5
244 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
Comment Utility
by using the

ListBox1.SelectedItems     collection

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

Author Comment

by:peanut1010
Comment Utility
yes please
0
 
LVL 11

Expert Comment

by:coopzz
Comment Utility
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
Comment Utility
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
Comment Utility
       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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Report events not being triggered 8 98
allStar challenge 1 40
Currency Conversion? 1 34
Not needed 13 53
I know it’s not a new topic to discuss and it has lots of online contents already available over the net. But Then I thought it would be useful to this site’s visitors and can have online repository on vim most commonly used commands. This post h…
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 …
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
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 …

763 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now