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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T13:47:43+00:00 2026-06-01T13:47:43+00:00

I have a textbox that gets a decimal value say 10500.00 the problem is

  • 0

I have a textbox that gets a decimal value say 10500.00 the problem is that the way I have it, when you enter a value and then enter the decimals it would not allow you to backspace or clear the textbox to input a new value.. it just gets stuck.. I have tried to set the value back to 0.00 but i think i placed it on the wrong place because it wont change it. Here is my code

 private void txtTransferAmount_KeyPress(object sender, KeyPressEventArgs e)
        {
            bool matchString = Regex.IsMatch(textBoxTransfer.Text.ToString(), @"\.\d\d");
            if (matchString)
            {
                e.Handled = true;
            }

            if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar) && e.KeyChar != '.')
            {
                e.Handled = true;
            }

            // only allow one decimal point
            if (e.KeyChar == '.' && (sender as TextBox).Text.IndexOf('.') > -1)
            {
                e.Handled = true;
            }
        }

What type of change do you suggest so that I may be able to backspace or clear the texbox an enter a new value?.

  • 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-01T13:47:44+00:00Added an answer on June 1, 2026 at 1:47 pm

    You can trap for the Backspace (BS) char (8) and, if found, set your handle to false.

    Your code may look like this…

    ....
    // only allow one decimal point
    if (e.KeyChar == '.' && (sender as TextBox).Text.IndexOf('.') > -1)
    {
        e.Handled = true;
    }
    
    if (e.KeyChar == (char)8)
        e.Handled = false;
    

    A suggestion to make your code a bit more intuitive to interpret what your event handler is doing, you may want to create a var that implies the logic you are implementing. Something like…

    private void txtTransferAmount_KeyPress(object sender, KeyPressEventArgs e)
    {
        bool ignoreKeyPress = false; 
    
        bool matchString = Regex.IsMatch(textBoxTransfer.Text.ToString(), @"\.\d\d");
    
        if (e.KeyChar == '\b') // Always allow a Backspace
            ignoreKeyPress = false;
        else if (matchString)
            ignoreKeyPress = true;
        else if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar) && e.KeyChar != '.')
            ignoreKeyPress = true;
        else if (e.KeyChar == '.' && (sender as TextBox).Text.IndexOf('.') > -1)
            ignoreKeyPress = true;            
    
        e.Handled = ignoreKeyPress;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a textbox that I would like for only numbers. But if I
I would like to have a TextBox that supports AutoComplete and lets users type
I have a textbox in a formview that gets its data from a linqdatasource
I have a textbox in a form field that gets populated by the user.
I have a TextBox that gets hidden depending on whether an item is selected
Here is what I want to do: I have a disabled textbox that gets
I'm using system.net.mail and have a textbox that users can enter their email address
I have a method that gets the value from text boxes and inserts them
I have a TextBox that is bound to a Text-property on an Entity-object. I'd
I have a textbox that i am binding to the viewmodel's string property. The

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.