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

  • Home
  • SEARCH
  • 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 524713
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T08:35:48+00:00 2026-05-13T08:35:48+00:00

I tryed to create my own numeric textbox here is my code: public class

  • 0

I tryed to create my own numeric textbox here is my code:

public class NumericTextBox : TextBox
{

    public NumericTextBox()
        : base()
    {
        this.Text = "0";
    }

    private void HandleKeyEvent(KeyEventArgs e)
    {
        e.Handled = true;
        if ((Keyboard.Modifiers & ModifierKeys.Alt) != 0)
        {
            return;
        }
        if (e.Key == Key.Back || e.Key == Key.Delete || e.Key == Key.Left || e.Key == Key.Right ||
            e.Key == Key.D0 || e.Key == Key.D1 || e.Key == Key.D2 || e.Key == Key.D3 || e.Key == Key.D4 || e.Key == Key.D5 || e.Key == Key.D6 ||
            e.Key == Key.D7 || e.Key == Key.D8 || e.Key == Key.D9 ||
            e.Key == Key.NumPad0 || e.Key == Key.NumPad1 || e.Key == Key.NumPad2 || e.Key == Key.NumPad3 || e.Key == Key.NumPad4 || e.Key == Key.NumPad5 || e.Key == Key.NumPad6 ||
            e.Key == Key.NumPad7 || e.Key == Key.NumPad8 || e.Key == Key.NumPad9)
        {
            e.Handled = false;
        }
        else if ((e.Key == Key.Subtract || (e.Key == Key.Unknown && e.PlatformKeyCode == 189)) && base.SelectionStart == 0 && (base.Text.Length == 0 || base.Text[0] != '-'))
        {
            e.Handled = false;
        }
    }

    protected override void OnKeyDown(KeyEventArgs e)
    {
        HandleKeyEvent(e);
        base.OnKeyDown(e);
    }

    protected override void OnKeyUp(KeyEventArgs e)
    {
        HandleKeyEvent(e);
        base.OnKeyUp(e);
    }
}

everything works like supposed but if you press alt and some numbers it creates the ascii symbol corresponding to the number.. is there any way to block an “alt + number combination?
it seems that alt + key just gets entered without going threw OnKeyUp or OnKeyDown…

  • 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-13T08:35:48+00:00Added an answer on May 13, 2026 at 8:35 am

    I got it working by using the TextChanged event here is my code…

    public class NumericTextBox : TextBox
    {
    
        int value;
    
        public NumericTextBox()
            : base()
        {
            this.Text = "0";
            this.TextChanged += new TextChangedEventHandler(NumericTextBox_TextChanged);
        }
    
        void NumericTextBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            int selectionStart = base.SelectionStart;
            bool changed = false;
            List<char> charList = new List<char>();
            for (int i = 0; i < base.Text.Length; i++)
            {
                if (IsValidChar(base.Text[i], i))
                {
                    charList.Add(base.Text[i]);
                }
                else
                {
                    if (selectionStart >= i)
                    {
                        selectionStart--;
                    }
                    changed = true;
                }
            }
            if (changed)
            {
                string text = new string(charList.ToArray());
                this.Text = text;
                this.SelectionStart = selectionStart;
            }
            int newValue;
            if (!int.TryParse(this.Text, out newValue))
            {
                this.Text = value.ToString();
                this.SelectionStart = this.Text.Length;
            }
            else
            {
                value = newValue;
            }
        }
    
        private bool IsValidChar(char c, int index)
        {
            return ((c == '-' && index == 0) || c == '0' || c == '1' || c == '2' || c == '3' || c == '4' || c == '5' || c == '6' || c == '7' || c == '8' || c == '9');
        }
    
        private void HandleKeyEvent(KeyEventArgs e)
        {
            e.Handled = true;
            if ((Keyboard.Modifiers & ModifierKeys.Control) == ModifierKeys.Control)
            {
                e.Handled = false;
            }
            if (e.Key == Key.Back || e.Key == Key.Delete || e.Key == Key.Left || e.Key == Key.Right ||
                e.Key == Key.D0 || e.Key == Key.D1 || e.Key == Key.D2 || e.Key == Key.D3 || e.Key == Key.D4 || e.Key == Key.D5 || e.Key == Key.D6 ||
                e.Key == Key.D7 || e.Key == Key.D8 || e.Key == Key.D9 ||
                e.Key == Key.NumPad0 || e.Key == Key.NumPad1 || e.Key == Key.NumPad2 || e.Key == Key.NumPad3 || e.Key == Key.NumPad4 || e.Key == Key.NumPad5 || e.Key == Key.NumPad6 ||
                e.Key == Key.NumPad7 || e.Key == Key.NumPad8 || e.Key == Key.NumPad9)
            {
                e.Handled = false;
            }
            else if ((e.Key == Key.Subtract || (e.Key == Key.Unknown && e.PlatformKeyCode == 189)) && base.SelectionStart == 0 && (base.Text.Length == 0 || base.Text[0] != '-'))
            {
                e.Handled = false;
            }
        }
    
        protected override void OnKeyDown(KeyEventArgs e)
        {
            HandleKeyEvent(e);
            base.OnKeyDown(e);
        }
    
        protected override void OnKeyUp(KeyEventArgs e)
        {
            HandleKeyEvent(e);
            base.OnKeyUp(e);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create my own class template for Visual Studio called Public Class.
I tryed to create new components from one base Windows Form, also I found
I tried to create my own Border class and then insert it in my
I've been following this tutorial to create my own custom project type and for
I tried to create the record in the datastore with my own key: class
I have tried create this table, but nothing I have tried works from FKs.
I tried to create base controller and write in it: Ext.define('My.Users.controller.Role', { extend :
I tried to create a code that adds 2 percent each time a component
I tried to create my own templated helpers, but I got stuck on TextBoxFor
I want to create my own RSS/XML feed. I fetch data from the database

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.