How to create a text box that accepts only numbers and full stop in VB.Net
-
-
-
Private Sub txt1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txt1.KeyPress
Try
If Not (Char.IsNumber(e.KeyChar) Or Char.IsControl(e.KeyChar)) Then e.Handled = True
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End SubMy advice is free, and you may get what you paid for.
-
Private Sub txt1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txt1.KeyPress
Try
If Not (Char.IsNumber(e.KeyChar) Or Char.IsControl(e.KeyChar)) Then e.Handled = True
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End SubMy advice is free, and you may get what you paid for.
Thank you very much Johan, Actually i wanted the box along with dots inbetween the numbers and I added IsPunctuation to your code. Thank you once again. Didi, I was trying to do it as in vb6 but it was driving me mad without an answer. Ofcourse I am a beginer in VB.Net and Johan's coding is very new to me. Danish, thanks for your reply but I needed a box with numeric values and with / without decimals to be keyed in. Thank you all. :)
-
Thank you very much Johan, Actually i wanted the box along with dots inbetween the numbers and I added IsPunctuation to your code. Thank you once again. Didi, I was trying to do it as in vb6 but it was driving me mad without an answer. Ofcourse I am a beginer in VB.Net and Johan's coding is very new to me. Danish, thanks for your reply but I needed a box with numeric values and with / without decimals to be keyed in. Thank you all. :)
If you are used to VB6, you'll find .NET a bit confusing in the beginning, because its approach is just a little different, however just like in the example code I showed you, check out all the new properties and methods. Lots of things have become much easier to do, and I am quite sure you'll come to love it in about two weeks time, and wonder how you managed to do it all with vb6.
My advice is free, and you may get what you paid for.
-
The KeyPress code that you were given will work, just remember that if a user pastes a value into the textbox, the keypress code isn't going to stop the alpha characters like usual. It will still be possible for alpha values to get into the textbox, and you may have to add code to check for it. Just thought I'd mention it, since I've encountered that issue before.