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