Capturing the Tab key in a usercontrol

D

Dom

I've created a user control that contains a textbox and a listbox. I
want to capture the tab key when it is pressed in the textbox. To do
this, I override the ProcessDialogKey event in the user control. But
it occurs to me that I am now processing the tab key when it is
pressed in either the textbox or the listbox. \

How do I override the textbox's event?

The only thing I can think of is that I have to create a MyTextBox
class that inherits the TextBox class, override the event there, and
then use this MyTextBox in my user control. But is there a simpler
way.

Dom
 
B

Ben Voigt [C++ MVP]

Dom said:
I've created a user control that contains a textbox and a listbox. I
want to capture the tab key when it is pressed in the textbox. To do
this, I override the ProcessDialogKey event in the user control. But

ProcessDialogKey is a method, not an event, and that's the root of your
problem. Turns out it is called by PreProcessMessage, but there I can't see
any way to hook in without deriving a new class.

Also, the bubbling up to the parent occurs to ProcessDialogKey, so you only
have access to the Keys data, not the entire message (which would let you
see which Control received the event).
it occurs to me that I am now processing the tab key when it is
pressed in either the textbox or the listbox. \

How do I override the textbox's event?

The only thing I can think of is that I have to create a MyTextBox
class that inherits the TextBox class, override the event there, and
then use this MyTextBox in my user control. But is there a simpler
way.

The only thing I can think of is to check the ActiveControl property of your
UserControl from inside your override of ProcessDialogKey.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top