Reputation: 1351
I want to show ToolStripDropDown
in a way as ComboBox
s dropdown is shown (or for example DateTimePicker
s 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:
As you can see dropdown "Ups" doesn't overflow window, doesn't has shadow (ComboBox
s dropdown has both) and even when I clicked on ComboBox
dropdown "Ups" is still visible.
Upvotes: 2
Views: 5782
Reputation: 11025
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);
}
Upvotes: 6