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.
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:
EDIT:
Alternatively, (if you don’t want to use a maskedTextBox) you could wire the KeyDown event to an EventHandler like this: