开发者

totally disabling TabOrder on the form

开发者 https://www.devze.com 2023-03-10 16:01 出处:网络
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.

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 s开发者_如何学运维ome similar event ....


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.


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);


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消