ToolStripCombobox displays at the top left corner of the screen when DropDownStyle is Simple

959 Views Asked by At

I have a ToolStripCombobox that when I set its DropDownStyle to Simple. The first time which I open the menu, it displays at the top left corner of the screen. However, when I select the same item for the second time, it displays in the correct location.

Is there a way to prevent the code from showing the list at the top left corner of the screen?

Thank you in advance for any help.

First Time

Result 1

Second Time

Result 2

3

There are 3 best solutions below

3
On BEST ANSWER

To solve the problem put this code in the Load event of form:

var item = toolStripComboBox1;
var createControl = item.Control.Parent.GetType().GetMethod("CreateControl",
    System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
createControl.Invoke(item.Control.Parent, new object[] { true });

It's a strange bug and I don't have any idea why the ToolStripComboBox with DisplayStyle set to Simple suffers from this bug but by setting DisplayStyle to DropDown or DropDownList doesn't have this bug.

Using above code, I forced the owner ToolStripDropDownMenu be created before being shown.

0
On

By setting the ToolStripCombobox style to Dropdown in design mode and then to Simple in the form's load event, I was able to get it to open properly.

0
On

Modifying the SelectedIndex on a ToolStripComboBox before it's parent object's Click event is being called will cause this issue.

In my case the parent object was a ToolStripMenuItem. When modifying the SelectedIndex in the Click event of the ToolStripMenuItem, the issue was fixed.

I only encountered this issue in .NET Core 3.1 and not in .NET 4.x.