Search code examples
c#winformstoolstripcontrolhost

Why doesn't this ToolStripControlHost work?


I'm trying to emulate this answer but though this works:

public class TrackBarMenuItem : ToolStripControlHost
{
    TrackBar trackBar;
    public TrackBarMenuItem()
        : base(new TrackBar())
    {
        trackBar = Control as TrackBar;
    }
}

This doesn't:

public class PanelMenuItem : ToolStripControlHost
{
    Panel panel;
    public PanelMenuItem()
        : base(new Panel())
    {
        panel = Control as Panel;
        Visible = true;
        Enabled = true;
        panel.AutoSize = false;
        panel.Size = new Size(100, 50);
    }
}

Why?

I'm calling them like this:

contextMenuStrip1.Items.Add(new TrackBarMenuItem());
contextMenuStrip1.Items.Add(new PanelMenuItem());

Solution

  • Set the minimum size of the Panel:

    public class PanelMenuItem : ToolStripControlHost {
      Panel panel;
      public PanelMenuItem()
        : base(new Panel()) {
        panel = Control as Panel;
        Visible = true;
        Enabled = true;
        panel.AutoSize = false;
        panel.Size = new Size(100, 50);
    
        panel.MinimumSize = panel.Size;
      }
    }