Results 1 to 16 of 16

Thread: How can i multi select items in listbox and pass values to a textbox

  1. #1

    Thread Starter
    Member
    Join Date
    Oct 2012
    Posts
    38

    How can i multi select items in listbox and pass values to a textbox

    Please I need help.

    I have a listbox that has multiselect property.

    I am having problems writing code to help me multi select items in the listbox and on a click event, items will display on a textbox that has a multi line property.

    Thanks in advance

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: How can i multi select items in listbox and pass values to a textbox

    Um, you posted this exact same question in the VB6 forum a couple of hours ago. Firstly, please don't double post. If you find that you have posted in the wrong forum then please ask the moderators to move the thread. VB6 and VB.NET are two different languages so if people start posting to both threads then at least one group is going to be wasting their time. That's not the way to make friends.

    So, which language are you using? Is it VB6 or VB.NET? Note that VB 2005 and later are VB.NET. We can then get the mods to delete the irrelevant thread and people can try to help you out, safe in the knowledge that there answers will be relevant and their time not wasted.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Member
    Join Date
    Oct 2012
    Posts
    38

    Re: How can i multi select items in listbox and pass values to a textbox

    sorry about that

    VB.NET

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: How can i multi select items in listbox and pass values to a textbox

    Assuming that the items in the ListBox are Strings in the first place, the simplest option is:
    Code:
    myTextBox.Lines = myListBox.SelectedItems.Cast(Of String)().ToArray()
    That creates a String array from the selected items and displays it one element per line in the TextBox.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Member
    Join Date
    Oct 2012
    Posts
    38

    Re: How can i multi select items in listbox and pass values to a textbox

    I tried your code and this error came up

    Unable to cast object of type 'System.Data.DataRowView' to type 'System.String

    Please help

  6. #6
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,414

    Re: How can i multi select items in listbox and pass values to a textbox

    try this:

    vb.net Code:
    1. Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
    2.     If ListBox1.DisplayMember = "" Then Return
    3.     TextBox1.Lines = ListBox1.SelectedItems.Cast(Of DataRowView).Select(Function(drv) drv.Item(ListBox1.DisplayMember).ToString).ToArray()
    4. End Sub

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: How can i multi select items in listbox and pass values to a textbox

    Quote Originally Posted by smile4kenny View Post
    I tried your code and this error came up

    Unable to cast object of type 'System.Data.DataRowView' to type 'System.String

    Please help
    Given that I said:
    Assuming that the items in the ListBox are Strings in the first place
    that's not a surprise.
    Quote Originally Posted by .paul. View Post
    try this:

    vb.net Code:
    1. Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
    2.     If ListBox1.DisplayMember = "" Then Return
    3.     TextBox1.Lines = ListBox1.SelectedItems.Cast(Of DataRowView).Select(Function(drv) drv.Item(ListBox1.DisplayMember).ToString).ToArray()
    4. End Sub
    There's a better approach that will work in all cases:
    Code:
    TextBox1.Lines = ListBox1.SelectedItems.
                              Cast(Of Object).
                              Select(Function(o) ListBox1.GetItemText(o)).
                              ToArray()
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  8. #8

    Thread Starter
    Member
    Join Date
    Oct 2012
    Posts
    38

    Re: How can i multi select items in listbox and pass values to a textbox

    Okay thanks bro...

    I modified your code and it worked this way

    For Each objDataRowView As DataRowView In Me.ClassTitleListBox.SelectedItem
    Me.TextBox1.Text &= (objDataRowView("ClassTitle").ToString() & ",")

    Next

    But, If I wanna save the output to an array instead of displaying on a textbox, do you know how I can do it?

    Thanks in advance

  9. #9
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,104

    Re: How can i multi select items in listbox and pass values to a textbox

    Rather than an array, use a List(of String). Your inner line changes from:

    Me.TextBox1.Text &= (objDataRowView("ClassTitle").ToString() & ",")

    into

    whateverMyListIsCalled.Add(objDataRowView("ClassTitle").ToString)

    You just have to declare a New List(of String) with whatever name is better than what I used as a name.
    My usual boring signature: Nothing

  10. #10

    Thread Starter
    Member
    Join Date
    Oct 2012
    Posts
    38

    Re: How can i multi select items in listbox and pass values to a textbox

    Hi Shaggy,

    what if I am using this code, how can I store the output as a list of string
    If CourseTitleListBox.DisplayMember = "" Then Return
    TextBox1.Lines = CourseTitleListBox.SelectedItems.Cast(Of DataRowView).Select(Function(drv) drv.Item(CourseTitleListBox.DisplayMember).ToString).ToArray()

  11. #11

    Thread Starter
    Member
    Join Date
    Oct 2012
    Posts
    38

    Re: How can i multi select items in listbox and pass values to a textbox

    Please someone help!

    I need to store the output in an array or string with comma seperating each item, because I will run an sql query that will search each of the stored item with a where statement.

  12. #12
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,104

    Re: How can i multi select items in listbox and pass values to a textbox

    The code you just showed already returns an array of string. That's what the .ToArray is. Alternatively, you could still use a list by changing that to .ToList, but since you are using that code, I see no reason to do that. TextBox1.Lines is an array of string. You could also have your own array of strings:

    Dim MyArray() As String

    and assign to that rather than assigning to TextBox1.Lines.
    My usual boring signature: Nothing

  13. #13

    Thread Starter
    Member
    Join Date
    Oct 2012
    Posts
    38

    Re: How can i multi select items in listbox and pass values to a textbox

    Thanks Shaggy I really do appreciate.

    Instead of using an array,
    do you know how I can store this as a string with comma seperating each items

  14. #14

    Thread Starter
    Member
    Join Date
    Oct 2012
    Posts
    38

    Re: How can i multi select items in listbox and pass values to a textbox

    When I assign to Dim MyArray() As String

    The output is system.String[]

  15. #15
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,414

    Re: How can i multi select items in listbox and pass values to a textbox

    dim outPut as string = string.join(",", CourseTitleListBox.SelectedItems.Cast(Of DataRowView).Select(Function(drv) drv.Item(CourseTitleListBox.DisplayMember).ToString).ToArray())

  16. #16

    Thread Starter
    Member
    Join Date
    Oct 2012
    Posts
    38

    Re: How can i multi select items in listbox and pass values to a textbox

    Thanks paul!

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