Results 1 to 8 of 8

Thread: Items from array to textboxes

  1. #1

    Thread Starter
    Member
    Join Date
    Sep 2010
    Location
    Greece
    Posts
    58

    Items from array to textboxes

    Hello to the community.

    I am using vb 2010 an I am dealing with a problem. I have an array of values and some textboxes
    Array has some values like these
    Dim numbers() as Integer= {1,7,2,4}

    I am trying to put these values to 4 textboxes in a random way.
    How can I do that?

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Items from array to textboxes

    Any time you want to do something random, you need to generate a random number and then use that in whatever way is appropriate for your scenario. You can shuffle a list by sorting it on a random number and then take the items onbe by one, much like dealing cards. In your case, you could shuffle the numbers or the TextBoxes, e.g.
    vb.net Code:
    1. Dim rng As New Random
    2. Dim numbers = {1,7,2,4}
    3. Dim textBoxes = {TextBox1, TextBox2, TextBox3, TextBox4}.OrderBy(Function(tb) rng.NextDouble()).ToArray()
    4.  
    5. For i = 0 To numbers.GetUpperBound(0)
    6.     textBoxes(i).Text = numbers(i).ToString()
    7. Next
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Member
    Join Date
    Sep 2010
    Location
    Greece
    Posts
    58

    Re: Items from array to textboxes

    Thanks for your help.
    I have managed somehow to make it work like this



    Code:
    Dim nums() As Double = {10,20,30,40,50}
    
            Dim rnd As New Random()
            Dim shufflenums = nums.OrderBy(Function() rnd.Next).ToArray()
    
            For Me.k = 0 To nums.GetUpperBound(0) 
                mytextbox(k).text = shufflenums(k).ToString
            Next k
    The problem is that some values are be repeated....

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Items from array to textboxes

    Quote Originally Posted by stratos View Post
    The problem is that some values are be repeated....
    I don't see how that's possible, if you have populated mytextbox properly. We can only guess that you've done that though. Have you actually debugged the code properly, using a breakpoint and stepping through it line by line? If you do that, which you should have done before posting here, then you'd be able to see what the contents of shufflenums and shufflenums(k) is so you'd be able to see whether there actually is a number duplicated and why. VS has a debugger for a reason. You need to use it to find and fix bugs like this.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5
    Frenzied Member
    Join Date
    Apr 2016
    Posts
    1,415

    Re: Items from array to textboxes

    Does he not mean that you can get e.g. 1,7,7,4? (the 7 repeated)

    I don't know if it can happen, I'm just asking... Because it's still a random number

  6. #6
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    9,017

    Re: Items from array to textboxes

    If he does mean that, it wouldn't make sense because those random values are only used to trick LINQ into randomly ordering the list. They aren't used for anything else and even if they do repeat, it wouldn't reduce the effectiveness of it's use.
    Last edited by Niya; Feb 26th, 2023 at 11:58 PM.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Items from array to textboxes

    Quote Originally Posted by schoemr View Post
    Does he not mean that you can get e.g. 1,7,7,4? (the 7 repeated)

    I don't know if it can happen, I'm just asking... Because it's still a random number
    It's possible, but Next will generate a value in the range 0 to Integer.MaxValue-1, so it's unlikely. As Niya said though, that wouldn't matter as it would just mean that the corresponding elements from the original array had equal priority within the sort. I'm pretty sure that the algorithm used in that scenario is unstable anyway, so you still wouldn't know whether those two values would appear in the original order or not.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  8. #8

    Thread Starter
    Member
    Join Date
    Sep 2010
    Location
    Greece
    Posts
    58

    Re: Items from array to textboxes

    Thank you guys for the support.
    I went through the code and I found what was wrong.
    I think shuffle code works as intended. I have messed up with some textboxes indices (some of them were the same), that's why they displayed the same values.
    So far, I have tested it and never got same numbers again. Array shuffles like a charm

Tags for this Thread

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