Results 1 to 12 of 12

Thread: If Then and controls [Resolved]

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Dec 2003
    Posts
    127

    If Then and controls [Resolved]

    I have hit a snag in a program I am trying to create. I came up with this program as a goal for myself in my efforts at learning VB.NET; I would like to finish the darn thing mainly to help myslef learn but also to help my mother's obsession with the lottery. Here's the code I have at the moment that throws all kinds of errors.

    Code:
    Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
    
            Dim Output = TextBox1.Text              ;Where the results are displayed in multiline
            Dim Rangeone = TextBox2.Text          ;Lowest number to randomize
            Dim Rangetwo = TextBox3.Text          ;Highest number to randomize
            Dim Picks = TextBox4.Text                 ;How many seperate rows of numbers is displayed
            Dim Plays = TextBox5.Text                 ;How many numbers per row are displayed
            Dim r As New System.Random()
    
            If Picks = "1" Then
                Output.Text = r.Next(Rangeone, Rangetwo + 1)
            Else : Picks = ""
                MsgBox("You must enter the number of picks")
            End If
    
            If Picks = "2" Then
                Output.Text = r.Next(Rangeone, Rangetwo + 1) & vbCrLf
            Else : Picks = ""
                MsgBox("You must enter the number of picks")
            End If
    
            If Picks = "3" Then
                Output.Text = r.Next(Rangeone, Rangetwo + 1)
            Else : Picks = ""
                MsgBox("You must enter the number of picks")
            End If
        End Sub
    
    End Class
    I need help showing the correct number of rows
    as in the commented section of Dim Picks = TextBox4.Text

    I need help formatting the code so that it will show the correct number of numbers per row
    as in the commented section of Dim Plays = TextBox5.Text

    The output of each number in each row needs to be seperated with a [ before each number and a ] after it too.

    Example input for Rangeone = 1
    Example input for Rangetwo = 12
    Example input for Picks = 3
    Example input for Plays = 6

    Example of output would look like
    [02] [10] [03] [06] [05] [09]
    [07] [08] [04] [11] [01] [12]
    [03] [02] [12] [01] [09] [06]

    Pointers, thoughts and examples are appreciated but remember I am very new to VB.NET still.
    Last edited by teamdad; Jun 13th, 2004 at 08:21 PM.

  2. #2
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    First off , why you are not specifiying the data type of the variables and use early binding . Like this :
    VB Code:
    1. Dim Output  As String= TextBox1.Text

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

    Most of your errors are caused by use od ";" as the comment indicator. You must use '

    Referring to Pirate's response, looking at your code it is possible that you are intending to create references to texboxes rather than string variables. I say this because in

    "Output.Text = r.Next(Rangeone, Rangetwo + 1)"

    You have treated "Output" as if it is a Textbox, not a string variable. Please confirm your intentions in this respect and we can then comment on other coding innacuracies.

    Meanwhile, you will find a Select Case more appropriate than the three If .... Then statements.

    The following is a possible alternative to your code but please note that your random statements need further amendment. Not only will it not work, but you must use a variable seed number. One way is to make it depend on the current time. I will post again on this.


    VB Code:
    1. Dim Output As String = TextBox1.Text
    2.         Dim Rangeone As String = TextBox2.Text        '         Lowest number to randomize
    3.         Dim Rangetwo As String = TextBox3.Text           'Highest number to randomize
    4.         Dim Picks As String = TextBox4.Text                 'How many seperate rows of numbers is displayed
    5.         Dim Plays As String = TextBox5.Text                 'How many numbers per row are displayed
    6.         Dim r As New System.Random
    7.         Select Case vAL(Picks)
    8.             Case 1
    9.                 Output = r.Next(Rangeone, Rangetwo + 1)
    10.             Case 2
    11.                 Output = r.Next(Rangeone, Rangetwo + 1) & vbCrLf
    12.             Case 3
    13.                 Output = r.Next(Rangeone, Rangetwo + 1)
    14.             Case Else
    15.                 MsgBox("You must enter the CORRECT number of picks")
    16.         End Select
    Last edited by taxes; Jun 7th, 2004 at 05:19 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.

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

    I've tidied this up a bit.

    Working on the assumption that you want your mother to have some effect on the choice of numbers I have got a running programme. Before I post it, I suggest you try yourself bearing in mind the following.

    1. You don not need a Select Case nor a multiple If .. Then structure. Just one single If..Then..Else to cover incorrect entries by mother.

    2. Your random number should be generated using a combination of the seed number entered by mother and the current time. (This will avoid exact repetition of denerted number sequences) As this could involve you in a long search the following is one way of doing this

    Rand1 = New Random(Now.Millisecond + Val(Picks))
    Output = Str(Rand1.Next(val(Range1), val(Range2)))


    Hope to see your effort soon.
    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.

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Dec 2003
    Posts
    127
    All your suggestions have helped me with a lot of the errors I was getting but I don't have it working yet. Below is the GUI with some dummy data put in it to help explain what I need help with.



    Under the Settings group box the "Numbers range from...To... is the range of numbers that the program will use to select the random numbers from starting lowest to highest.

    Under the Settings group box the "Number of lines" tells the program how many lines in the Picks section will be displayed.

    Under the Settings group box the "Numbers per line" tells the program how many individual numbers will be displayed per line **they are seperated with a [ before the number and a ] after the number and single numbers 0-9 must be preceeded by a 0 "zero"

    When the button is pushed <<Here's where I need the help>> it will pick random numbers between the range of numbers that has been typed into the range fileds, the results will display in the Picks group box based on the number that is put in for the Number of lines and the number put in for the Numbers per line boxes. Any of the numbers under Settings can be any number of numbers.

    My code looks scratchy mainly because I have tried to model my ideas in this program after a dice rolling game I found on the Internet; but also because it's harder to learn code from trial and error with hands on writing. I don't have the capabilities to read a book and apply what I read and have something come out a working model.

  6. #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.

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Dec 2003
    Posts
    127
    taxes,

    I cleaned up

    Code:
    New Sub
    
     Private Sub CalculateNumbers()
    
    so it now reads as below.  It cleared up some syntax errors.
    
    End Sub
    
     Private Sub CalculateNumbers()
    I changed my textboxes to match your example and it still won't function. It seems to be working in the respect that it throws the duplication message -I am assuming that this MessageBox gets thrown if a number is duplicated in the Outcome box?? but it never show's anything in Outcome.

    Any other thoughts about what's going on?

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

    Have you missed the code from your post?
    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.

  9. #9

    Thread Starter
    Lively Member
    Join Date
    Dec 2003
    Posts
    127
    There were syntax errors with "New Sub" so I changed it to "End Sub" and started after it with the "Private Sub CalculateNumbers()
    " statement.

    It still don't function at all. I didn't understand your last reply about have I missed the code from my post.

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

    It is getting late over here so I am posting below the full code. It works completely and I have chosen my lottery numbers with it


    In the form General Section

    VB Code:
    1. Dim Output As String
    2.     Dim Rangeone As String       '         Lowest number to randomize
    3.     Dim Rangetwo As String           'Highest number to randomize
    4.     Dim Picks As String                  'How many seperate rows of
    5.                                                     numbers is displayed
    6.     Dim Plays As String                'How many numbers per row are
    7.                                                    displayed
    8.     Dim Rand1 As System.Random
    9.     Dim arrNumbers(1, 1) As Boolean
    10.  
    11.  
    12.     Private Sub ButtonStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click
    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
    21.             MsgBox("You must enter the CORRECT number of Plays")
    22.             Exit Sub
    23.         Else
    24.             Rand1 = New Random(Now.Millisecond)       'sets random
    25.                                                                                  generator in
    26.                                                                                   motion
    27.             CalculateNumbers()
    28.         End If
    29.  
    30.     End Sub
    31.  
    32.  
    33.     Private Sub CalculateNumbers()
    34.         Dim iCount, iCount1, iCount2 As Integer
    35.         Dim strSelection As String
    36.         ListBox1.Items.Clear()
    37.         Dim strTest As String
    38.         For iCount = 1 To Val(Plays)    'controls number of lines
    39.                                                         requested
    40.             strTest = ""
    41.             For iCount1 = 1 To Val(Picks)   'controls number of
    42.                                                              selections per line
    43.                 Do
    44.                     Output = Str(Rand1.Next(Val(Rangeone), Val(Rangetwo)))      'selects next random number
    45.                     If arrNumbers((Val(Output) - Val(Rangeone) + 1), 0) = False Then
    46.                         arrNumbers(Val(Output) - Val(Rangeone) + 1, 0) = True    'stores TRUE in selected number line of array
    47.                         strTest = strTest & Output & "  "
    48.                         Exit Do
    49.                     End If
    50.                 Loop
    51.             Next iCount1
    52.             strSelection = ""
    53.             For iCount2 = 0 To Val(Rangetwo)        'extract selected
    54.                                                                           numbers and re-
    55.                                                                           set array
    56.                 If arrNumbers(iCount2, 0) = True Then
    57.                     strSelection = strSelection & Str(iCount2) & ", "
    58.                     arrNumbers(iCount2, 0) = False
    59.                 End If
    60.             Next iCount2
    61.             ListBox1.Items.Add(strSelection)
    62.         Next iCount
    63.     End Sub

    I have rempved the messagebox showing duplicate numbers, which I included only to confirm that part was working OK.

    Let me know how you get on (Especially if you win )
    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.

  11. #11

    Thread Starter
    Lively Member
    Join Date
    Dec 2003
    Posts
    127

    Help with If Then and controls [RESOLVED]

    A "BIG" thank you taxes for setting me straight.

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

    You're welcome. It made me write a program I ought to have done years ago.

    By theway, to mark the thread "Resolved" you must edit your FIRST post.
    Last edited by taxes; Jun 10th, 2004 at 04:40 PM.
    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