Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Textbox subclass

Status
Not open for further replies.

kyledunn

Programmer
Jan 2, 2001
145
US
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
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top