How to make a Textbox that accepts only numbers and one decimal place in Visual Basic?

Create a TextBox and go to its KeyPress Event.

This textbox accepts Numbers, one (1) decimal place and Special keys (Enter, Backspace, Delete, etc.)

Private Sub Textbox1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles Textbox1.KeyPress

Dim decimalPoint As String
decimalPoint = Application.CurrentCulture.NumberFormat.NumberDecimalSeparator

e.Handled = Not (
                (e.KeyChar = decimalPoint And sender.Text.IndexOf(decimalPoint) = -1) Or
                Char.IsDigit(e.KeyChar) Or
                Asc(e.KeyChar) = 8
            )
End Sub

Published by Christian Foster

Code-blooded, coffee-lover, tall, dark and chubby. I love to draw, has motion-sickness and a sleepy-head. BTW, graduate of BS Computer Science, Associate in Computer Science and certified UiPath RPA Developer. Loyal to my partner and a father of a cute bouncing baby daughter!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Create your website with WordPress.com
Get started
%d bloggers like this: