如何使用数组项填充列表框2

时间:2014-03-26 17:35:34

标签: vb.net

我试图用数组中的项目填充列表框,我已经声明了一个数组并分配了它保存的字符串,但我不确定我是否正确地完成了这项工作,我想使用数组中的字符串来填充一个列表框,这是我已经完成的代码,我该怎么做,任何人都可以给我代码,我可以使用这些代码填充列表框。

Dim NewDefinition As String

NewDefinition = InputBox(" Please enter definition in the box and click OK. " & " The definition entered will be added to the list. ", " Add Definition")

lstDefinitions.Items.Add(NewDefinition)

Dim NewDefinition1 As String = lstDefinitions.Items(0).ToString
Dim NewDefinition2 As String = lstDefinitions.Items(1).ToString
Dim NewDefinition3 As String = lstDefinitions.Items(2).ToString
Dim NewDefinition4 As String = lstDefinitions.Items(3).ToString
Dim NewDefinition5 As String = lstDefinitions.Items(4).ToString
Dim NewDefinition6 As String = lstDefinitions.Items(5).ToString
Dim NewDefinition7 As String = lstDefinitions.Items(6).ToString
Dim NewDefinition8 As String = lstDefinitions.Items(7).ToString
Dim NewDefinition9 As String = lstDefinitions.Items(8).ToString
Dim NewDefinition10 As String = lstDefinitions.Items(9).ToString

Dim NewDefinitions(10) As String
NewDefinitions(0) = NewDefinition1
NewDefinitions(1) = NewDefinition2
NewDefinitions(2) = NewDefinition3
NewDefinitions(3) = NewDefinition4
NewDefinitions(4) = NewDefinition5
NewDefinitions(5) = NewDefinition6
NewDefinitions(6) = NewDefinition7
NewDefinitions(7) = NewDefinition8
NewDefinitions(8) = NewDefinition9
NewDefinitions(9) = NewDefinition10

最初我想让它工作,所以我的程序中的一个表单上有一个列表框,somone使用输入框输入10个文本,然后他输入的每个文本都是一个变量字符串,然后将这些字符串分配给一个数组,这个数组用于填充不同形式的另一个列表框,虽然这样会更容易,我对阵列的知识非常有限,我必须承认,我想我让它更清晰

2 个答案:

答案 0 :(得分:0)

正如Steven Doggart所说,你可能会以错误的方式解决这个问题。第一部分应该看起来像这样:

For i As Integer = 1 To 10 Step 1
  Dim newDefinition As String = InputBox(" Please enter definition in the box and click OK.  The definition entered will be added to the list.", " Add Definition")

  lstDefinitions.Items.Add(NewDefinition)
Next

假设您始终希望用户输入10个项目。完成后,你就可以做任何你想做的10件事。

Dim newDefinitions As IEnumerable(Of String) = (From item In lstDefinitions.Items
                                                Select item.ToString())

For Each newDef As String in newDefinitions
    ' Do something with each string here
    Console.Writeline(newDef)
    ' I wrote to Console, but you can add them to another listbox or whatever.
Next

答案 1 :(得分:0)

您描述的第一步是将通过InputBox输入的项目添加到ListBox。这可以通过调用Add上的ListBox方法来实现,如下所示:

Dim NewDefinition As String
NewDefinition = InputBox(" Please enter definition in the box and click OK. " & " The definition entered will be added to the list. ", " Add Definition")
lstDefinitions.Items.Add(NewDefinition)

但是,只添加一个项目。如果您希望它们添加多个,则需要将该代码放在单独的事件处理程序中,例如“添加定义”按钮的Click事件。如果要强制它们按顺序添加固定数量的它们,您可以改为使用循环,例如:

For i As Integer = 0 to 9
    Dim NewDefinition As String
    NewDefinition = InputBox(" Please enter definition in the box and click OK. " & " The definition entered will be added to the list. ", " Add Definition")
    lstDefinitions.Items.Add(NewDefinition)
Next

一旦你拥有ListBox中的所有项目,从中读取所有项目以将它们存储在数组中相当容易。您可以使用LINQ执行此操作,如下所示:

Dim items() As String = lstDefinitions.Items.OfType(Of String).ToArray()

或者您可以使用循环,如下所示:

Dim items(lstDefinitions.Items.Count - 1) As String  ' Create the array with the correct size
For i As Integer = 0 To lstDefinitions.Items.Count   ' Loop through all of the items in the list box
    items(i) = lstDefinitions.Items(i).ToString()    ' Add the current item to the array
Next

获得数组后,可以很容易地将它们添加到第二个ListBox,如下所示:

ListBox2.Items.AddRange(items)