0

I am already blocking empty input now and I need to block numerical input for my textbox but I do not know how to do this

Here is my partial code

if (AmateurCheckBox.IsChecked == true)

        {
            if (String.IsNullOrEmpty(NewNameTextBox.Text))
            {
                Prompt();
            }


            else
            {
                MessageBox.Show("Amateur Competitor Added.");
            }
        }
Umut
  • 7
  • 5
  • Possible duplicate: http://stackoverflow.com/questions/294611/input-handling-in-winform – Mikanikal Apr 25 '16 at 16:29
  • This does not answer my question it is not a duplicate – Umut Apr 25 '16 at 16:32
  • There are answers there that allow you to formulate your own code to prevent users from entering numerical input using the Key events of a TextBox. The OP is asking for the same thing, "block certain input keys" to your "block numerical input"....... – Mikanikal Apr 25 '16 at 16:36

2 Answers2

0

You need to convert the text to integer value and see if parsing works; If parsing works, prompt for user input as per your logic. However, I don't understand the second else if statement you had!!

if (AmateurCheckBox.IsChecked == true)

    {
        int outValue;
        if (String.IsNullOrEmpty(NewNameTextBox.Text))
        {
            Prompt();
        }


        else if (Int.TryParse(NewNameTextBox.Text, outValue))
        {
            Prompt();
        }

        else
        {
            MessageBox.Show("Amateur Competitor Added.");
        }
    }
techspider
  • 3,370
  • 13
  • 37
  • 61
0

you can do it in event KeyPress of textbox :

private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
    e.Handled = char.IsDigit(e.KeyChar);
}
Abdellah OUMGHAR
  • 3,627
  • 1
  • 11
  • 16