2016-06-05 3 views
0

나는 그룹화해야하며 listbox1의 5 개 항목을 말하고 listbox2에 추가 할 문자열로 변환합니다."x"목록 상자 항목을 그룹화하고 다른 목록 상자에 추가하는 방법?

dim s as string="" 'a string to collect listbox1 items 
dim count as integer=Listbox1.items.count 

    Do While count > 0 
     Select Case count 

      Case Is <= 5 
       For i = 0 To ListBox1.Items.Count - 1 
        s &= ListBox1.Items.Item(i).ToString 
        ListBox1.Items.RemoveAt(i) 
       Next 
       ListBox2.Items.Add(s) 
       Exit Do 'If there are <=5 items, then done , exit loop 

      Case Is > 5 
       For i = 0 To 4 
        s &= ListBox1.Items.Item(i).ToString 
        ListBox1.Items.RemoveAt(i) 'delete each item in listbox1, after add 
       Next 
       ListBox2.Items.Add(s) 
       s = "" ' Reset the s string to receive new items 
       count = count - 5 'reduce count and loop over again 
       End Select 
Loop 

을 어떻게 든, 내가 그룹에 ListBox1 거의 항목 5의 그룹으로하고 Listbox2에 추가 할 수 있습니다, 그러나 일부 루프 (I가 있는지 확인 후에 ListBox1에이 남아 있습니다 : 다음 코드는 내가 지금까지 가지고있다 나는 8 개의 항목을 가지고있다. 위의 코드에서 내가 어디에 잘못되었는지 보여 주시겠습니까?

아래 내가 그것을 접근하는 것이 방법이다, 당신의 알고리즘은 너무 복잡하다고 생각 ~

답변

1

을 주셔서 대단히 감사합니다.

희망이 그레이엄

' 
    ' Move items from ListBox1 to ListBox2 
    ' 
    Dim s As String = "" 
    For count As Integer = 0 To ListBox1.Items.Count - 1 


     ' update every 5 

     If (count Mod 5 = 0) Then 

      ' Only update if not the first time 
      If (count <> 0) Then 
       ListBox2.Items.Add(s) 
       s = "" 
      End If 
     End If 
     s = s + ListBox1.Items.Item(count).ToString 

    Next 

    ' 
    ' Add the last ones 
    ' 

    If (s <> "") Then 
     ListBox2.Items.Add(s) 
    End If 

    ' 
    ' Clear down listbox 1 
    ' 
    ListBox1.Items.Clear() 
+0

당신은 @Graham, 너무 간단하고 깔끔한 제작하는 데 도움이됩니다. 당신의 해결책을 가져 주셔서 감사합니다 ~ –