Dr.Virusi

hi, I want my texbox1 to accept just numbers but no words


Re: Visual Basic Express Edition textbox ?

frxstrem

If you want a maximum number of numbers, it's best to use a MaskedTextBox.






Re: Visual Basic Express Edition textbox ?

Dr.Virusi

Yes, but i want my textbox1 to accept only numbers, you understand only numbers not words.




Re: Visual Basic Express Edition textbox ?

Dr.Virusi

Help me anyone




Re: Visual Basic Express Edition textbox ?

Martin Cowen


Code Snippet

Private Sub Textbox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress

If Char.IsNumber(e.KeyChar) Or Char.IsControl(e.KeyChar) Then

e.Handled = False

Else

e.Handled = True

End If

End Sub





Re: Visual Basic Express Edition textbox ?

Dr.Virusi

'Handled' is not a member of 'System.EventArgs'.
'Handled' is not a member of 'System.EventArgs'.
'KeyChar' is not a member of 'System.EventArgs'.
'KeyChar' is not a member of 'System.EventArgs'.




Re: Visual Basic Express Edition textbox ?

Martin Cowen

I had to work backwards from your list of errors to see what you had done.
My code is correct.
You have pasted the If statement into the Textbox.Click event handler, not the Textbox.KeyPress event handler. If you either
(a) paste the If statement in the textbox.KeyPress event handler which the Visual Basic IDE will generate for you if you select your text box name on the top left dropdown box of the code editor screen and select the KeyPress event in the right hand dropdown list or
(b) paste the whole of the code that I gave in my last sample and change the name of the text box to suit your application, you will find that it works correctly.

You will see that in the Click event handler, e is a System.EventArg, where as in the KeyPress event handler, e is System.Windows.Forms.KeyPressEventArgs which is a completely different class.




Re: Visual Basic Express Edition textbox ?

Dr.Virusi

Thank you.




Re: Visual Basic Express Edition textbox ?

Solitaire

Enter this in the textbox KeyPress event:

If Not Char.IsNumber(e.KeyChar)Then e.Handled = True

Only the digits from 0-9 will be permitted. But if you also want the Backspace key to work, you need to add one more line:

If e.KeyChar = Chr(8) Then e.Handled = False






Re: Visual Basic Express Edition textbox ?

Martin Cowen

Please mark the code sample as the correct answer, for others to find.