.Net Toolstrip/MenuStrip Focus Issues

和自甴很熟 提交于 2019-12-24 02:08:24

问题


No matter what the scenario may be I'm able to recreate this annoying problem 100% of the time. Create a .Net project, C# or VB.Net. Add a ToolStrip control to the form. Create a few simple DropDownButton(s) that contain at least 2 menu items. Add any other controls you wish, a list box (populate it so it can receive focus correctly) and a ComboBox control. Either assign shortcut keys or enable TabStop on the ToolStrip so that it can receive focus by Keyboard.

Run the project (Debug/Release, which ever you fancy). Use your Keyboard to give the ToolStrip Focus (by tab or shortcut key). Arrow down into a sub item. Now select the escape key to collapse the Toolstrip sub menu. Tab to the ListBox or ComboBox that contains a few Items. All looks great right? Now use your arrow keys to navigate in these controls... Surprise! your back on the ToolStrip and the control you thought had focus doesn't!

I've tried multiple things to force focus on the ListBox. One example is I'd add the event handler for OnEnter (ListBox.Enter+=...) and add some code like:

ListBox.Focus();
ListBox.Select(); 

Nothing was a success... It seems like once the menu expands on a toolstrip you will be forever stuck on this control using your Keyboard... This is important for me to resolve due to the fact that i work with blind users whom use keyboard navigation only... Is this a bug? I cannot reproduce this in MFC...

Any suggestions?

Update I was able to find a control that doesn't reproduce this strangeness...

System.Windows.Forms.MainMenu is the only "Tool Bar object" that doesn't behave like the others...

I'd still like some feedback on the above though (Help for others and myself)...

Update 2 The underlying issue is within [ToolStripObject].TabFocus property... if set to false all seems to work ok... giving focus back to the control that "looks" like it's focused. But having that capability to allow a blind user to navigate throughout all UI controls via tab is a handy thing to implement... it's too bad this property doesn't work like it should....


回答1:


I got it to work by overriding the ToolStripMenuItem:

public class ToolStripMenuItemEx : ToolStripMenuItem {

  protected override bool ProcessCmdKey(ref Message m, Keys keyData) {
    if (keyData == Keys.Escape) {
      ToolStripDropDownButton tb = this.OwnerItem as ToolStripDropDownButton;
      if (tb != null) {
        tb.HideDropDown();
        return false;
      }
    }
    return base.ProcessCmdKey(ref m, keyData);
  }
}


来源:https://stackoverflow.com/questions/14238151/net-toolstrip-menustrip-focus-issues

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!