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
첫 번째 코드는 제대로 작동하지 않으며, 두 번째로 'AddRange'가 listbox의 멤버가 아니라는 오류가 발생합니다. System.Linq을 가져 왔습니다. –
"별로 좋지 않습니다"는 무엇입니까? 순서는 항목을 하나씩 배포 할 때와 다르지만 어쨌든 항목이 임의적이어서 중요하지 않습니다. 두 번째 예제를 업데이트했습니다. 코드 일부가 누락되었습니다. – DAXaholic
사실 첫 번째 코드가 작동 중입니다. 몇 분 전에 시도했지만 그렇지 않았습니다. 도움을 주셔서 감사합니다. 훨씬 전문적입니다. –