Trapping a Keystroke
-
I have subclassed a Windows Form TextBox control. Inside my subclass I override the OnKeyDown like this: protected override void OnKeyDown(KeyEventArgs e) { // Trap keystrokes here???? } Shouldn't this have the effect of inhibiting the user from entering any keystrokes, since I don't pass the event up to the parent method? When I execute and type into the texbox, I see my keystrokes. I am trying to trap keystrokes this way. What am I doing wrong? Thanks! Mark
-
I have subclassed a Windows Form TextBox control. Inside my subclass I override the OnKeyDown like this: protected override void OnKeyDown(KeyEventArgs e) { // Trap keystrokes here???? } Shouldn't this have the effect of inhibiting the user from entering any keystrokes, since I don't pass the event up to the parent method? When I execute and type into the texbox, I see my keystrokes. I am trying to trap keystrokes this way. What am I doing wrong? Thanks! Mark
MarkMokris, You have misunderstood what this event does. The event is fired when someone types something, it does not stop or change the users input, since you do nothing in the event. If you want to stop users entering info into a textbox, set it to ReadOnly or Disabled. Regards, Gareth.
-
MarkMokris, You have misunderstood what this event does. The event is fired when someone types something, it does not stop or change the users input, since you do nothing in the event. If you want to stop users entering info into a textbox, set it to ReadOnly or Disabled. Regards, Gareth.
Thanks. But then, is there anyway to trap a keystroke before it appears in the textbox? What if I needed to prohibit certain special characters from being typed? Thanks again! Mark
-
Thanks. But then, is there anyway to trap a keystroke before it appears in the textbox? What if I needed to prohibit certain special characters from being typed? Thanks again! Mark
-
I have subclassed a Windows Form TextBox control. Inside my subclass I override the OnKeyDown like this: protected override void OnKeyDown(KeyEventArgs e) { // Trap keystrokes here???? } Shouldn't this have the effect of inhibiting the user from entering any keystrokes, since I don't pass the event up to the parent method? When I execute and type into the texbox, I see my keystrokes. I am trying to trap keystrokes this way. What am I doing wrong? Thanks! Mark
MarkMokris wrote:
since I don't pass the event up to the parent method
That's done automatically (events use multi-cast delegates). In OnKeyPress, in your
// Trap keystrokes here????
test for particular characters and set e.Handled to true for the ones you want to ignore. -
I have subclassed a Windows Form TextBox control. Inside my subclass I override the OnKeyDown like this: protected override void OnKeyDown(KeyEventArgs e) { // Trap keystrokes here???? } Shouldn't this have the effect of inhibiting the user from entering any keystrokes, since I don't pass the event up to the parent method? When I execute and type into the texbox, I see my keystrokes. I am trying to trap keystrokes this way. What am I doing wrong? Thanks! Mark
OnKeyDown is a method that raises the KeyDown event for handlers of that event. You're not stopping keystrokes from showing up because you haven't stopped the processing of the key. You've only stopped the raising of an event. If you want to stop the keystroke from occuring, inside your custom textbox control, handle the KeyDown event. Inside there, do your processing to figure out if you want the key to be stopped or not and set the
e.SuppressKeyPress
property totrue
if you want it stopped.A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007 -
I have subclassed a Windows Form TextBox control. Inside my subclass I override the OnKeyDown like this: protected override void OnKeyDown(KeyEventArgs e) { // Trap keystrokes here???? } Shouldn't this have the effect of inhibiting the user from entering any keystrokes, since I don't pass the event up to the parent method? When I execute and type into the texbox, I see my keystrokes. I am trying to trap keystrokes this way. What am I doing wrong? Thanks! Mark
Use the
Handled
property to trap keystrokes on theKeyUp
event. On theKeyPress
event you can capture the key that is pressed.void KeyRecord_KeyUp(Object sender, KeyEventArgs e) { e.Handled = true; } void KeyRecord_KeyPress(Object sender, KeyPressEventArgs e) { e.Handled = true; MessageBox.Show(e.KeyChar.ToString()); }
I hope this is of some help. Regards Guy P.S. you will also need to set
Handled
to true in the KeyDown event.You always pass failure on the way to success.