I am attempting to create a subclass to filter text input into a text box to allow only numeric input. Here is the code I am using:
public class NumericInput : System.Windows.Forms.TextBox
{
protected override bool ProcessKeyEventArgs(ref Message m)
{
if (m.msg == 0x0102) // WM_CHAR
{
char c = (char)m.wParam;
if (c < '0' || c > '9')
{
return true;
}
}
return base.ProcessKeyEventArgs(ref m);
}
}
When I create a text box using the designer mode (possibly this approach is my problem) and then create a new instance using:
textboxname = new NumericInput();
the input appears to use the new class because the standard OnKeyDown events are no longer used but it is still returning all characters.
When I use:
System.Windows.Forms.TextBox textboxname = new NumericInput();
the standard OnKeyDown events still fire. This is my only indication that the new class is being used.
Can someone help me out with understanding subclasses (derived class) to allow capturing keystrokes so they don't get displayed when typing in a textbox?
Any discussion would be greatly appreciated?
Thanks,
Kyle
public class NumericInput : System.Windows.Forms.TextBox
{
protected override bool ProcessKeyEventArgs(ref Message m)
{
if (m.msg == 0x0102) // WM_CHAR
{
char c = (char)m.wParam;
if (c < '0' || c > '9')
{
return true;
}
}
return base.ProcessKeyEventArgs(ref m);
}
}
When I create a text box using the designer mode (possibly this approach is my problem) and then create a new instance using:
textboxname = new NumericInput();
the input appears to use the new class because the standard OnKeyDown events are no longer used but it is still returning all characters.
When I use:
System.Windows.Forms.TextBox textboxname = new NumericInput();
the standard OnKeyDown events still fire. This is my only indication that the new class is being used.
Can someone help me out with understanding subclasses (derived class) to allow capturing keystrokes so they don't get displayed when typing in a textbox?
Any discussion would be greatly appreciated?
Thanks,
Kyle