Restrict the textbox to accept number only.
ithelp
Nearly a Posting Maven
2,230 posts since May 2006
Reputation Points: 769
Solved Threads: 128
Because you didn't assign Num value to TextBox.Text value
you need to modify your code to
Private Sub TxtNum_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TxtNum.TextChanged
Dim Num As Integer
Num = Int.Parse(TxtNum.Text)
If Num > 100 Then
MsgBox("You win a lucky prize")
ElseIf Num < 100 Then
MsgBox("Better luck next time")
End If
End Sub
End Class
Ramy Mahrous
Postaholic
2,196 posts since Aug 2006
Reputation Points: 480
Solved Threads: 276
waynespangler is correct, you don't want to use TextChanged event for that.
crazyhorse09
Junior Poster in Training
62 posts since Mar 2009
Reputation Points: 19
Solved Threads: 7
waynespangler is correct, you don't want to use TextChanged event for that.
That's right textchanged is not recommended.
My suggestion is to use a KeyPress or KeyUp/KeyDown event.
Try this one:
Private Sub Textbox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Textbox1.KeyPress
If e.KeyChar = Chr(Keys.Enter) Then
If Me.Textbox1.Text = "" Or Not IsNumeric(Me.Textbox1.Text) Then
MsgBox("Invalid Number Format", MsgBoxStyle.Critical)
Me.Textbox1.Focus()
Me.Textbox1.SelectAll()
Exit Sub
Else
Dim i As Integer = Val(Textbox1.Text)
If i >= 100 Then
MsgBox("you win")
Exit Sub
Else
MsgBox("you lose")
End If
End If
End If
End Sub
c0deFr3aK
Junior Poster in Training
71 posts since Mar 2009
Reputation Points: 11
Solved Threads: 10
Hey friends!!!! how could you answer their question without looking at their code?!!!! which line they assigned textbox value to Num object?!!!!!
Ramy Mahrous
Postaholic
2,196 posts since Aug 2006
Reputation Points: 480
Solved Threads: 276
onedizzydevil
You are not setting the Num variable to value that was entered into the textbox; therefore, it will always be 0.By Ramy Mahrous
Hey friends!!!! how could you answer their question without looking at their code?!!!! which line they assigned textbox value to Num object?!!!!!
Didn't you see my reply?!!
Ramy Mahrous
Postaholic
2,196 posts since Aug 2006
Reputation Points: 480
Solved Threads: 276