我有两个按钮,一个文本框和一个列表框。我在一个显示在列表框中的文本框中输入了25个数字。一些我如何从该列表框创建一个数组名称,并按照订单显示在另一个列表框中。这是我能想到的最后一步。任何建议都会有所帮助
评论代码
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
If ListBox5Box.Items.Count < 20 Then 'this works
MessageBox.Show("Exactly Twenty Numbers Must Be Entered")
ListBox6.Items.Add(GoLstBox5.Text)'this does not work nothing comes over to listbox6
Dim beerArray(19) As Integer beerArray(19) = GoLstBox.Text Array.Sort(beerArray)'this will work once the other works
For i = 0 To beerArray.GetUpperBound(0)
ListBox6.Items.Add(beerArray(i).ToString)
Next
我现在有了这个,但没有任何内容显示在新的列表框中
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
If ListBox1.Items.Count < 20 Then
MessageBox.Show("Exactly Twenty Numbers Must Be Entered")
' ListBox6.Items.Add(GoLstBox.Text)
Dim beerArray(ListBox1.Items.Count - 1) As Object
Listbox1.Items.CopyTo(beerArray, 0)
Array.Sort(beerArray)
For i = 0 To beerArray.GetUpperBound(0)
ListBox6.Items.AddRange(beerArray)
Next
End If
End Sub
...
答案 0 :(得分:0)
您可以使用ListBox.ObjectCollection.CopyTo Method将Listbox.Items检索到数组中。 您会注意到我将beerArray从整数数组更改为Object数组,这是因为一旦项目作为对象存储在ListBox中。因此,除了创建一个函数来处理Items并返回它们之外,这将是最佳选择。
Dim beerArray(ListBox1.Items.Count - 1) As Object
ListBox1.Items.CopyTo(beerArray, 0)
'Manipulate your array here
'Then add it to your Second ListBox
ListBox2.Items.AddRange(beerArray)
根据OP添加的代码进行编辑
如果您的If语句错误,如果您的第一个ListBox的项目数为20或更大,则不会填充您的第二个ListBox。你需要做这样的事情。
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
If ListBox1.Items.Count <> 20 Then
MessageBox.Show("Exactly Twenty Numbers Must Be Entered")
Else
Dim beerArray(ListBox1.Items.Count - 1) As Object
ListBox1.Items.CopyTo(beerArray, 0)
Array.Sort(beerArray)
For i = 0 To beerArray.GetUpperBound(0)
ListBox6.Items.Add(beerArray(i))
Next
'If you want to use the AddRange you do not need the above For Loop Just use this
'ListBox6.Items.AddRange(beerArray)
End If
End Sub