Restricting textbox input

M

mdb

I'm trying to restrict the characters that are valid in a textBox, so I
derived from System.Windows.Forms.TextBox, and overrode WndProc, catching
the key up/down messages... Everything works except the arrow keys - the
text cursor never moves. Why won't the arrows work? I know I'm catching
the correct WParam values...

protected override void WndProc(ref Message m)
{
if ((m.Msg == 256) || (m.Msg == 258))
{
int v = (int)m.WParam;

if ((v > (int)'0') && (v < (int)'9'))
{
base.WndProc (ref m);
return;
}

switch(v)
{
case 8: // Backspace
case 13: // Enter
case 33: // PgUp
case 34: // PgDn
case 35: // End
case 36: // Home
case 37: // Left
case 38: // Up
case 39: // Right
case 40: // Down
case 46: // Delete
base.WndProc(ref m);
break;
default:
break;
}
}
else
{
base.WndProc(ref m);
}
}
 
I

Ignacio Machin \( .NET/ C# MVP \)

HI,

Instead of creating a new class just use the TextBox.KeyPress event, there
you will get two properties
1- Handled that indicate that the key was hanbdle and it should be igonred.
2- KeyChar which is a char, y ou can use Char.Is(Control|Digit|Letter|etc )
as needed.


cheers,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top