Solved

e.Handled = true doesn't work OnKeyDown

Posted on 2006-06-28
8
584 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
 

Author Comment

by:udibs
ID: 17001322
I did,
OnTabPressed() returns true and not false as I said.
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

743 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now