Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

vb.net read multiple selection is listbox

Posted on 2004-08-27
5
Medium Priority
?
252 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 140 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

Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

Question has a verified solution.

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

The SignAloud Glove is capable of translating American Sign Language signs into text and audio.
This article will show how Aten was able to supply easy management and control for Artear's video walls and wide range display configurations of their newsroom.
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…
Six Sigma Control Plans

715 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