VB - 将ListBox中的多个项目放到文本框中

时间:2015-11-11 11:39:49

标签: .net vb.net windows windows-forms-designer

我需要从ListBox中插入多个选定的项目到TextBox,如果我取消选择某个项目,它将从文本框中删除。

示例

  1. 名称1 - 已选择
  2. 名称2
  3. Name3 - 已选择
  4. 结果

    TextBox:Name1,Name3

    我的代码:

    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
    

    但是当我选择一个项目时,它会在文本框中插入“ System.Windows.Forms.ListBox ”有人可以帮我吗?

2 个答案:

答案 0 :(得分:1)

试试这个

changePosition(){
    // Take the first one
    let first = this.instances[0];
    // Reinsert the first one
    this.loader.loadIntoLocation(first.hostComponentType, this.elementRef, "container")
      .then((_) => first.dispose()); // dispose() destroys the previous instance
}

name可能包含文本proprety 试试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.text我不记得

答案 1 :(得分:0)

ListBox.SelectedItems是集合。你有迭代抛出它。 https://msdn.microsoft.com/en-us/library/system.windows.forms.listbox.selecteditems(v=vs.110).aspx

试试这个(c#语法):

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