Curbside Coder

"Tech blog for code newbies, wizards and ninjas!"

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

Leave a comment

Navigation

About

Hello world! I’m Christian Foster, your Curbside Coder! Welcome to my corner of the internet. Let’s make a dent in the universe!

Design a site like this with WordPress.com
Get started