How to keep focus on a ToolStripControlHost's Owner in a ToolStrip

1.4k Views Asked by At

Using C# with .Net 2.0 (though the problem occurs in 4.0)

There appears to be a bug involving the focus of the ToolStrip control and ToolStripControlHosts in Windows Forms.

Consider this example: The user clicks on ToolStripMenuItem "Test A", then diagonally moves the mouse onto the drop down ToolStripMenuItem "5". When dragging diagonally the mouse passes over "Test B", but when the mouse enters the drop down list, the focus is moved back from "Test B" to "Test A" automatically. This works correctly.

Example 1 : https://i.stack.imgur.com/0X9mZ.png

Now consider a similar example but with a ToolStripControlHost in the drop down list. The mouse moves diagonally, passing over "Test B" which receives focus. When the mouse enters the ToolStripControlHost, the focus is NOT returned to the parent "Test A", and therefore the drop down list closes and the ToolStripControlHost is hidden.

Example 2 : https://i.stack.imgur.com/XJ75K.png

My question is, how can I handle this situation? Is there a way to force the focus onto the ToolStripMenuItem "Test A"?

I have tried setting the autoclose property of "Test A"'s DropDown to false, and then manually handling the close event, but this causes several issues. When the ToolStripControlHost is clicked, the ToolStrip receives an OnClose event with the ToolStripDropDownCloseReason being AppClicked. Additionally any textboxes within the ToolStripControlHost do not receive text events because the ToolStrip appears to be consuming them (backspace moves the focus up in the drop down list, rather than deleting text etc.).

Thanks for your help

Simon

1

There are 1 best solutions below

0
On

So, I ran into similar issues when I wanted to add Controls into the ToolStrip's DropDown items. The first part of this solution, at least in my case, involved creating custom UserControls that contained everything I needed. However, even when doing this, I still had focus issues until I placed everything into LayoutControls. Everything worked as expected, save for the DevExpress CheckedComboBoxEdit, as it grabs focus, and without the DropDown item having a handle, it loses focus and is lost.

I also tried managing a lot of the events, and setting AutoClose to false, but it ended up creating other issues.

My suggestion would be to create a UserControl with a LayoutControl to contain your textbox (and other items if necessary). This will also allow you to put any additional functionality (events, binding, etc.) that you want to hook into the textbox in the code behind the UserControl. I hope this helps, let me know if you have any additional questions.