为什么这个ToolStripControlHost不起作用?

时间:2012-04-18 12:53:37

标签: c# winforms toolstripcontrolhost

我试图模仿this回答,但这有效:

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

这不是:

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);
    }
}

为什么?

我这样称呼他们:

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

1 个答案:

答案 0 :(得分:6)

设置面板的最小尺寸:

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;
  }
}