e.Handled = true doesn't work OnKeyDown

Hi All,
MyRichEdit is derived from RichTextBox.
I'm overriding OnKeyDown.
I'm trying to handle the TAB key but without printing it
in the edit box:

protected override bool IsInputKey(Keys keyData)
{
        switch (keyData)
        {
                case Keys.Tab:
                case Keys.Up:
                case Keys.Down:
                case Keys.Left:
                case Keys.Right:
                {
                        return true;
                }
        }
        return base.IsInputKey(keyData);
}


protected override void OnKeyDown(KeyEventArgs e)
{
        switch (e.KeyData)
        {
                case Keys.Tab:
                {
                        e.Handled = OnTabPressed();
                        break;
                }
                                :
                                :
                }
                base.OnKeyDown (e);
}


OnabPressed() returns false, but the TAB is still printed
in MyRichEdit.
What am I missing?
I'm using VS2003, XP SP1
Thanks!
Udi
udibsAsked:
Who is Participating?
 
dstanley9Connect With a Mentor Commented:
Intersting... RichTextBox may not handle KeyDown like other controls...

from MSDN:

Handled is implemented differently by different controls within Windows Forms. For controls like TextBox which subclass native Win32 controls, it is interpreted to mean that the key message should not be passed to the underlying native control. If you set Handled to true on a TextBox, that control will not pass the key press events to the underlying Win32 text box control, but it will still display the characters that the user typed.

Try using the KeyPress event instead.
0
 
Carl TawnSystems and Integration DeveloperCommented:
I would guess it is because you are calling base.OnKeyDown(e); after your code. Try calling it first.
0
 
udibsAuthor Commented:
Nope,
It didn't help :(
Any other ideas?
0
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

 
dstanley9Commented:
Set Handled to TRUE to prevent the key from printing.  Think of it as "I handled this event, so the OS should not do anything."
0
 
udibsAuthor Commented:
I did,
OnTabPressed() returns true and not false as I said.
0
 
dstanley9Commented:
Have you verified in the debugger that e.Handled is set to False?  Is it still set to false after the call to base.OnKeyDown?
0
 
AdGrootCommented:
De RichTextBox have a property AcceptsTab, if you set this to false, the RichTextbox will not Accepts Tabs

MyRichEdit.AcceptsTab = false;


Ad
0
 
udibsAuthor Commented:
Moving the TAB hanler to onKeyPress did solve it.
the same goes for space by the way :)
Thanks!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.