• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 600
  • Last Modified:

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
0
udibs
Asked:
udibs
1 Solution
 
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
 
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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
 
dstanley9Commented:
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
 
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

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now