none
random unique textboxes RRS feed

  • Question

  • THis is CF 1.0. I have 10 textboxes. There are 9 levels. On the first level in a ten character word one textbox will be blank while the rest show the other 9 letters. This blank textbox will be random. On level 2 there will be 2 random blanks. and so on.

    So the problem I have is making from 2 - 9 textboxes random blank and unique.


    Alan
    Thursday, December 2, 2010 6:31 AM

Answers

  • The problem of generating a set of N unique random numbers from a finite domain is fairly common.  The most common technique is to generate the random numbers in a loop, with a retry occuring whenever a number is repeated.  This method is fine, but when collections are available, I prefer using a more elegant approach.

    In the code below, an ArrayList is created and filled with references to ten text boxes.  In a loop, five random numbers are generated and used as indexes into the ArrayList.  Each time through the loop, the random number generated is in the range of zero (0) to the count of items in the ArrayList less one (1).  The Visibility of each text box selected in this way is set to False to hide that text box, and the text box's reference is removed from the ArrayList using the RemoveAt method so that it cannot be selected again, and reducing the ArrayList Count by one.

    You didn't specify, but I'm guessing from your past posts you're developing in VB.Net.  If you use this approach, you'll have to adapt the code as needed for your application.  Perhaps instead of hiding the selected text boxes, you may just want to set their Text properties differently.  You'll also need to vary the number of text boxes selected with each level. 

    ' Create an ArrayList initially containing references to all our text boxes
    Dim al As New ArrayList(10)
    
    al.Add(TextBox1)
    al.Add(TextBox2)
    al.Add(TextBox3)
    al.Add(TextBox4)
    al.Add(TextBox5)
    al.Add(TextBox6)
    al.Add(TextBox7)
    al.Add(TextBox8)
    al.Add(TextBox9)
    al.Add(TextBox10)
    
    ' Make all 10 text boxes visible initially
    For i As Integer = 0 To 9
     Dim tb As TextBox = DirectCast(al(i), TextBox)
     tb.Visible = True
    Next
    Dim r As New Random()
    
    ' Now randomly hide 5 of the 10 text boxes
    For i As Integer = 0 To 4
     Dim ndx As Integer = r.Next(al.Count)
     Dim tb As TextBox = DirectCast(al(ndx), TextBox)
     tb.Visible = False
     al.RemoveAt(ndx)
    Next
    
    

    Tom Gensel PTG Systems, LLC
    http://www.ptgsystems.com
    • Marked as answer by anaylor01 Thursday, December 2, 2010 9:20 AM
    Thursday, December 2, 2010 8:26 AM

All replies

  • The problem of generating a set of N unique random numbers from a finite domain is fairly common.  The most common technique is to generate the random numbers in a loop, with a retry occuring whenever a number is repeated.  This method is fine, but when collections are available, I prefer using a more elegant approach.

    In the code below, an ArrayList is created and filled with references to ten text boxes.  In a loop, five random numbers are generated and used as indexes into the ArrayList.  Each time through the loop, the random number generated is in the range of zero (0) to the count of items in the ArrayList less one (1).  The Visibility of each text box selected in this way is set to False to hide that text box, and the text box's reference is removed from the ArrayList using the RemoveAt method so that it cannot be selected again, and reducing the ArrayList Count by one.

    You didn't specify, but I'm guessing from your past posts you're developing in VB.Net.  If you use this approach, you'll have to adapt the code as needed for your application.  Perhaps instead of hiding the selected text boxes, you may just want to set their Text properties differently.  You'll also need to vary the number of text boxes selected with each level. 

    ' Create an ArrayList initially containing references to all our text boxes
    Dim al As New ArrayList(10)
    
    al.Add(TextBox1)
    al.Add(TextBox2)
    al.Add(TextBox3)
    al.Add(TextBox4)
    al.Add(TextBox5)
    al.Add(TextBox6)
    al.Add(TextBox7)
    al.Add(TextBox8)
    al.Add(TextBox9)
    al.Add(TextBox10)
    
    ' Make all 10 text boxes visible initially
    For i As Integer = 0 To 9
     Dim tb As TextBox = DirectCast(al(i), TextBox)
     tb.Visible = True
    Next
    Dim r As New Random()
    
    ' Now randomly hide 5 of the 10 text boxes
    For i As Integer = 0 To 4
     Dim ndx As Integer = r.Next(al.Count)
     Dim tb As TextBox = DirectCast(al(ndx), TextBox)
     tb.Visible = False
     al.RemoveAt(ndx)
    Next
    
    

    Tom Gensel PTG Systems, LLC
    http://www.ptgsystems.com
    • Marked as answer by anaylor01 Thursday, December 2, 2010 9:20 AM
    Thursday, December 2, 2010 8:26 AM
  • Tom, you are a genius.
    Alan
    Thursday, December 2, 2010 9:20 AM