Text box to accept only number in C#

GameBuilder

i am using a French keyboard. On top keyboard there are keys like &,é,",',(,-,è,_,ç,à to use it as a number i have to press shift or turn the capslock on.

maximum number that i can put in text box in 24.

with capLock on : i can put numbers. with capLock off : i cannot put numbers using shift.

also i can put values like &,é,",',(,-,è,_,ç,à in the text box.

public class NumericalTextBox : TextBox
    {
        private int MaxValue;

        public NumericalTextBox()
        {
        }
        public NumericalTextBox(int max_value)
        {
            MaxValue = max_value;
        }

        protected override void OnKeyDown(KeyEventArgs e)
        {
            if (e.KeyCode < Keys.D0 || e.KeyCode > Keys.D9)
            {
                if (e.KeyCode < Keys.NumPad0 || e.KeyCode > Keys.NumPad9)
                {
                    if (e.KeyCode != Keys.Back)
                    {
                        e.SuppressKeyPress = true;
                    }
                }
            }
            if (Control.ModifierKeys == Keys.Shift)
            {
                e.SuppressKeyPress = true;
            }

        }
        protected override void OnKeyPress(KeyPressEventArgs e)
        {

            if (MaxValue >= 0 && !char.IsControl(e.KeyChar) && char.IsDigit(e.KeyChar))
            {
                try
                {
                    string s = this.Text + e.KeyChar;
                    int x = int.Parse(s);
                    if (x >= MaxValue)
                        e.Handled = true;
                }
                catch
                {
                    //e.Handled = true;
                }
            }
            base.OnKeyPress(e);
        }


    }
}
Fabian Bigler

If you can, use the NumericUpDown-Control instead. It provides you the wished behaviour (filtering non-numeric values).

However, if you must use a textbox, the comment of Shtako-verflow points to the answer.

The top answer's code:

private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
    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;
    }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

accept only number in input text box without using script

From Dev

how to set text field that only accept number and comma (,) in PDF Fillable Form?

From Dev

Angular 2 - accept only natural number in input

From Dev

Input field which accept only number with maxlength

From Dev

Which annotation to use for only accept number as input?

From Dev

phone number validation that accept only + and - signs

From Dev

Regex for accept number with only one occurrence of '%' at last

From Dev

Format a text box as a number in a report

From Dev

C# how to validate a string property to only accept 10 digit number

From Dev

bootstrap form input accept text only

From Dev

To allow input text accept only numbers

From Dev

How to validate text and number within text box?

From Dev

Number only input box with range restriction

From Dev

Make text box only highlight text on first click only

From Dev

Turbo C - Scanf to only accept one character

From Dev

Turbo C - Scanf to only accept one character

From Dev

write a loop that prints a number of lines the user entered into the text box, this is using c# windows application

From Dev

jqGrid pagination page number text box not updating

From Dev

Number formatting for PHP generated text box

From Dev

Required a number in text box in Inno Setup

From Dev

Number formatting for PHP generated text box

From Dev

Deleting number from text box produces error

From Dev

Show divs based on number entered into text box

From Dev

Trying to resize TextBox + font size in text box with C# and WPF, can only do one or the other

From Dev

Value will blink only once at the html text box

From Dev

ng pattern for allowing only '-' in input text box

From Dev

Allow only numbers into a input text box

From Dev

text box only displaying one word as value

From Dev

Allow only numbers in range to be entered into text box

Related Related

  1. 1

    accept only number in input text box without using script

  2. 2

    how to set text field that only accept number and comma (,) in PDF Fillable Form?

  3. 3

    Angular 2 - accept only natural number in input

  4. 4

    Input field which accept only number with maxlength

  5. 5

    Which annotation to use for only accept number as input?

  6. 6

    phone number validation that accept only + and - signs

  7. 7

    Regex for accept number with only one occurrence of '%' at last

  8. 8

    Format a text box as a number in a report

  9. 9

    C# how to validate a string property to only accept 10 digit number

  10. 10

    bootstrap form input accept text only

  11. 11

    To allow input text accept only numbers

  12. 12

    How to validate text and number within text box?

  13. 13

    Number only input box with range restriction

  14. 14

    Make text box only highlight text on first click only

  15. 15

    Turbo C - Scanf to only accept one character

  16. 16

    Turbo C - Scanf to only accept one character

  17. 17

    write a loop that prints a number of lines the user entered into the text box, this is using c# windows application

  18. 18

    jqGrid pagination page number text box not updating

  19. 19

    Number formatting for PHP generated text box

  20. 20

    Required a number in text box in Inno Setup

  21. 21

    Number formatting for PHP generated text box

  22. 22

    Deleting number from text box produces error

  23. 23

    Show divs based on number entered into text box

  24. 24

    Trying to resize TextBox + font size in text box with C# and WPF, can only do one or the other

  25. 25

    Value will blink only once at the html text box

  26. 26

    ng pattern for allowing only '-' in input text box

  27. 27

    Allow only numbers into a input text box

  28. 28

    text box only displaying one word as value

  29. 29

    Allow only numbers in range to be entered into text box

HotTag

Archive