none
Selected items in listbox to textbox

    Question

  • I've got a bound listbox, SelectionMode is MultiSimple, ValueMember is FacultyID, DisplayMember is FacultyName.  I'm trying to pull selected items from the listbox and display in a textbox.  Here is my code:

        Dim dview As DataRowView
        For Each dview In lstFaculty.SelectedItems
          txtSelectedFaculty.Text &= txtSelectedFaculty.Text + DirectCast(lstFaculty.SelectedItem, DataRowView)("FacultyName").ToString() & vbCrLf
        Next
    

    I'm not getting the results I want.  If I've selected "Mickey Mouse" and "Donald Duck", then I get "Mickey Mouse" 3 times in the textbox.  If I select "Mickey Mouse", "Donald Duck", and "Foghorn Leghorn", then "Mickey Mouse" gets displayed 4 times in the textbox. 

    What am I doing wrong?  Any help or advice is appreciated.

    Tuesday, July 06, 2010 3:05 PM

Answers

  • Hi,

    if you use lstFaculty.SelectedItem you will get always the first select item in the listbox. So try the following:

    'Visual Basic 2008 - .net 2.0 - Any CPU
            For Each Item As Object In lstFaculty.SelectedItems
                txtSelectedFaculty.Text &= 
    DirectCast(Item, DataRowView)("FacultyName").ToString() & vbCrLf
            
    Next


    Hannes

    If you have got questions about this, just ask.
    Mark the thread as answered if the answer helps you. This helps others who have the same problem !
    C# to VB.NET: http://www.developerfusion.com/tools/convert/csharp-to-vb/
    • Marked as answer by db_dweeb Wednesday, July 07, 2010 2:26 PM
    Wednesday, July 07, 2010 2:10 PM

All replies

  • Hi kris_hood,

    If each item in your LISTBOX shows more than one piece of information you will

    get each item as shown in the LISTBOX on a separate line in your TextBox.

    Unless you convert each item to STRING format and then split the text.

    However try this.>>

     

     Dim dview As DataRowView
     For Each dview In lstFaculty.SelectedItems
      txtSelectedFaculty.Text &= dview.ToString & vbCrLf
     Next

     


    Regards, John : Trying hard for another goal into the .Net!!
    Tuesday, July 06, 2010 3:37 PM
  • Thanks for your help.  Now I'm getting "...DataRowView" displayed in the textbox instead of FacultyName.  However, if I select 2 names, then I get 2 "...DataRowView" in the textbox.  So half my problem is solved.

    From what I've researched, I have to use DirectCast to display the DisplayName or else I'll see "...DataRowView".

    Tuesday, July 06, 2010 3:46 PM
  • Hi kris_hood,

    How many items are there in each dview?

    See this as one item i.e "FacultyName" I hope this helps you further.

     

    DirectCast(lstFaculty.SelectedItem, DataRowView)("FacultyName").ToString

     

    See.>>

     Dim dview As DataRowView
      For Each dview In lstFaculty.SelectedItems
       txtSelectedFaculty.Text &= DirectCast(lstFaculty.SelectedItem, DataRowView)("FacultyName").ToString & vbCrLf
      Next


    Regards, John : Trying hard for another goal into the .Net!!
    Wednesday, July 07, 2010 2:31 AM
  • I hope I'm answering your question correctly about the amount of items in dview.  If I select 2 items, then dview has 2 items.  The listbox itself is bound to an Access 2007 table that currently has 26 items in it.  But that can change if the user adds or deletes from the table.

    I've tried the DirectCast code you've given me before and it returns the 1st item selected repeated however for however many items I've selected.  For example, if I choose "Mickey Mouse" and "Donald Duck", then "Mickey Mouse" is returned twice. 

    So again, half my problem is solved.  I'm getting the same number of items in the textbox as what I select, which is good, but the correct items are not displayed. 

    Thanks for your help.  Very much appreciated.

    • Proposed as answer by Stupot008 Friday, September 09, 2011 7:21 PM
    Wednesday, July 07, 2010 1:28 PM
  • Hi,

    if you use lstFaculty.SelectedItem you will get always the first select item in the listbox. So try the following:

    'Visual Basic 2008 - .net 2.0 - Any CPU
            For Each Item As Object In lstFaculty.SelectedItems
                txtSelectedFaculty.Text &= 
    DirectCast(Item, DataRowView)("FacultyName").ToString() & vbCrLf
            
    Next


    Hannes

    If you have got questions about this, just ask.
    Mark the thread as answered if the answer helps you. This helps others who have the same problem !
    C# to VB.NET: http://www.developerfusion.com/tools/convert/csharp-to-vb/
    • Marked as answer by db_dweeb Wednesday, July 07, 2010 2:26 PM
    Wednesday, July 07, 2010 2:10 PM
  • Yes!  Thank you, Hannes!  That worked just fine.  Just before I read your post, I found this snippet of code that seems to work just fine:

    For i As Integer = 0 To lstFaculty.SelectedItems.Count - 1
        txtSelectedFaculty.Text &= DirectCast(lstFaculty.SelectedItems(i), DataRowView)(1).ToString & vbCrLf
    Next
    

    Your code appears to be a little cleaner and easier to understand.

    Thanks.

    Wednesday, July 07, 2010 2:26 PM