Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tool Strip (ToolStripDropDownButton) close and lose window focus

I have a ToolStrip with a ToolStripDropDownButton. This in turn pop up a UserControl with a DateTimePicker. When I click on the top numbers on the calendar (in green), everything works fine (user control stay open). When I click on one of the lower dates (in red), the ToolStripDropDown closes and the application loses focus. This causes the user to have to double click to open up the DropDown again (one time to focus the application, an other to open up the drop down).

This happens when the calender date are not on top of the user control. As you can see, the user control stops on the "green" dates.

I hope this is clear. This seems to be a windows bug since this happens with a very simple project. Have anyone seens this problem? Is there a fix for this?

enter image description here

[UPDATE]

I notice the same thing with dropdown.

enter image description here

It doesn't happen with the user control is very big.

enter image description here

Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load

    Dim uc As New UserControl1
    Dim cms As New ContextMenuStrip()
    cms.Items.Add(New ToolStripControlHost(uc))
    ToolStripButton1.DropDown = cms

End Sub
like image 410
the_lotus Avatar asked Dec 17 '12 18:12

the_lotus


1 Answers

Feature, not a bug. The control needs to know that the user clicked outside of the dropdown so it can then automatically close the dropdown. That's done with "mouse capture", exposed in Winforms by the Capture property. Capturing the mouse ensures that mouse messages are sent to the control, even if the mouse is no longer located inside the control window.

Perhaps it is obvious what's happening now, you are not in fact clicking on the calendar. Once the dropdown is displayed, the control captures the mouse. The click is redirected to the ToolStripDropDown. Which sees that the mouse is no longer inside the dropdown when you click in the "red area" and thus closes the dropdown window.

Dropdowns like this are really only suitable for simple items, putting any control inside of it that creates a toplevel window, like DateTimePicker or ComboBox just isn't a supported scenario.

like image 125
Hans Passant Avatar answered Sep 22 '22 22:09

Hans Passant