|
-
Jun 14th, 2004, 06:20 AM
#4
PowerPoster
Re: Validation problem
Originally posted by teamdad
I am looking for suggestions on a better way (one that works).... with the following code. All works great but the red colored part. I have tried diffrent versions of it but I still struggle. Is there a way to use more than one If....Then....Else statement in the same sub? or is there a more convenient way to throw diffrent error messages depending on the type of condition not met.
For those that may question this.... TextBox2.Text must be a greater value than TextBox1.Text
Code:
Dim Rng1 = TextBox1.Text
Dim Rng2 = TextBox2.Text
Dim Rand As New System.Random(Now.Millisecond)
If TextBox1.Text = "" Or TextBox2.Text = "" Or TextBox1.Text = "0" Or TextBox2.Text = "0" Then
MessageBox.Show("Range must be positive numbers!", "Input Error")
ElseIf TextBox1.Text <= TextBox2.Text Then
MessageBox.Show(TextBox2.Text & "is less than" & TextBox1.Text, "Range Error")
Else
Label1.Text = Rand.Next(Rng1, Rng2 + 1)
End If
Hi,
You are still hanging on to VB6 stuff. In VB.NET TextBox.Text is NEVER a number, whereas it can be in VB6.
You have declared Rng1 & Rng2 as strings. whereas you must use numbers in the Rand.Next function.
Rand.Next(Rng1, Rng2 + 1) is intended to produce a number, but TextBox1.Text requires a string.
By using Val(TextBox.text) you can reduce your code.
We have covered all this in your previous threads.
VB Code:
Dim Rng1 as Integer = Val(TextBox1.Text)
Dim Rng2 as Integer = Val(TextBox2.Text)
Dim Rand As New System.Random(Now.Millisecond)
If Val(TextBox1.Text) <= 0 Or Val(TextBox2.Text) <= 0 Then
MessageBox.Show("Range must be positive numbers!", "Input Error")
ElseIf Val(TextBox1.Text) <= Val(TextBox2.Text) Then
MessageBox.Show(TextBox2.Text & "is less than" & TextBox1.Text, "Range Error")
Else
Label1.Text = str(Rand.Next(Rng1, Rng2 + 1))
End If
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
|