views:

239

answers:

4

I have a control which contains a NumericUpDown. The updown is only shown when the container has focus, so the container has to be selectable (or else it could never receive focus). I want the control to behave as a single entity with regards to tab order; that is, when the user tabs to the control, it shows the updown and the updown is focused; when the user tabs away from the updown, it is as if they had tabbed away from the control.

It's easy enough to achieve the first part: in the container's OnEnter, I focus the updown. If the user tabs away without shift, it also works fine, since the next control in the tab order is the correct one. However, the previous control in the tab order to the updown is the container, since it had to be selectable; so when the user shift-tabs away from the updown, the container is selected, and therefore the updown gets selected again.

How do I select the previous control to the container control, when the user shift-tabs away from the updown?

UPDATE:

My problem isn't detecting when I need to do this - it's finding the control to send focus to.

UPDATE:

SelectNextControl only seems to work within the container's parent's controls; if the container is the only control on its parent, it doesn't change focus, even if there are other controls elsewhere in the hierarchy that ought to receive focus via tab.

A: 

It's a hack, but you can use the OnEnter event coupled with a boolean variable. If the variable is set to true then you were already in your container and go to the previous control (which could be a property of your container control so you know where you are going).

If the variable is false, your just getting to your custom control and focus on the up/down.

On the exit of the container, set the variable back to false.

I'm sure there's something simpler out there, but offhand this is the quickest thing I can think of.

rie819
my problem isn't detecting when I need to do this - it's finding the control to send focus to.
Simon
A: 

if you know the direction of the tab you could use SendKeys.Send("+{TAB}"); and SendKeys.Send("{TAB}");

or you could use Control.SelectNextControl()

Hath
+1  A: 
void UserControl1_Leave(object sender, EventArgs e)
    {
        this.numericUpDown1.Visible = false;
        Control c = Parent.Controls[this.Name];
        int i = Parent.Controls.IndexOf(c);
        Parent.Controls[i - 1].Focus();
    }

I've added this leave event to a custom control and its working for me. Basically when the user shift tabs away this event sets the focus to the previous control in the parent form's control collection. Don't know if its what your looking for exactly but hopefully it will send you in the right direction.

Phil
That would be presuming that there was a previous control in the Parent.Controls collection, but with a few adjustments, taking this into account, this would be the right solution.
Joergen Bech
A: 

Actually this seems to be the default behavior for me?

eschneider
There is an overload for selectnextcontrol to go backwards:SelectNextControl(Me, False, True, False, False)
eschneider