Results 1 to 3 of 3

Thread: Distributing items across several listboxes.

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2007
    Posts
    520

    Distributing items across several listboxes.

    Hey guys, long time since I posted here. I've been working in C#, and have yet to find a decent *ACTIVE* C# forum for help...



    So, heres my scenario:

    ListboxQueue 1: 80 Items. (Queue)

    ListboxWork 1(through 6): 0 Items (Work 2 be done)



    ListBoxWork is actually sitting inside of a List(of Listbox).



    What is the best way to distribute each item from the queue into the work (with a max of 8 per LB)...

    Now, I could do a lot of if statements, but I would like it to use the List(of listbox) to do the spread...

    Now, I have 80 items, 6 "workers"...with a max of 8 per worker, it'd come out to 48...which means i'd like to leave the last 32 items in the queue.



    (I know I stated this is for C#, but I also know how to convert VB as well)


    Thanks for the solution.


    I am not necessarily looking for a code snippet, but more so a direction.

  2. #2
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: Distributing items across several listboxes.

    For i = 0 To 41 Step 6:For j = 0 to 5: From LBQ i+J to Listbox i
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  3. #3
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: Distributing items across several listboxes.

    try this:

    Code:
    Public Class Form1
    
        Dim listBoxes() As ListBox
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            listBoxes = New ListBox() {ListboxWork1, ListboxWork2, ListboxWork3, ListboxWork4, ListboxWork5, ListboxWork6}
            ListboxQueue.Items.AddRange(Enumerable.Range(1, 80).Select(Function(x) x.ToString).ToArray)
        End Sub
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim items() As String = ListboxQueue.Items.Cast(Of String).ToArray
            For x As Integer = 0 To 5
                listBoxes(x).Items.AddRange(items.Skip(x * 8).Take(8).ToArray)
                For x2 As Integer = 0 To 7
                    ListboxQueue.Items.RemoveAt(0)
                Next
            Next
        End Sub
    
    End Class

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width