1

我正在尝试将多个数组放入单个列表框中。下面是代码。当我让每个输出进入它自己的列表框时效果很好,但我试图将它们全部放在一个带有当前标题的列表框中。有人可以帮忙吗?最后一件事,我还希望将重复值放入另一列,但不知道如何。我所能做的就是消除重复项(想超越并显示消除了哪些数字)

Public Class Form1
    Dim randomNum As New Random()
    Dim SampleArray(19) As Integer
    Dim aryNums() As Integer = SampleArray
    Dim distinctNums = SampleArray.Distinct()

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        ListBox1.Items.Clear()
        ListBox2.Items.Clear()
        ListBox3.Items.Clear()

        For i = 0 To 19
            SampleArray(i) = randomNum.Next(10, 100)
            ListBox1.Items.Add(SampleArray(i).ToString)
        Next
        Array.Sort(SampleArray)
        For i = 0 To SampleArray.GetUpperBound(0)
            ListBox2.Items.Add(SampleArray(i))
        Next
        For Each num In distinctNums
            ListBox3.Items.Add(num.ToString())
        Next
    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        ListBox4.Items.Add("Index" & vbTab & "Random" & vbTab & "Sorted" & vbTab & "Unique")
        For i = 0 To 19
            SampleArray(i) = randomNum.Next(10, 100)
            ListBox4.Items.Add(i & vbTab & SampleArray(i).ToString)
            'ListBox4.Items.Add(SampleArray(i).ToString)
        Next
        Array.Sort(SampleArray)
        For i = 0 To SampleArray.GetUpperBound(0)
            ListBox4.Items.Add(vbTab & vbTab & SampleArray(i))
        Next
        For Each num In distinctNums
            ListBox4.Items.Add(vbTab & vbTab & vbTab & num.ToString())
        Next

    End Sub
End Class
4

2 回答 2

0

如果你想要多列listbox,最好使用listview. 将其设置为显示网格,添加标题:

ListView1.Columns.Add("header1", 80)
ListView1.Columns.Add("header2", 120)

对于您的联合列表,伪代码:

dim myList as List(of string) = new List(of string)
for each item in array1
    myList.Add(item)
next
for each item in array2
    myList.Add(item)
next
.....
' sort if necessary

for each item in myList
     dim isDuplicate as Boolean = FindIfDup(item) ' In this method iterate listview to see if the item exists
     ListView1.Items.Add(new String(){item, iif(isDuplicate, "Duplicate", "")})
next
于 2013-09-28T01:45:37.250 回答
0

虽然列表视图更容易且更恰当,但使用列表框并不难。这是一种方法:

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    ListBox4.Items.Clear()
    ListBox4.Items.Add("Index" & vbTab & "Random" & vbTab & "Sorted" & vbTab & "Unique" & vbTab & "Duplicates")
    For i = 0 To SampleArray.Count - 1
        SampleArray(i) = randomNum.Next(10, 100)
        ListBox4.Items.Add(i & vbTab & SampleArray(i).ToString)
        'ListBox4.Items.Add(SampleArray(i).ToString)
    Next
    Array.Sort(SampleArray)
    Dim distinctNums() As Integer = SampleArray.Distinct().ToArray
    For i = 0 To SampleArray.Count - 1
        ListBox4.Items(i + 1) = DirectCast(ListBox4.Items(i + 1), String) & vbTab & SampleArray(i).ToString
    Next
    For i = 0 To distinctNums.Count - 1
        ListBox4.Items(i + 1) = DirectCast(ListBox4.Items(i + 1), String) & vbTab & distinctNums(i).ToString()
    Next

    For i = 0 To SampleArray.Count - 2
        If SampleArray(i) = SampleArray(i + 1) Then
            ListBox4.Items(i + 1) = DirectCast(ListBox4.Items(i + 1), String) & vbTab & SampleArray(i).ToString()
        End If
    Next
End Sub
于 2013-09-29T03:16:41.263 回答