Question

I want to show ToolStripDropDown in a way as ComboBoxs dropdown is shown (or for example DateTimePickers dropdown). So I wrote this code in my Form:

private readonly ToolStripDropDown _toolStripDropDown = new ToolStripDropDown
{
    TopLevel = false,
    CanOverflow = true,
    AutoClose = true,
    DropShadowEnabled = true
};

public Form1()
{
    InitializeComponent();
    var label = new Label{Text = "Ups"};
    var host = new ToolStripControlHost(label)
    {
        Margin = Padding.Empty,
        Padding = Padding.Empty,
        AutoSize = false,
        Size = label.Size
    };

    _toolStripDropDown.Size = label.Size;
    _toolStripDropDown.Items.Add(host);
    Controls.Add(_toolStripDropDown);
}

private void button1_Click(object sender, EventArgs e)
{
    _toolStripDropDown.Show(button1.Left, button1.Top + button1.Height);
}

When I click on button my ToolStripDropDown is shown but there is no shadow, no overflow, no autoclose. What am I doing wrong?

Label in ToolStripControlHost is for simplicity. I use WinForms and .NET 4.

Update:
issue image

As you can see dropdown "Ups" doesn't overflow window, doesn't has shadow (ComboBoxs dropdown has both) and even when I clicked on ComboBox dropdown "Ups" is still visible.

Was it helpful?

Solution

You are parenting your control to the Form, which restricts it to that parent's ClipRectangle.

Remove the TopLevel designation, remove the parenting, calculate the position in screen coordinates, and finally, show the menu:

    private readonly ToolStripDropDown _toolStripDropDown = new ToolStripDropDown
    {
        //TopLevel = false,
        CanOverflow = true,
        AutoClose = true,
        DropShadowEnabled = true
    };

    public Form4()
    {
        InitializeComponent();

        var label = new Label { Text = "Ups" };
        var host = new ToolStripControlHost(label)
        {
            Margin = Padding.Empty,
            Padding = Padding.Empty,
            AutoSize = false,
            Size = label.Size
        };

        _toolStripDropDown.Size = label.Size;
        _toolStripDropDown.Items.Add(host);
        //Controls.Add(_toolStripDropDown);
    }

    private void button1_Click(Object sender, EventArgs e)
    {
        Point pt = this.PointToScreen(button1.Location);
        pt.Offset(0, button1.Height);
        _toolStripDropDown.Show(pt);

    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top