1
私はランダムな項目を持つリストボックスを持っています。私は2つの部分に分割し、各部分を1つのリストボックスに入れたいと思っていました。私はそれをすることができましたが、私は非常に面倒なコードを手に入れました、オンラインで助けを見つけることができなかったので、それを行う別の方法があるかどうか尋ねたいだけです。ここに私のコードです。リストボックスの項目を2つの部分に分割する
Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
'items count
Dim count_listbox1 As Integer = ListBox1.Items.Count - 1
'half the count - 1 (im going to use it on the for loop)
Dim metade_listbox1_1 As Integer = (count_listbox1/2) - 1
'half the count
Dim metade_listbox1_2 As Integer = (count_listbox1/2)
' (first part of the listbox)
For i = 0 To metade_listbox1_1
list1.Items.Add(ListBox1.Items.Item(i)) 'list1 - listbox that contains 1 half
Next
' (second part of the listbox items)
For i = metade_listbox1_2 To count_listbox1
list2.Items.Add(ListBox1.Items.Item(i)) 'list2 - listbox that contains 2 half
Next
'check if number of items its even or odd, if odd, deletes the first item of the list2,
'because its the same as the last item from list1
If eo = False Then
list2.Items.Remove(list2.Items.Item(0))
End If
End Sub
最初のコードはうまくいきませんでした。もう1つは、 'AddRange'がリストボックスのメンバーではないというエラーが表示されます。私はSystem.Linqをインポートしました –
"あまり良くありません"とは何ですか?順序はアイテムを1つずつ配布するのとは異なりますが、あなたのアイテムはランダムであるとは限りません。 2番目の例を更新しました。コードの一部が見つかりませんでした。 – DAXaholic
実際には最初のコードは動作していますが、数分前に試しましたが、それは悪くはありませんでした。それはうまく働いています、助けてくれてありがとう、より多くの専門家。 –