validation query

A

A.J

Hi......
(Using vb.net, access as database and datareader to access the data)


Now i got few textboxes having attribute like name,age,phonenumber
etc.I want the txtname to contain only alpha(characters); likewise
txtage to contain number and txtphonenumber be in the format of
####-####-##(ex-9999-9999-99).­I searched the forum for this
validation
query and got the answer that this can be done using
char.Iscontrol/Isdigit...etc..­.I got the Concept(Like it takes a
unicode character 16-bit as argument and returns a boolean value) and
unable to figure out how to implement this or is there any other way of

implementing this concept.


As i am a newbie so please explain this concept in detail.


Thankx in advance!!!!
 
S

stand__sure

what you do is override the OnKeyPress method and then set e.Handled =
True for cases that you wish to exclude.

a quick example to allow only numbers...

Public Class TextBox_NumericOnly : Inherits
System.Windows.Forms.TextBox
Protected Overrides Sub OnKeyPress(ByVal e As KeyPressEventArgs)
If (Char.IsNumber(e.KeyChar)=False) Then e.Handled=True
End Sub
End Class

you don't have to create your own control -- you can simply Override
the method, but since this is a common scenario, you may want to write
your own so that you can re-use it...
 
A

A.J

stand__sure said:
what you do is override the OnKeyPress method and then set e.Handled =
True for cases that you wish to exclude.

a quick example to allow only numbers...

Public Class TextBox_NumericOnly : Inherits
System.Windows.Forms.TextBox
Protected Overrides Sub OnKeyPress(ByVal e As KeyPressEventArgs)
If (Char.IsNumber(e.KeyChar)=False) Then e.Handled=True
' What this "handled" method suppose to do??
 
K

Ken Tucker [MVP]

Hi,

Here is a link to a few free masked edit boxes.

http://www.codeproject.com/vb/net/cpflexmaskeditbox.asp
http://www.codeproject.com/useritems/MaskedTextBox.asp


Ken
----------------------
Hi......
(Using vb.net, access as database and datareader to access the data)


Now i got few textboxes having attribute like name,age,phonenumber
etc.I want the txtname to contain only alpha(characters); likewise
txtage to contain number and txtphonenumber be in the format of
####-####-##(ex-9999-9999-99).­I searched the forum for this
validation
query and got the answer that this can be done using
char.Iscontrol/Isdigit...etc..­.I got the Concept(Like it takes a
unicode character 16-bit as argument and returns a boolean value) and
unable to figure out how to implement this or is there any other way of

implementing this concept.


As i am a newbie so please explain this concept in detail.


Thankx in advance!!!!
 
S

stand__sure

it stops the character from being passed to the next event handler
(i.e. it is handled and no further processing is required).

you can also do field-level testing in the Validating event handler
(normally just to display a MessageBox describing the error and to
return focus to the control via e.Cancel = True)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top