2

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

Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
Pucho
  • 370
  • 2
  • 20

3 Answers3

2

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.

Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
  • Perfect solution, thank you Reza. Also, thank you for editing the question too. – Pucho Nov 02 '16 at 23:14
  • BTW, When you accept an answer, it would be great if you also vote for the answer by click on up arrow near the post. It's not compulsory at all, but it's common, reasonable and recommended. For more information about how does accepting answers work see this [post](http://meta.stackexchange.com/questions/23138/how-to-accept-the-answer-on-stack-overflow). – Reza Aghaei Nov 05 '16 at 07:28
  • Hi Reza, I did it the first time, but I did not have enough reputation. Now I do. Thanks again. – Pucho Nov 07 '16 at 18:08
1

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.

HowlinMad
  • 11
  • 1
0

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.

r3verse
  • 1,000
  • 8
  • 19