如何将列表框的所有项目转换为文本框

时间:2014-05-04 14:12:25

标签: vb.net visual-studio-2010

如何将Listbox1Listbox2项转换为Textbox。按钮按..

转换格式:>

  

{Listbox1.item1 / listbox2.item1},{listbox1.item2 / listbox2.item2},   {listbox1.item3 / listbox2.item3}

依旧......

我尝试了许多代码,但没有工作......“

   Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    For Each ListBoxLine1 As Object In ListBox2.Items

        For Each ListBoxLine As Object In ListBox1.Items
            TextBox1.AppendText(ListBoxLine.ToString & " / " & ListBoxLine1.ToString)
        Next ListBoxLine
    Next ListBoxLine1
End Sub

1 个答案:

答案 0 :(得分:0)

您必须根据有多少列表框修改此内容。这种方法只是创建一个字符串数组,交错各种LB项,然后在构造后将其发布到TB。

Private Sub Button1_Click(...

   Dim strText As String()              ' temp array

   ' get first listbox items as a string array:
   strText = ListBox1.Items.Cast(Of String).ToArray

     ' rebuild array for each ListBox:
   strText = AddListItemsToArray(strText, ListBox2)
   strText = AddListItemsToArray(strText, ListBox3)
   ' etc

   ' post finished array to ML TB:
   tb.Lines = strText

End Sub

然后一个小函数来修改每个LB的数组。您可以更改此设置以一次性增加所有LB的数组(您必须考虑计数变量的所有LB)但我不知道有多少LB.这也可以用作Sub。

Private Function AddListItemsToArray(strT() As String, lb As ListBox) As String()
    ' use smallest size for loop index
    Dim count As Integer = Math.Min(strT.Count, lb.Items.Count) - 1

    For n As Integer = 0 To count
        strT(n) &= " / " & lb.Items(n).ToString
    Next

    Return strT
End Sub