|
-
Jun 6th, 2004, 06:57 PM
#1
Thread Starter
Lively Member
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.
-
Jun 6th, 2004, 07:14 PM
#2
Sleep mode
First off , why you are not specifiying the data type of the variables and use early binding . Like this :
VB Code:
Dim Output As String= TextBox1.Text
-
Jun 7th, 2004, 04:57 AM
#3
PowerPoster
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:
Dim Output As String = TextBox1.Text
Dim Rangeone As String = TextBox2.Text ' Lowest number to randomize
Dim Rangetwo As String = TextBox3.Text 'Highest number to randomize
Dim Picks As String = TextBox4.Text 'How many seperate rows of numbers is displayed
Dim Plays As String = TextBox5.Text 'How many numbers per row are displayed
Dim r As New System.Random
Select Case vAL(Picks)
Case 1
Output = r.Next(Rangeone, Rangetwo + 1)
Case 2
Output = r.Next(Rangeone, Rangetwo + 1) & vbCrLf
Case 3
Output = r.Next(Rangeone, Rangetwo + 1)
Case Else
MsgBox("You must enter the CORRECT number of picks")
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.
-
Jun 7th, 2004, 06:21 AM
#4
PowerPoster
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.
-
Jun 7th, 2004, 09:57 PM
#5
Thread Starter
Lively Member
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.
-
Jun 8th, 2004, 05:21 AM
#6
PowerPoster
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:
In the form declaration section
Dim Output As String
Dim Rangeone As String ' Lowest number to randomize
Dim Rangetwo As String 'Highest number to randomize
Dim Picks As String 'How many seperate rows of numbers is displayed
Dim Plays As String 'How many numbers per row are displayed
Dim Rand1 As System.Random
Dim arrNumbers(1, 1) As Boolean
In the Button_Click event
Rangeone = TextBox2.Text 'lowest number permitted
Rangetwo = TextBox3.Text 'highest number permitted
Picks = TextBox5.Text 'number of numbers per line
Plays = TextBox4.Text 'number of lines required
ReDim arrNumbers(Val(Rangetwo) - Val(Rangeone) + 1, 1)
If Val(Plays) = 0 Or Val(Plays) > 3 Then 'this puts a limit
on the maximum
number of Plays.
MsgBox("You must enter the CORRECT number of Plays")
Exit Sub
Else
Rand1 = New Random(Now.Millisecond) 'sets random generator in motion
CalculateNumbers() 'calls number generation Sub
End If
New Sub
Private Sub CalculateNumbers()
Dim iCount, iCount1, iCount2 As Integer
For iCount = 1 To Val(Plays) 'controls number of lines requested
For iCount1 = 1 To Val(Picks) 'controls number of selections per line
Do
Output = Str(Rand1.Next(Val(Rangeone), Val(Rangetwo))) 'selects next random number
If arrNumbers((Val(Output) - Val(Rangeone)), 0) = True Then
MessageBox.Show(Output & "Duplicated")
Else
arrNumbers(Val(Output) - Val(Rangeone) + 1, 0) = True 'stores TRUE in selected number line of array
Exit Do
End If
Loop
Next
'arrNumbers will now have the required number of numbers for one line
'insert code to store them in the Picks listbox or whatever you are using.
' to do this iterate through arrNumbers checking for those which are set to TRUE
For iCount2 = 0 To Val(Rangetwo) 're-set array
If arrNumbers(iCount2, 0) = True Then
arrNumbers(iCount2, 0) = False
End If
Next
Next
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.
-
Jun 9th, 2004, 06:04 PM
#7
Thread Starter
Lively Member
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?
-
Jun 9th, 2004, 06:10 PM
#8
PowerPoster
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.
-
Jun 9th, 2004, 06:28 PM
#9
Thread Starter
Lively Member
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.
-
Jun 9th, 2004, 06:29 PM
#10
PowerPoster
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:
Dim Output As String
Dim Rangeone As String ' Lowest number to randomize
Dim Rangetwo As String 'Highest number to randomize
Dim Picks As String 'How many seperate rows of
numbers is displayed
Dim Plays As String 'How many numbers per row are
displayed
Dim Rand1 As System.Random
Dim arrNumbers(1, 1) As Boolean
Private Sub ButtonStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click
Rangeone = TextBox2.Text 'lowest number permitted
Rangetwo = TextBox3.Text 'highest number permitted
Picks = TextBox5.Text 'number of numbers per line
Plays = TextBox4.Text 'number of lines required
ReDim arrNumbers(Val(Rangetwo) - Val(Rangeone) + 1, 1)
If Val(Plays) = 0 Or Val(Plays) > 3 Then
MsgBox("You must enter the CORRECT number of Plays")
Exit Sub
Else
Rand1 = New Random(Now.Millisecond) 'sets random
generator in
motion
CalculateNumbers()
End If
End Sub
Private Sub CalculateNumbers()
Dim iCount, iCount1, iCount2 As Integer
Dim strSelection As String
ListBox1.Items.Clear()
Dim strTest As String
For iCount = 1 To Val(Plays) 'controls number of lines
requested
strTest = ""
For iCount1 = 1 To Val(Picks) 'controls number of
selections per line
Do
Output = Str(Rand1.Next(Val(Rangeone), Val(Rangetwo))) 'selects next random number
If arrNumbers((Val(Output) - Val(Rangeone) + 1), 0) = False Then
arrNumbers(Val(Output) - Val(Rangeone) + 1, 0) = True 'stores TRUE in selected number line of array
strTest = strTest & Output & " "
Exit Do
End If
Loop
Next iCount1
strSelection = ""
For iCount2 = 0 To Val(Rangetwo) 'extract selected
numbers and re-
set array
If arrNumbers(iCount2, 0) = True Then
strSelection = strSelection & Str(iCount2) & ", "
arrNumbers(iCount2, 0) = False
End If
Next iCount2
ListBox1.Items.Add(strSelection)
Next iCount
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.
-
Jun 9th, 2004, 07:22 PM
#11
Thread Starter
Lively Member
Help with If Then and controls [RESOLVED]
A "BIG" thank you taxes for setting me straight.
-
Jun 10th, 2004, 04:59 AM
#12
PowerPoster
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|