Results 1 to 8 of 8

Thread: [RESOLVED] [2005] Get SeletedValues from ListBox with Multi-Select

  1. #1

    Thread Starter
    Frenzied Member bmahler's Avatar
    Join Date
    Oct 2005
    Location
    Somewhere just west of the Atlantic
    Posts
    1,568

    Resolved [RESOLVED] [2005] Get SeletedValues from ListBox with Multi-Select

    Ok, so I populate a listbox with a dataset like so

    vb Code:
    1. lbNucs.DataSource = ds.Tables(0)
    2. lbNucs.DisplayMember = "Analyte"
    3. lbnucs.ValueMember = "AnalytePK"

    This works fine, but... I am trying to return the values of multiple selected items. It seems to only return the value of the first selected item in the group. Is it possible to do this by this method?

    I have tried setting the SelectedIndex and it still only returns the value of the first item. any help would be appreciated.
    Last edited by bmahler; Apr 4th, 2007 at 11:37 AM.
    Boooya
    • Visual Studio 2008 Professional
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • Don't forget to rate helpful posts!
    • If you're question was answered please mark your thread [Resolved]


    Code Contributions:
    PHP
    PHP Image Gallery v1.0PHP Image Gallery v2.0
    VB 2005
    Find Computers on a networkSimple License EncryptionSQL Server Database Access dllUse Reflection to Return Crystal ReportDocumentSilently Print PDFGeneric Xml Serailizer


    Useful Links: (more to come)
    MSDN (The first and foremost)MSDN Design Guidelines API Reference • Inno Setup CompilerInno Setup PreprocessorISTool - Fairly easy to use GUI for creating inno setup projects • Connection StringsNAnt -Automated BuildsCruise Control .NET - Frontend for automated builds

  2. #2
    Fanatic Member bgmacaw's Avatar
    Join Date
    Mar 2007
    Location
    Atlanta, GA USA
    Posts
    524

    Re: [2005] Get SeletedValues from ListBox with Multi-Select

    See SelectedIndexCollection

  3. #3

    Thread Starter
    Frenzied Member bmahler's Avatar
    Join Date
    Oct 2005
    Location
    Somewhere just west of the Atlantic
    Posts
    1,568

    Re: [2005] Get SeletedValues from ListBox with Multi-Select

    That only returns the indexes that are selected, I need to get the value of each selected index. I came up with a way to do it, but it seems like a real work around and there must be a better way to accomplish this.

    vb Code:
    1. Dim intNucPk As Integer
    2.  
    3.         'A list to hold the indexes
    4.         Dim ids As New List(Of Integer)
    5.         'Add the selected indexes to the list
    6.         For i As Integer = 0 To Me.lbNucs.SelectedIndices.Count - 1
    7.             ids.Add(Me.lbNucs.SelectedIndices(i))
    8.         Next
    9.         'Set the selected Item to nothing
    10.         lbNucs.SelectedItem = Nothing
    11.  
    12.         'Loop through the selected indicies
    13.         For i As Integer = 0 To ids.Count - 1
    14.             'Set the selected index
    15.             lbNucs.SelectedIndex = ids.Item(i)
    16.             'Get the value
    17.             intNucPk = lbNucs.SelectedValue
    18.             'Add the nuclide to the analysis code
    19.             tryAddNuc(intNucPk)
    20.             'Set the selected item to nothing
    21.             lbNucs.SelectedItem = Nothing
    22.         Next
    23.         'Repopulate the listbox with the new values
    24.         getNucsForAC()
    Boooya
    • Visual Studio 2008 Professional
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • Don't forget to rate helpful posts!
    • If you're question was answered please mark your thread [Resolved]


    Code Contributions:
    PHP
    PHP Image Gallery v1.0PHP Image Gallery v2.0
    VB 2005
    Find Computers on a networkSimple License EncryptionSQL Server Database Access dllUse Reflection to Return Crystal ReportDocumentSilently Print PDFGeneric Xml Serailizer


    Useful Links: (more to come)
    MSDN (The first and foremost)MSDN Design Guidelines API Reference • Inno Setup CompilerInno Setup PreprocessorISTool - Fairly easy to use GUI for creating inno setup projects • Connection StringsNAnt -Automated BuildsCruise Control .NET - Frontend for automated builds

  4. #4
    Frenzied Member circuits2's Avatar
    Join Date
    Sep 2006
    Location
    Kansas City, MO
    Posts
    1,027

    Re: [2005] Get SeletedValues from ListBox with Multi-Select

    .SelectedItem should be a collection with a zero-based index. Did you try looping through .SelectedItem(index).Value?
    Show the love! Click (rate this post) under my name if I was helpful.

    My CodeBank Submissions: How to create a User Control | Move a form between Multiple Monitors (Screens) | Remove the MDI Client Border | Using Report Viewer with Visual Studio 2012 Express

  5. #5
    Fanatic Member bgmacaw's Avatar
    Join Date
    Mar 2007
    Location
    Atlanta, GA USA
    Posts
    524

    Re: [2005] Get SeletedValues from ListBox with Multi-Select

    SelectedItems might be what you're looking for. It still requires a loop though

    vb Code:
    1. For Each SelectedItem As String In ListBox1.SelectedItems
    2.     ListBox2.Items.Add(SelectedItem)
    3. Next

    Of course, the "As String" would need to be whatever object you were storing in the listbox.

    It's too bad the following doesn't work

    vb Code:
    1. ListBox2.Items.AddRange(ListBox1.SelectedItems)

    But for some reason the MS .NET Team decided to make ListBox.ObjectCollection and ListBox.SelectedObjectCollection incompatible.

  6. #6

    Thread Starter
    Frenzied Member bmahler's Avatar
    Join Date
    Oct 2005
    Location
    Somewhere just west of the Atlantic
    Posts
    1,568

    Re: [2005] Get SeletedValues from ListBox with Multi-Select

    .SelectedItem(index).Value doe snot work. There is no value property for that.

    @bgmacaw I guess you did not read my original post. I am looking to return the valuemember of each selected Item, not the index. The index is easy, but I need the ValueMember of that index. I have actually done this, by deselecting each item after it is added like so.
    vb Code:
    1. Dim intNucPk As Integer
    2.  
    3.         'A list to hold the indexes
    4.         Dim ids As New List(Of Integer)
    5.         'Add the selected indexes to the list
    6.         For i As Integer = 0 To Me.lbNucs.SelectedIndices.Count - 1
    7.             ids.Add(Me.lbNucs.SelectedIndices(i))
    8.         Next
    9.         'Loop through the selected indicies
    10.         For i As Integer = 0 To ids.Count - 1
    11.             intNucPk = lbNucs.SelectedValue
    12.             'Add the nuclide to the analysis code
    13.             tryAddNuc(intNucPk)
    14.             'Deselect the item
    15.             lbNucs.SetSelected(ids(i), False)
    16.         Next
    17.         'Repopulate the listbox with the new values
    18.         getNucsForAC()

    Seems like the only way to get the SelectedValue of the next item.

    anyway, marking this thread resolved, because I found a solution.
    Boooya
    • Visual Studio 2008 Professional
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • Don't forget to rate helpful posts!
    • If you're question was answered please mark your thread [Resolved]


    Code Contributions:
    PHP
    PHP Image Gallery v1.0PHP Image Gallery v2.0
    VB 2005
    Find Computers on a networkSimple License EncryptionSQL Server Database Access dllUse Reflection to Return Crystal ReportDocumentSilently Print PDFGeneric Xml Serailizer


    Useful Links: (more to come)
    MSDN (The first and foremost)MSDN Design Guidelines API Reference • Inno Setup CompilerInno Setup PreprocessorISTool - Fairly easy to use GUI for creating inno setup projects • Connection StringsNAnt -Automated BuildsCruise Control .NET - Frontend for automated builds

  7. #7
    Fanatic Member bgmacaw's Avatar
    Join Date
    Mar 2007
    Location
    Atlanta, GA USA
    Posts
    524

    Re: [2005] Get SeletedValues from ListBox with Multi-Select

    Quote Originally Posted by bmahler
    @bgmacaw I guess you did not read my original post. I am looking to return the valuemember of each selected Item, not the index. The index is easy, but I need the ValueMember of that index.
    Sorry I missed that when I read your post. I didn't catch on that you meant ValueMember when you said value. I'm glad you resolved it.

    Here's another way I found to do it:

    vb Code:
    1. For Each SelectedItem As DataRowView In ListBox1.SelectedItems
    2.     ListBox2.Items.Add(SelectedItem("AnalytePK"))
    3. Next

    When you databind the control it puts a DataRowView object in the Listbox's Items collection so you can access any members of a row in the original DataTable that way.

  8. #8

    Thread Starter
    Frenzied Member bmahler's Avatar
    Join Date
    Oct 2005
    Location
    Somewhere just west of the Atlantic
    Posts
    1,568

    Re: [RESOLVED] [2005] Get SeletedValues from ListBox with Multi-Select

    Thanks for that one, that is definitely more efficient than my method
    Boooya
    • Visual Studio 2008 Professional
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • Don't forget to rate helpful posts!
    • If you're question was answered please mark your thread [Resolved]


    Code Contributions:
    PHP
    PHP Image Gallery v1.0PHP Image Gallery v2.0
    VB 2005
    Find Computers on a networkSimple License EncryptionSQL Server Database Access dllUse Reflection to Return Crystal ReportDocumentSilently Print PDFGeneric Xml Serailizer


    Useful Links: (more to come)
    MSDN (The first and foremost)MSDN Design Guidelines API Reference • Inno Setup CompilerInno Setup PreprocessorISTool - Fairly easy to use GUI for creating inno setup projects • Connection StringsNAnt -Automated BuildsCruise Control .NET - Frontend for automated builds

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width