Bohn
Bohn

Reputation: 26919

totally disabling TabOrder on the form

WinForms: I don't want any tab order. I want myself be able to programatically handle all the tab orders on the form with some logic that I need. How can I completely disable tab order? I assume after that I should deal with KeyDown event of each contorl or some similar event ....

Upvotes: 0

Views: 3382

Answers (4)

Hans Passant
Hans Passant

Reputation: 942267

You need to override the form's ProcessCmdKey() method. Test keydata == Keys.Tab and (Keys.Shift | Keys.Tab) to detect respectively a forward and a backward tab. Return true to indicate that you've used the key and it shouldn't be used anymore. Which defeats Winforms default handling for the Tab key. No additional changes are needed to the controls.

The form's ActiveControl property tells you which control currently has the focus, you'll need to use it to figure out which control should be focused next. Beware that it can technically be null. Watch out for controls that are embedded in a container control, like a Panel or UserControl. Making this work is definitely unpleasant, also very hard to maintain. Only do this if there are a limited number of controls on the form.

Upvotes: 3

DRapp
DRapp

Reputation: 48179

In addition to disabling the tab stops for the pageframe, as you mentioned, YOU want to control which "tab" is active. You can have a custom property on your form of "WhichTab" should be shown. Then, override the click event and check if the incoming sender/eventarg page is that of another page... no matter what, force focus back to the "WhichTab" YOU are in control of setting... When ready to activate said page, tell the tab control object to ACTIVATE the new page to get displayed to the user.

Upvotes: -1

DeveloperX
DeveloperX

Reputation: 4683

As Adrian said by setting tab stop to false you can disable it a Function like this can be usefull to diable all tabstop

private void DiableTabStop(Control ctrl)
{
    ctrl.TabStop = false;
    foreach (Control item in ctrl.Controls)
    {
        DiableTabStop(item);
    }
}

and calling it at form load

DiableTabStop(this);

Upvotes: 2

adrianbanks
adrianbanks

Reputation: 83004

One approach is to set the TabStop property of every control in the form to false. This will prevent the tab key from giving the controls focus.

If you don't want to do this manually for every control (e.g. in the design view), you can create a method that will iterate over all of the controls in the form's Controls collection and set the property on each one, then call it from your form's constructor.

Upvotes: 1

Related Questions