Wrox Programmer Forums
|
VB.NET 2002/2003 Basics For coders who are new to Visual Basic, working in .NET versions 2002 or 2003 (1.0 and 1.1).
Welcome to the p2p.wrox.com Forums.

You are currently viewing the VB.NET 2002/2003 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 17th, 2003, 05:22 PM
Registered User
 
Join Date: Nov 2003
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default Listbox multiselections

New to VB.NET

How do I capture multi selections from a listbox?
The listbox is loaded with results of a query from a SQL Server 2000 database.
I have tried everything I can think of, plus every hint I have found on the internet. I can only capture the first selection, the second and third and so on always have the vale of the first selection found.
I have been able to do this in VB6 but not VB.NET.

I would really appreciate any answers or hints.
thanks, Michael


 
Old November 17th, 2003, 05:42 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,101
Thanks: 0
Thanked 2 Times in 2 Posts
Default

Directly from the help file...

Quote:
quote:
When multiple items are selected, the SelectedIndex value reflects the selected item that appears first in the list.
Quote:
quote:
To retrieve a collection containing the indexes of all selected items in a multiple-selection ListBox, use the SelectedIndices property. If you want to obtain the item that is currently selected in the ListBox, use the SelectedItem property. In addition, you can use the SelectedItems property to obtain all the selected items in a multiple-selection ListBox.
Quote:
quote:
SelectedIndices Gets a collection that contains the zero-based indexes of all currently selected items in the ListBox.

Property Value
A ListBox.SelectedIndexCollection containing the indexes of the currently selected items in the control. If no items are currently selected, an empty ListBox.SelectedIndexCollection is returned.

Remarks
For a multiple-selection ListBox, this property returns a collection containing the indexes to all items that are selected in the ListBox. For a single-selection ListBox, this property returns a collection containing a single element containing the index of the only selected item in the ListBox. For more information on how to manipulate the items of the collection, see ListBox.SelectedIndexCollection.

The ListBox class provides a number of ways to reference selected items. Instead of using the SelectedIndices property to obtain the index position of the currently selected item in a single-selection ListBox, you can use the SelectedIndex property. If you want to obtain the item that is currently selected in the ListBox, instead of the index position of the item, use the SelectedItem property. In addition, you can use the SelectedItems property if you want to obtain all the selected items in a multiple-selection ListBox.

Hal Levy
Web Developer, PDI Inc.

NOT a Wiley/Wrox Employee
 
Old November 18th, 2003, 11:07 AM
Registered User
 
Join Date: Nov 2003
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks Hal

I had tried the SelectedIndices, but I would only get the first selected value.
When I test run this, I select 3 items in the listbox when I run the program. I get 3 copies of the first items value.


        Dim myPieceOfData As String
        Dim myIndex As Integer = 0

        For Each myIndex In Me.lbx201CaseStatus.SelectedIndices
            If myIndex > 0 Then
                myPieceOfData = myPieceOfData + Me.lbx201CaseStatus.SelectedValue
            End If
        Next




 
Old November 20th, 2003, 08:58 PM
Registered User
 
Join Date: Nov 2003
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Search the selected items collection.

Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
        PizzaListBox.Items.Add("Cheese")
        PizzaListBox.Items.Add("Pepperoni")
        PizzaListBox.Items.Add("Mushrooms")
        PizzaListBox.Items.Add("Sausage")
        PizzaListBox.Items.Add("Onions")
        PizzaListBox.Items.Add("Peppers")
        PizzaListBox.Items.Add("Olives")
        PizzaListBox.Items.Add("Anchovies")
        PizzaListBox.Items.Add("Pineapple")
    End Sub

    Private Sub OrderButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OrderButton.Click
        Dim intNum As Integer
        Dim strAnswer As String
        'Search the SelectedItems Collection
        For intNum = 0 To PizzaListBox.SelectedItems.Count - 1
            strAnswer = strAnswer & " " & PizzaListBox.SelectedItems(intNum)
        Next intNum
End Sub

 
Old April 28th, 2005, 11:49 AM
Registered User
 
Join Date: Apr 2005
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I've been scrounging all over for the answer to this question, and no one's posted it, so I will.

The trick is that the above suggestions dont work when you have the listbox bound to a datasource. It see the value of the listbox as a datarowview. Here's how I solved it:


        Dim x As Integer
        Dim itemvalue As DataRowView
        Dim targettext As String

        For x = 0 To lstBox.SelectedItems.Count - 1
            itemvalue = lstBox.SelectedItems(x)
            targettext = itemvalue.Item("ColumnName")
        Next


Hope this helps somebody.
Usarian M. Skiff

Usarian M. Skiff
 
Old May 24th, 2006, 08:50 AM
Registered User
 
Join Date: May 2006
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Dude, I have been looking all over the net for this and there is not a mention about a bound listbox until I came across this article by luck. Thanks so much it was a great help now I can move forward.

-rjp

 
Old October 30th, 2006, 08:49 AM
Registered User
 
Join Date: Oct 2006
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hay guys... i was having the same problem trying to figure out a way to get the items out of a listbox and place it in a string. I figured out a way to do so that might be a bit crued but hopefully you guys will find it a little friendly... :)


Presume that your Listbox is "lstTrace"

Code:
        Dim intNumBer As Integer = lstTrace.Items.Count - 1
        Dim strCollect As String = ""
        Dim a As Integer
        Try
            For a = 0 To intNumBer Step 1
                Dim b As Integer
                For b = 0 To lstTrace.SelectedItems.Count
                    Try
                        If lstTrace.Items.Item(a) Is lstTrace.SelectedItems.Item(b) Then
                            strCollect += lstTrace.Items.Item(a) & Chr(10)
                        End If
                    Catch
                          'Null
                    End Try
                Next
            Next
        Catch
               'Null
        End Try
        If strCollect = "" Then
                 strCollect = "Item Not Selected"
        End If
        MessageBox.Show(strCollect)


There Ya go... Hope it helps ya :D





Similar Threads
Thread Thread Starter Forum Replies Last Post
multiple Listbox values in another listbox terryv Excel VBA 0 June 27th, 2007 07:01 AM
Help with listbox MMJiggy62 Beginning VB 6 5 July 11th, 2006 10:56 PM
About listbox csc820203 C# 2 May 1st, 2004 01:32 AM
About listbox csc820203 C# 1 April 29th, 2004 06:17 AM
I'm back :) Listbox var from listbox MichaelTJ .NET Web Services 2 October 21st, 2003 07:06 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.