Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4247380
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T04:05:58+00:00 2026-05-21T04:05:58+00:00

Given a texbox, how can I completely ignore non-digit characters? So if I press

  • 0

Given a texbox, how can I completely ignore non-digit characters? So if I press “A” or “Z”, those characters would never appear in the textbox. I’d like to run a bit of code if the user tries to input digits and if he tries to input non-digits too..

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T04:05:59+00:00Added an answer on May 21, 2026 at 4:05 am

    Try this

    Private Sub textBox1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles textBox1.KeyPress
        If Char.IsDigit(e.KeyChar) Then
            e.Handled = False
        Else
            e.Handled = True
        End If
    
    End Sub
    

    if e.Handled is set to true for non-digits no input will go to the text box; otherwise the event messages flow as they normally would.

    EDIT:

    To handle BackSpace and Delete is a little more work but you can try this

    Private Sub textBox1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles textBox1.KeyPress
        If Char.IsDigit(e.KeyChar) OrElse e.KeyChar = CChar(Keys.Delete) OrElse e.KeyChar = CChar(Keys.Back) Then
            e.Handled = False
        Else
            e.Handled = True
        End If
    
    End Sub
    
    Protected Overrides Function ProcessCmdKey(ByRef msg As Message, keyData As Keys) As Boolean
        If keyData = Keys.Delete OrElse keyData = Keys.Back Then
            OnKeyPress(New KeyPressEventArgs(CChar(keyData))
        End If
    
        Return MyBase.ProcessCmdKey(msg, keyData)
    End Function
    

    KeyPress event does not get fired for Delete and Backspace keys and hence the override of ProcessCmdKey to manually call the form’s OnKeyPress()

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to use a Validator to guarantee that a given textbox's submitted
How to see the which JSP file contains the given UI element like textbox
Given a horizontal LinearLayout in Android, I'd like to create: (1) a textbox which
Good night, I would like to know how can I highlight a part of
Is it possible to give my HtmlHelper textbox an id ? For instance like
In ASP.NET MVC I could define a textBox editor like this and give it
Given a package, how can I automatically find all its sub-packages?
I want to display the first and last characters of any given string entered
I am developing an application in JSF 2.0 and I would like to have
Given a WinForms TextBox control with MultiLine = true and AcceptsTab == true ,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.