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 8497095
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T00:01:50+00:00 2026-06-11T00:01:50+00:00

I have a textbox in a vb form and I want to limit the

  • 0

I have a textbox in a vb form and I want to limit the range of characters that the user can put into the textbox to:" abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890^-*().". The textbox is to insert SI Units into a database so i need consistent syntax. If the user types an invalid character into the textbox I would like the textbox to refuse to insert it, or remove it straight away, leaving the cursor in the same position within the textbox. I would also like the textbox to replace "/" with "^(-" and place the cursor before this.

I have found some code elsewhere which I have edited to do this but the code is bad, it activates on text changed within the textbox. This causes the code to fail, when the user inputs a disallowed value the code it activates itself when it tries to changes the text within the textbox.

Here is my code, the textbox starts with the contents "enter SI Units" from the form designer.

Private Sub TxtQuantityTextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtSIUnit.TextChanged

If txtSIUnit.Text = "Enter SI Units" Then
        Exit Sub
    End If

    Dim charactersAllowed As String = " abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890^-*()."
    Dim Text As String = txtSIUnit.Text
    Dim Letter As String
    Dim SelectionIndex As Integer = txtSIUnit.SelectionStart
    Dim Change As Integer




    Letter = txtSIUnit.Text.Substring(SelectionIndex - 1, 1)
    If Letter = "/" Then
        Text = Text.Replace(Letter, "^(-")
        SelectionIndex = SelectionIndex - 1
    End If



    Letter = txtSIUnit.Text.Substring(SelectionIndex - 1, 1)
    If charactersAllowed.Contains(Letter) = False Then
        Text = Text.Replace(Letter, String.Empty)
        Change = 1
    End If


    txtSIUnit.Text = Text
    txtSIUnit.Select(SelectionIndex - Change, 0)

    If txtQuantity.Text <> "Enter Quantity" Then
        If cmbStateRateSumRatio.SelectedIndex <> -1 Then
            bttAddQUAtoDatabase.Enabled = True
        End If
    End If

End Sub`

Thanks for you help.

  • 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-06-11T00:01:52+00:00Added an answer on June 11, 2026 at 12:01 am

    Use the KeyPress event. Set e.Handled to true if you don’t like the character. It’s a one-liner:

    Private Const AllowedChars = " abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890^-*()."
    
    Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As PressEventArgs) Handles TextBox1.KeyPress
        If e.KeyChar >= " "c AndAlso Not AllowedChars.Contains(e.KeyChar) Then e.Handled = True
    End Sub
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a textbox in a form that I created. I want to add
I have a TextBox in a Windows Form that allows a user to enter
C# question: I have a textBox in a default form Form1 and I want
I have a web form where I have a textbox in which the user
I have a web form with textbox and button. I want after ENTER key
I have a textbox on dialog form, I want to default focus on it
i have a form with some textbox When a user press the enter key
I have a textbox on a MS Access form that users are going to
I have a textbox on a form and i want to set it to
I have a textbox in my form. On the form load event, i want

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.