filtering keys

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi

How do I filter keys in a control, derived from TextBox so that my text box accepts only numbers (or only the keys I want, for that matter). I wrote the following code

class CustomTextBox:TextBox
...
protected override OnKeyDown(KeyEventArgs e

if (e.KeyCode!=Keys.A) // accept only the 'A' ke
{ Handled=true
els
{base.KeyDown



but the text box still displays all I the keys I press
I can override the OnTextChanged event and somehow keep track of what was pressed and change or not the text accordingly, but this is not the right way
Surely there must be a proper way to filter keys

Thank you
 
* "=?Utf-8?B?am9obiBzbWl0aA==?= said:
How do I filter keys in a control, derived from TextBox so that my text box accepts only numbers (or only the keys I want, for that matter). I wrote the following code:

class CustomTextBox:TextBox {
...
protected override OnKeyDown(KeyEventArgs e)
{
if (e.KeyCode!=Keys.A) // accept only the 'A' key
{ Handled=true}

Use 'OnKeyPress' and set 'e.Handled = true'. Notice that this will not
prevent the user from pasting text from the clipboard.
 
Back
Top