airsam

How can I ensure that a user can only enter the following into a normal text box:

Number between 0 and 100 (excluding 0 and 100)
No letters
Only numbers

Thanks




Re: Visual Basic Express Edition Validate text box using code

ReneeC

create a keypress event and use IsNumeric

If the keypress is Enter in that event if all characters are numeric, convert the text in the tectbox using Cint and compare the results, rejecting number outside the range of 1 to 99






Re: Visual Basic Express Edition Validate text box using code

airsam

Cant I just use if statements




Re: Visual Basic Express Edition Validate text box using code

ReneeC

You could.

Select case e.keychar

Case keycharOfInterest1

Case keycharOfInterest2

end select

is tidier.






Re: Visual Basic Express Edition Validate text box using code

Tall Dude

(For my friend ReneeC ...)

The following can be done without

using APIs, but the API validation looks cool,

so, what the heck.

' Add an errorprovider from the toolbox

Public Class Form1

Declare Function SetWindowLong Lib "user32" Alias _

"SetWindowLongA" (ByVal hwnd As Integer, ByVal nIndex As Integer, _

ByVal dwNewLong As Integer) As Integer

Declare Function GetWindowLong Lib "user32" Alias _

"GetWindowLongA" (ByVal hwnd As Integer, _

ByVal nIndex As Integer) As Integer

' This routine handles the numeric only part of

' the error checking

Sub NumericEdit(ByVal TheControl As Control)

Const ES_NUMBER = &H2000&

Const GWL_STYLE = (-16)

Dim x As Integer

Dim Estyle As Integer

Estyle = GetWindowLong(TheControl.Handle, GWL_STYLE)

Estyle = Estyle Or ES_NUMBER

x = SetWindowLong(TheControl.Handle, GWL_STYLE, Estyle)

End Sub

Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

' Name the textbox(s) you want to be numeric only, does not accept '.'

Call NumericEdit(TextBox1)

Call NumericEdit(TextBox2)

End Sub

Private Sub TextBox_TextChanged(ByVal sender As System.Object, _

ByVal e As System.EventArgs) Handles _

TextBox1.TextChanged, _

TextBox2.TextChanged

Dim tb As TextBox = DirectCast(sender, TextBox)

' clear any errorprovider error messages

ErrorProvider1.SetError(tb, "")

End Sub

Private Sub TextBox_Validating(ByVal sender As System.Object, _

ByVal e As System.ComponentModel.CancelEventArgs) Handles _

TextBox1.Validating, _

TextBox2.Validating

Dim tb As TextBox = DirectCast(sender, TextBox)

If Val(tb.Text) < 1 Or Val(tb.Text) > 99 Then

ErrorProvider1.SetError(tb, "Value must be between 1 and 99")

End If

End Sub

End Class






Re: Visual Basic Express Edition Validate text box using code

LouieG

Hi AirSam,

depends on exactly what you want to do. do you want to validate it as the user types it in . or do you want to validate it when the user leaves the field i'm assuming you only use integers and not decimal values.

1) if "as the user types it in" then use the field.TextChanged event. something like:

if not isnumeric(field.text) then

blah...blah...

exit sub

end if

if cint(field.text) < 1 or cint(field.text) > 99 then

blah.. blah..

exit sub

end if

"blah..blah..." is how you want to handle the error

2) if "after the user has entered the field and now leaves the field" then use the field.Validating event. something like:

if not isnumeric(field.text) then

blah..blah..

e.cancel = true

exit sub

end if

if cint(field.text) < 1 or cint(field.text) > 99 then

blah.. blah..

e.cancel=true

exit sub

end if

the one 1) validates the characters as the user types it in and 2) validates it only after the user has typed the whole value and wants to go on to the next field

Louie