Solved

e.Handled = true doesn't work OnKeyDown

Posted on 2006-06-28
8
594 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 

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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

689 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