0

I have a ToolStripMenuItem that contain submenus to select from. The problem is that they are displaying in the wrong place:enter image description here

I have this code that I used for the submenu (this was for the ToolStripCombobox -Thank you Reza for the solution-) of the above items but I'm having a hard time tweaking in to make it work for the ToolStripMenuItem as it does not contain a Control.Parent.GetType() :

private void Form_Load(object sender, EventArgs e)
    {
        var item = toolStripComboBox;
        var createControl = item.Control.Parent.GetType().GetMethod("CreateControl",
            System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
        createControl.Invoke(item.Control.Parent, new object[] { true });

As always, any help is appreciated.

Community
  • 1
  • 1
Pucho
  • 370
  • 2
  • 20

1 Answers1

0

I think the root of your whole problem is your using the form load event handler instead of the form constructor. When I run the following code, the menu items are in exactly the right place when the form loads:

public Form1()
{
    InitializeComponent();
    ToolStripComboBox item1 = new ToolStripComboBox();
    item1.Items.AddRange(new object[]
    {
        "One",
        "Two",
        "Thtree"
    });
    item1.DropDownStyle = ComboBoxStyle.Simple;
    menuStrip1.Items.Add(item1);
    ToolStripMenuItem item2 = new ToolStripMenuItem();
    item2.Text = "Four";
    menuStrip1.Items.Add(item2);
}
tinstaafl
  • 6,908
  • 2
  • 15
  • 22
  • I'm marking this as an accepted answer because it prompted me to build the same application from scratch just to see why tinstaafi was able to render the menu without any problems. Somewhat, somehow, something is messed up in the original form as the new form displays all the items without any issues. – Pucho Nov 23 '16 at 00:37