Solved

Detecting multiple items in a listbox - windows forms

Posted on 2004-10-03
1
152 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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

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…
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

707 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

16 Experts available now in Live!

Get 1:1 Help Now