This may be a simple question and it may be a complex question. I am not sure.

Lets assume I have a windows form app that has three different areas: Area 1, Area 2. If there a way to tell the program when I am assigning tabbing order that I want a separate tabbing order for each area and I do not want them to overlap. What I mean is I want a separate order for Area 1 and Area 2. When I reach the end of the tabbing order in Area 1 I want it to roll to the top of the order in Area 1, not move to Area 2.

Is there a way to do this? Thanks.

Recommended Answers

All 3 Replies

You could create each area as a frameless child window of a MDI form. That way they will act as independent windows regarding tab (and events).

You should be able to capture the TAB press and override the tab action.
I had a go using four buttons and a few other controls on a form.
Tab order was set to go from button1, button2, button3, button4 then on to other controls.
The following code kept the tab action looping around the buttons.
(However it does not prevent the tab from the other controls arriving at the buttons)

// flag to record when tab is pressed
bool TabPressed = false;

private void button4_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
{
    // capture tab key press (with no shift)
    if (e.KeyCode == Keys.Tab && !e.Shift)
    {
        TabPressed = true;
    }
}

private void button4_Leave(object sender, EventArgs e)
{
    // if exiting this control by TAB then go to button1
    if (TabPressed)
    {
        this.button1.Select();
        TabPressed = false;
    }
}

private void button1_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
{
    // capture tab key press (with shift)
    if (e.KeyCode == Keys.Tab && e.Shift)
    {
        TabPressed = true;
    }
}

private void button1_Leave(object sender, EventArgs e)
{
    // if exiting this control by TAB then go to button4
    if (TabPressed)
    {
        button4.Select();
        TabPressed = false;
    }
}

I came up with another method which I think is a bit tidier.
This time, put the objects in panels to define the areas.
On capturing the TAB key for the first and last controls store the desired next control in a variable.
Capture when focus leaves the panel and go to the required control.

// Control for next tab override
Control TabOverrideControl = null;

private void button4_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
{
    // capture tab key press (with no shift)
    if (e.KeyCode == Keys.Tab && !e.Shift)
    {
        TabOverrideControl = button1;
    }
}

private void button1_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
{
    // capture tab key press (with shift)
    if (e.KeyCode == Keys.Tab && e.Shift)
    {
        TabOverrideControl = button4;
    }
}

private void panel1_Leave(object sender, EventArgs e)
{
    // if leaving the area and TAB override control set then go to new control
    if (TabOverrideControl != null)
    {
        TabOverrideControl.Select();
        TabOverrideControl = null;
    }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.