Allowing only number in the textbox
i want text box only needs to allowed numeric no .in other way round . i need to adapt the following code in vb.net .let me know please .
Code:
Private Sub TxtWeekNo_KeyPress(KeyAscii As Integer)
If KeyAscii = 8 Or KeyAscii = 27 Or KeyAscii = 13 Then Exit Sub 'we need to allow backspace & some other key
If Not (KeyAscii > 47 And KeyAscii < 58) Then
KeyAscii = 0
End If
End Sub
Re: Allowing only number in the textbox
Re: Allowing only number in the textbox
Not sure how KeyAscii gets to this Sub. Looks like it is a number already. If you are asking about how to test a value in a text box you can use the IsNumeric function.
Re: Allowing only number in the textbox
Quote:
Originally Posted by
Mur16
Not sure how KeyAscii gets to this Sub. Looks like it is a number already. If you are asking about how to test a value in a text box you can use the IsNumeric function.
That was VB 6 code, that the OP wants to 'convert' into .NET
Re: Allowing only number in the textbox
I see . . .
Code:
Private Sub TxtWeekNo_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TxtWeekNo.TextChanged
' test to see if TxtWeekNo.Text IsNumeric
End Sub
Re: Allowing only number in the textbox
I agree with your test for IsNumeric. The problem I can foresee here is the fact that sometimes special keys also needs allowance. Keys such as the Backspace, perhaps even the Delete key should be allowed. Another thing to consider here is the formatting of the numbers, ie, the presenting of them. Will decimals be allowed. Will a thousands-separator be needed and so forth. I was initially thinking about the Masked Edit Box, but decided to go with the VBForums link, as it is more useful at the end of the day
Re: Allowing only number in the textbox