CzechJura
CzechJura

Reputation: 3

VB - Put multiple items from ListBox to textbox

I need insert multiple selected items from ListBox to TextBox and if I deselect some item, it will be deleted from the text box.

Example:

  1. Name1 - selected
  2. Name2
  3. Name3 - selected

Result:

TextBox: Name1, Name3

My code:

Private Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBox1.SelectedIndexChanged
    Dim Name As String = ListBox1.SelectedItems.ToString
    TextBox3.Text = Name + ", "
End Sub

But when I select an item, so it will insert into the textbox this "System.Windows.Forms.ListBox" Can someone help me please?

Upvotes: 0

Views: 2230

Answers (2)

Paulo Lima
Paulo Lima

Reputation: 142

Try this

Private Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBox1.SelectedIndexChanged
      TextBox3.Text = ""
      For Each Name In ListBox1.SelectedItems
        TextBox3.Text += Name + ", "
      Next
End Sub

name might have the text proprety in it try Name.text or Name.Caption i don't remember very well

Upvotes: 1

unconnected
unconnected

Reputation: 1011

ListBox.SelectedItems is collection. You have iterate throw it. https://msdn.microsoft.com/en-us/library/system.windows.forms.listbox.selecteditems(v=vs.110).aspx

Try this (c# syntax):

var names = string.Join(",",ListBox1.SelectedItems.Select(x=>x.ToString())

Upvotes: 0

Related Questions