Results 1 to 12 of 12

Thread: If Then and controls [Resolved]

Threaded View

  1. #6
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949
    Hi,

    OK. That has cleared up one matter. You don't want mother to influence the numbers generated. You have done a good job on the form design, it clearly indicated the underlying code required.

    I'm not quite sure which part in which you now require help, so check your code with the following:

    NOTE: My textbox names will vary from yours.

    VB Code:
    1. In the form declaration section
    2.  
    3.  Dim Output As String
    4.     Dim Rangeone As String       '         Lowest number to randomize
    5.     Dim Rangetwo As String           'Highest number to randomize
    6.     Dim Picks As String                  'How many seperate rows of numbers is displayed
    7.     Dim Plays As String                'How many numbers per row are displayed
    8.     Dim Rand1 As System.Random
    9.     Dim arrNumbers(1, 1) As Boolean
    10.  
    11.  
    12. In the Button_Click event
    13.  
    14.    Rangeone = TextBox2.Text      'lowest number permitted
    15.         Rangetwo = TextBox3.Text      'highest number permitted
    16.         Picks = TextBox5.Text         'number of numbers per line
    17.         Plays = TextBox4.Text         'number of lines required
    18.         ReDim arrNumbers(Val(Rangetwo) - Val(Rangeone) + 1, 1)
    19.  
    20.         If Val(Plays) = 0 Or Val(Plays) > 3 Then     'this puts a limit
    21.                                                                            on the maximum
    22.                                                                            number of Plays.
    23.             MsgBox("You must enter the CORRECT number of Plays")
    24.             Exit Sub
    25.         Else
    26.             Rand1 = New Random(Now.Millisecond)       'sets random generator in motion
    27.             CalculateNumbers()                'calls number generation Sub
    28.         End If
    29.  
    30.  
    31. New Sub
    32.  
    33.  
    34.  Private Sub CalculateNumbers()
    35.         Dim iCount, iCount1, iCount2 As Integer
    36.         For iCount = 1 To Val(Plays)    'controls number of lines requested
    37.             For iCount1 = 1 To Val(Picks)   'controls number of selections per line
    38.                 Do
    39.                     Output = Str(Rand1.Next(Val(Rangeone), Val(Rangetwo)))      'selects next random number
    40.                     If arrNumbers((Val(Output) - Val(Rangeone)), 0) = True Then
    41.                         MessageBox.Show(Output & "Duplicated")
    42.                     Else
    43.                         arrNumbers(Val(Output) - Val(Rangeone) + 1, 0) = True    'stores TRUE in selected number line of array
    44.                         Exit Do
    45.                     End If
    46.                 Loop
    47.             Next
    48.  
    49.             'arrNumbers will now have the required number of numbers for one line
    50.             'insert code to store them in the Picks listbox or whatever you are using.
    51.             ' to do this iterate through arrNumbers checking for those which are set to TRUE
    52.  
    53.             For iCount2 = 0 To Val(Rangetwo)    're-set array
    54.                 If arrNumbers(iCount2, 0) = True Then
    55.                     arrNumbers(iCount2, 0) = False
    56.                 End If
    57.             Next
    58.         Next
    59.     End Sub

    This is not the tidiest way to do it but is what I guessed is more likely to be nearer your effort.

    As an improvement, you might try splitting the Sub into 2 or 3 shorter Subs, or even Functions.

    Let me know how you get on.
    Last edited by taxes; Jun 8th, 2004 at 05:26 AM.
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

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