controlling the input into a textbox

G

Guest

Does anyone know how can ensure that the only input that is accepted into a textbox is a numeric (only digits 1-9 accepted)

Is this possible

I am using vb.net to create a windows app

thx folks
 
C

Cor

Hi Massy,

Does not matter, this is a sample as you was asking.

Try it, I hope this helps?

Cor

Private Sub textbox1_KeyUp(ByVal sender As Object, _
ByVal e As System.Windows.Forms.KeyEventArgs) Handles textbox1.KeyUp
If e.KeyValue <> 8 Then
If Not IsNumeric(textbox1.Text) OrElse CInt(textbox1.Text) = 0
MessageBox.Show("Only 1 to 9 is allowed")
textbox1.Focus()
End If
End If
End Sub
 
R

Rigga

Private Sub TextBox_KeyPress(ByVal sender As Object, ByVal e As
System.Windows.Forms.KeyPressEventArgs) Handles MyBase.KeyPress
If Char.IsControl(e.KeyChar) Then
Exit Sub
End If
If Not Char.IsNumber(e.KeyChar) Then
e.Handled = True
End If
End Sub


HTH
Rigga.

Massy said:
Does anyone know how can ensure that the only input that is accepted into
a textbox is a numeric (only digits 1-9 accepted)?
 
G

Guest

Hi Cor

I tried your code and it worked fine. However it still leaves the character that has been pressed to remain in the textbox after the messagebox has been displayed. Is there anyway to remove the character that has just been input?

Rigga
I tried the example u gave me but i could not get it to work. Is there something im doin wrong
I replaced the textbox1 value in your code to the name of my textbox. Even i the user enters charcter values in this field, no action is taken! Is there something else i should be doin

thx
 
G

Guest

Hi Rigga, I have just got your code to work, I changed the mybase.Keypress to MyTextbox.Keypress

Thx for your code Cor and Rigga. It is appreciated
:blush:)
 

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