Newbie question: how to set focus to a usercontrol

C

Christian Stapfer

I have a usercontrol that contains another usercontrol,
which in turn contains a PictureBox control.
Under a certain condition, I need to set the input
focus on the outermost usercontrol. But once the PictureBox
control (the outermost usercontrol's grandchild) has got
the input focus, I cannot transfer the focus back to the
outermost usercontrol (at least not by simply invoking its
Focus() member function).
Why is this? What can I do to take away the keyboard input
focus from that grandchild, the PictureBox control, and
transfer it back to the outermost usercontrol? (I have tried
to find information about keyboard focus handling that seems
relevant to this question, but what I have found seems rather
far removed from what I need to understand here...)

Note: My application requires very little keyboard input:
mainly a few keyboard-accelerators, and the copy/paste/
cut/delete key. But the copy/paste/cut/delete key must
go to the right usercontrol, because copy/paste/cut/delete
must be handled differently, depending on which usercontrol
has the input focus (or, rather, should have it, from my
point of view). In essence, I am trying to base input
focus on the position of the mouse pointer: if that grandchild
PictureBox control does not have the mouse pointer within
its bounds, it should not receive keyboard input, i.e.
should not have input focus.

Regards and thanks in advance for any enlightenment on
this question,
Christian
 
S

Stoitcho Goutsev \(100\)

Christian,

UserControl as a container control doesn't get the input focus per se.If you
try to focus this control it transfers the focus to one of its children.
Further more a control have to be able to process shortcuts and alike even
if it doesn't have the focus. Otherwise imaging if you have a text aditor
and you want to save the file by pressing Ctrl+S and you have to click on
the frame first; it is not very useful, istn't it.

To implement your shortcuts system take a look ProcessKeyPreview method as
well as on the other keybiard methods that starts ProcessXXXX and are
declared for the UserControl class.

HTH
 

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