Solved

e.Handled = true doesn't work OnKeyDown

Posted on 2006-06-28
8
590 Views
Last Modified: 2011-09-20
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
Comment
Question by:udibs
8 Comments
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 17000700
I would guess it is because you are calling base.OnKeyDown(e); after your code. Try calling it first.
0
 

Author Comment

by:udibs
ID: 17000949
Nope,
It didn't help :(
Any other ideas?
0
 
LVL 25

Expert Comment

by:dstanley9
ID: 17001183
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:udibs
ID: 17001322
I did,
OnTabPressed() returns true and not false as I said.
0
 
LVL 25

Expert Comment

by:dstanley9
ID: 17001603
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
 
LVL 25

Accepted Solution

by:
dstanley9 earned 125 total points
ID: 17001670
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
 
LVL 5

Expert Comment

by:AdGroot
ID: 17002257
De RichTextBox have a property AcceptsTab, if you set this to false, the RichTextbox will not Accepts Tabs

MyRichEdit.AcceptsTab = false;


Ad
0
 

Author Comment

by:udibs
ID: 17008474
Moving the TAB hanler to onKeyPress did solve it.
the same goes for space by the way :)
Thanks!
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

679 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question