CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Sep 2018
    Posts
    3

    Adding Items From 1 RichTextBox into Different Arrays

    I've been struggling to figure out how to do this and hope someone can help. Here is my scenario...

    I have a RichTextBox with multiple items inside and the number of items may vary from one day to the next. I also have the option to run my program on either 1, 2 or 3 different threads (each thread will have its own array to work from). Once started, I would like the program to look at how many items are listed in the RichTextBox and then split them up evenly into each individual array.

    For example, if I put 1 item in the RichTextBox and select 1 thread, it will simply add that one item into the array created for the first thread. If I put 3 items into the RichTextBox and select 3 threads, it will take the first item and add it to the first array, the second item and add it to the second array, and then add the third item to the third array. If I put 6 items in and select 3 threads, it will put the first two into the first array, the second two into the second array, and the final two into the third array. Keep in mind, there might be days where I have 5 items in the RichTextBox and 2 threads selected. If this were the case then I would want the first 2 items added to the first array and the remaining 3 added to the second array.

    Here is some code I've been messing around with, but I know I'm way off because I've never done this before. You can tell by the very first line that if I don't have a round number it's not going to work. For example, if I were to put 7 items into the RichTextBox and select 3 threads, the Integer I'm going to get is 2.3333333

    Code:
    'Get number of items in RichTextBox and divide by number of threads selected to determine how many items should go into each individual array
    Dim itemsPerThread As Integer = txtItems.Lines.Count / nmThreads.Value
    
    'Run loop to add the itemsPerThread into arrays
    Dim myitemArray1 As New ArrayList
    Dim myitemArray2 As New ArrayList
    Dim myitemArray3 As New ArrayList
    Dim x As Integer = 1
    Dim arrayCount As Integer = 1
    
    For i As Integer = 0 To txtItems.Lines.Count - 1
        While x <= itemsPerThread
            myitemArray(arrayCount).Add(i)
            x = x + 1
        End While
        x = 0
        arrayCount = arrayCount + 1
    Next

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Adding Items From 1 RichTextBox into Different Arrays

    Your problem has nothing to do with the RichTextBox.
    It is just simple mathematics. Try to solve it with a peace of paper and a pencil.
    Then learn the basics how to work with integer numbers (integer arithmetic).
    Then implement your algorithm in code!
    Victor Nijegorodov

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

Featured