The question is simple: how to do it? I've a couple of MaskedTextboxes which are filled by some indirect user actions (so not by typing in values directly). The text within the textboxes is colored red when the input is being rejected by the mask. Fine so far.
However, I don't want the end-user to be able to edit the boxes directly. So, I added the following code:
private void HandleMaskedTextBoxKeyPress(object sender, KeyPressEventArgs e)
{
e.Handled = true;
}
private void HandleMaskedTextBoxKeyDown(object sender, KeyEventArgs e)
{
e.Handled = true;
e.SuppressKeyPress = true;
}
And while most characters are handled, the backspace and delete keys aren't: text disappears when pressing those keys. How can those keys be handled, too?
(Another option would be to set the readonly
property of the boxes to true
, but then the text doesn't get colored red anymore when input is rejected. I tried to force the red color in the MaskInputRejected
event, but this didn't work out, either.)