Solved

Detecting multiple items in a listbox - windows forms

Posted on 2004-10-03
1
153 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
1 Comment
 
LVL 85

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
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 …
This is an introductory video for CloudBerry Managed Backup. You will learn how to sign up with the service and get started in a few minutes.
Both in life and business – not all partnerships are created equal. Spend 30 short minutes with us to learn:   • Key questions to ask when considering a partnership to accelerate your business into the cloud • Pitfalls and mistakes other partners…

911 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

22 Experts available now in Live!

Get 1:1 Help Now