Solved

Detecting multiple items in a listbox - windows forms

Posted on 2004-10-03
1
157 Views
Last Modified: 2008-03-17
I don't know why this isn't working for me, but it isn't , and I need some advice.

I have a windows forms app with a ListBox. The examples in the MSDN documentation say to load it up with ListItems - but I can't find a ListItem object in Windows Forms. I did find one in System.Web.UI.WebControls, so I added that, and build my ListBox like this:

            Dim l As ListItem = New ListItem("MyItem")
            ListBox1.Items.Add(l)

I do this for each of several items, and that loads the listbox.

The listbox has the multiselect property on. So I select several items, and then try to detect them like this:

                For Each li As ListItem In lstStyles.Items
                    If li.Selected Then
                    ..do something
                    end if
                Next

But it never detects a selected item. Never. So what gives? What am I doing wrong?
0
Comment
Question by:toddhd
[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
1 Comment
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 125 total points
ID: 12214366
   Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim i As Short

        For i = 1 To 10
            ListBox1.Items.Add("Item" & i)
        Next
        ListBox1.SelectionMode = SelectionMode.MultiSimple
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim s As String

        For Each s In ListBox1.SelectedItems()
            Debug.WriteLine(s)
        Next
    End Sub
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

728 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