1

I am trying to figure out how to make it so that if I press my button to do a action, (like show a messagebox) and my maskedtextbox's text isn't a number, then it goes and does something like say that you can only have a number in the TextBox or something like that. I can't seem to figure it out.

I have tried to use this:

if (!System.Text.RegularExpressions.Regex.IsMatch(binTxtbx.Text, @"0-9"))
            e.Handled = true;

But if I use that it wont put any text into the maskedtextbox.

If you know if anyone asked the same question that I did, please tell me.

3
  • 1
    If you're using a MaskedTextBox the Mask property can (should) be used to prevent invalid entry MaskedTextBox.Mask = "000"
    – jac
    Jan 19, 2013 at 2:21
  • I just don't like to have to have underscores in my MaskedTexBox.
    – Dozer789
    Jan 19, 2013 at 21:53
  • 1
    That's fine, but it is still better to prevent the user from entering invalid data than it is to let them, then tell them afterward they can't do that. You could change the default _ prompt to a space character. I don't like that personally because it is non-standard, or you could use a TextBox and handle the KeyDown event to prevent non-numeric entry - stackoverflow.com/questions/463299/…
    – jac
    Jan 20, 2013 at 4:15

3 Answers 3

3

If you don't mind using a maskedTextBox, and just don't like the underscores (as you mentioned in your comment), simply change the PromptChar to a blank.

You can do this either in the Design View in the MaskedTextBox Properties, or in the code like this:

myMaskedTextBox.PromptChar = ' ';


EDIT:

Alternatively, (if you don't want to use a maskedTextBox) you could wire the KeyDown event to an EventHandler like this:

    private void numericComboBox_KeyDown(object sender, KeyEventArgs e)
    {
        try
        {
            e.SuppressKeyPress = false;

            // Determine whether the keystroke is a number from the top of the keyboard.
            if (e.KeyCode < Keys.D0 || e.KeyCode > Keys.D9)
            {
                // Determine whether the keystroke is a number from the keypad.
                if (e.KeyCode < Keys.NumPad0 || e.KeyCode > Keys.NumPad9)
                {
                    // Determine whether the keystroke is a backspace or arrow key
                    if ((e.KeyCode != Keys.Back) && (e.KeyCode != Keys.Up) && (e.KeyCode != Keys.Right) && (e.KeyCode != Keys.Down) && (e.KeyCode != Keys.Left))
                    {
                        // A non-numerical keystroke was pressed.
                        // Set the flag to true and evaluate in KeyPress event.
                        e.SuppressKeyPress = true;
                    }
                }
            }
        }
        catch (Exception ex)
        {
            //Handle any exception here...
        }
    }
1
  • I just figured out how to change the prompt character in the properties, and that first code you you had worked too! Thanks a lot.
    – Dozer789
    Jan 23, 2013 at 14:48
1

The expression should be [0-9] with the square braces.

Full code:

!System.Text.RegularExpressions.Regex.IsMatch(binTxtbx.Text, "^[0-9]*$")
1
  • Should it be: [0-9], @[0-9] or @["0-9"] ?
    – Dozer789
    Jan 19, 2013 at 2:08
1

Maybe you could use

if (binTxtbx.Text.Any(c => char.IsNumber(c)))
{
   // found a number in the string
}

or

if (binTxtbx.Text.All(c => char.IsNumber(c)))
{
    // the string is a number
}
1
  • Thanks, but i still can't figure out how to make it so that if it Isnt a number that it removes whatever the other person put in (like a letter) or shows like a messagebox or something. Can you figure out how to do that?
    – Dozer789
    Jan 19, 2013 at 13:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.