Results 1 to 4 of 4

Thread: Validation problem [Resolved]

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Dec 2003
    Posts
    127

    Validation problem [Resolved]

    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
    Last edited by teamdad; Jun 17th, 2004 at 08:48 PM.

  2. #2
    Lively Member ayan's Avatar
    Join Date
    Jan 2004
    Posts
    112
    the code states [let's say textbox1.text=3 and textbox2.text=3] lesser... i'm quiet not sure about this. i assume it's <

    i'm not sure.

  3. #3
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: Validation problem

    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 Val(TextBox1.Text) <= Val(TextBox2.Text) Then
                MessageBox.Show(TextBox2.Text & "is less than" & TextBox1.Text, "Range Error")
            Else
               Label1.Text = Rand.Next(Rng1, Rng2 + 1)
                      End If
    The original code you posted is only comparing the text values. Not the numeric values in there.

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

    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:
    1. Dim Rng1 as Integer = Val(TextBox1.Text)
    2.         Dim Rng2 as Integer = Val(TextBox2.Text)
    3.         Dim Rand As New System.Random(Now.Millisecond)
    4.         If Val(TextBox1.Text) <= 0 Or Val(TextBox2.Text) <= 0 Then
    5.             MessageBox.Show("Range must be positive numbers!", "Input Error")
    6.         ElseIf Val(TextBox1.Text) <= Val(TextBox2.Text) Then
    7.             MessageBox.Show(TextBox2.Text & "is less than" & TextBox1.Text, "Range Error")
    8.         Else
    9.             Label1.Text = str(Rand.Next(Rng1, Rng2 + 1))
    10.         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
  •  



Click Here to Expand Forum to Full Width