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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T02:23:31+00:00 2026-06-01T02:23:31+00:00

I have a textbox that accepts only these values: . , – $ m

  • 0

I have a textbox that accepts only these values:

. , – $ m M k K b B and numbers

It also only accepts at most two decimal places. My problem is that after I tab off the textbox and then select it again, I am unable to type any information or overwrite whatever exists already in the textbox.

/// Checks if the decimal places are more than 2 and it will not allow user to keyin if its more than 2.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void txtAmount_KeyDown(object sender, KeyEventArgs e)
{
    string textValue = txtAmount.Text.Trim();
    if (textValue.IndexOf(".") > 0)
    {
        string[] decimalvalue = textValue.Split(new char[] { '.' });
        if (decimalvalue[1].Length >= 2)
            cancelCharacter = true;
        else
            cancelCharacter = false;
    }
}

/// <summary>
/// Doesn't allow the user to enter any other value other than 
/// ". , - $ m M k K b B and numbers"
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void txtAmount_KeyPress(object sender, KeyPressEventArgs e)
{
    if ((e.KeyChar < 48 || e.KeyChar > 57) &&
         e.KeyChar != 46 && e.KeyChar != 36 && e.KeyChar != 36 && e.KeyChar != 32 && e.KeyChar != 44 && e.KeyChar != 8 && e.KeyChar != 45 &&
         e.KeyChar != 75 && e.KeyChar != 107 && e.KeyChar != 77 && e.KeyChar != 109 && e.KeyChar != 66 && e.KeyChar != 98)
    {
        e.Handled = true;
    }
    else if (cancelCharacter && e.KeyChar != 8 && e.KeyChar != 45 &&
             e.KeyChar != 75 && e.KeyChar != 107 && e.KeyChar != 77 && e.KeyChar != 109 && e.KeyChar != 66 && e.KeyChar != 98)
    {
        e.Handled = true;
    }
    else if (e.KeyChar == '.' && txtAmount.Text.IndexOf(".") > -1 || cancelCharacter && e.KeyChar != 8)
    {
        e.Handled = true;
    }
}
  • 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-01T02:23:32+00:00Added an answer on June 1, 2026 at 2:23 am

    Looks like you need to reset your cancelCharacter flag. Handle the Leave event:

    private void txtAmount_Leave(object sender, EventArgs e)
    {
        cancelCharacter = false;
    }
    

    How about this. Change to this line in you keydown event to watch for how many characters are selected:

    if (decimalvalue[1].Length >= 2 && txtAmount.SelectionLength < 1 && 
        txtAmount.SelectionStart > textValue.IndexOf("."))
    
    • 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 have a textbox that gets a decimal value say 10500.00 the problem is
I'd like to make a textbox that accepts only numbers, but not integer, but
I have written some JavaScript and jQuery code that accepts only numeric input in
I do have an html form that accepts textbox input of sha1 hashes per
I am wanting to have a numbers/currency only textbox control. So basically it should
I have a textbox that should accept datetime formats only. I am not sure
I have the text box which only accepts number and dot so that i
I have a text box that accepts integers, and I need it to run
I have a TextBox that is bound to a Text-property on an Entity-object. I'd

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.