?
Solved

Detecting multiple items in a listbox - windows forms

Posted on 2004-10-03
1
Medium Priority
?
158 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 500 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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses
Course of the Month15 days, 2 hours left to enroll

770 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