0

I want my Win-Forms application (which has many different forms) to accept only certain characters of user input. This restriction should apply to all forms, all input fields, etc. Is there any simple solution to implement this without adding keyboard-events to every single form and even better, without changing any existing form?

First I have thought of a low-level keyboard hook but this would be applied globally to every user input in every application running on the system which isn't the ideal solution, I guess. Any other possibilities or suggestions?

user1225775
  • 253
  • 5
  • 14

1 Answers1

0

You can do that if you create a custom control for each control you need and set your control to inherit the core controls. For example, this code will prevent clicking on zero:

public class MyTextBox : TextBox
{
    protected override void OnKeyUp(KeyEventArgs e)
    {
        if (e.KeyCode == Keys.D0)
            e.SuppressKeyPress = true;
    }
}

You can even drag this control from the toolbox (after you build).

Amiram Korach
  • 13,056
  • 3
  • 28
  • 30
  • But this would mean I would have to swap every single control accepting user input in my application. – user1225775 Aug 06 '12 at 08:33
  • If you already built you windows, yes you would have to do it. However, this is not two hard. You can find and replace all "System.Windows.Forms.TextBox" with "YourNamespace.MyTextBox". You can also create a FormBase that will do it dynamically but the code is much more complicated, and also overriding all controls you use is a good thing to do. – Amiram Korach Aug 06 '12 at 08:37
  • In defence of mentioned idea, you can recursively enumerate all `System.Windows.Forms.Control`'s in your Form and subscribe to [PreviewKeyDown](http://msdn.microsoft.com/en-us/library/system.windows.forms.control.previewkeydown.aspx) or [OnKeyDown](http://msdn.microsoft.com/en-us/library/system.windows.controls.control.onkeydown(v=vs.95).aspx) event(with all needed logic in event handler) of each item. Should work. – alex.b Aug 06 '12 at 08:40
  • OK, but using this method the next problem would be copy and paste from the clipboard. I could filter keyboard input but every (restricted) text could be pasted from the clipboard. – user1225775 Aug 06 '12 at 09:21
  • You can also use the OnTextChanged method and check what have changed since the last method call. – Amiram Korach Aug 06 '12 at 09:30
  • OK Amiram, thanks for your answer. It seems like there is no really good way for doing this instead of creating a custom control. For filtering keyboard input it is possible to use the [IMessageFilter](http://msdn.microsoft.com/en-us/library/system.windows.forms.imessagefilter.aspx) for the Application, which works fine. The problem using this method is pasting from clipboard which cannot be detected using MessageFilter. Therefore I see no other way at the moment than creating a custom control. – user1225775 Aug 07 '12 at 09:57
  • Go for it. I have a big project where all controls are custom controls. It gives your much more power. – Amiram Korach Aug 07 '12 at 09:59