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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T00:04:54+00:00 2026-05-31T00:04:54+00:00

I have an NumericTextBox component that extends an TextBox in my WinForms application. To

  • 0

I have an NumericTextBox component that extends an TextBox in my WinForms application. To filter the characters that is inputed on the textbox I overrided the OnKeyPress method and check for the numeric KeyCodes from Keys enum.

This is working great.

My problem is to extend more this NumericTextBox, so I created a CurrencyTextBox and put in that coe for formattings and etc…

My solution to make my NumericTextBox extendable was to create a Method with an char array for the accepted chars for this textbox that is not numbers. So in my OnKeyPress Method I iterate on my char array to check if the key pressed is inside and permit this key.

My problem is that in my NumericTextBox since I let only numbers to be typed I can’t type commas and periods, in the KeyPress the code has the OEMComma and OEMPeriod that are not the same codes from the chars (‘,’, ‘.’).

My code that checks for the accepted chars looks like this:

protected override void OnKeyDown(KeyEventArgs e)
{
...
    var acceptedChars = false;
    // AcceptedChars is a String Property with all chars that this textBox should accept
    foreach (var acceptedChar in AcceptedChars.ToCharArray())
    {
        if (acceptedChar == Convert.ToChar(e.KeyCode))
        {
            acceptedChars = true;
            break;
        }
    }
...
}

When I hit the comma the KeyCode comes with 188 that is the OEMComma, the Convert.ToChar() function convert it to ‘1/4’.

PS: I’m not using the OnKeyPress event because I want to prevent the typing and not correct what was typed.

  • 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-31T00:04:55+00:00Added an answer on May 31, 2026 at 12:04 am

    tis worked for me:

    protected override void OnKeyPress(KeyPressEventArgs e)
    {
        base.OnKeyPress(e);
        if (false == Char.IsDigit(e.KeyChar) && e.KeyChar != ',' && e.KeyChar != '.') e.Handled = true;
    }
    

    the magic is setting e.Handled = true, that way character never gets to textbox

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

Sidebar

Related Questions

all. I have a usercontrol NumericTextBox that only allows numeric entries. I need to
I have a numeric textbox in C# .NET which is nothing more than a
Have you used VS.NET Architect Edition's Application and System diagrams to start designing a
Have you determined a maximum number of characters allowed in FCKEditor ? I seem
Have a n-tire web application and search often times out after 30 secs. How
I have created a custom control inheriting TextBox . This custom control is a
Have a bunch of WCF REST services hosted on Azure that access a SQL
I want to design a numeric textbox in silverlight. I have added keydown event
Have a look at one of my websites: moskah.com The problem is that it
I have a custom property that works perfectly, except when it's bound to an

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.