我希望以ToolStripDropDown
下拉列表的方式显示ComboBox
(例如DateTimePicker
下拉列表。所以我在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);
}
当我点击按钮时,显示ToolStripDropDown
,但没有阴影,没有溢出,没有自动关闭。我究竟做错了什么?
Label
中的 ToolStripControlHost
是为了简单起见。我使用WinForms和.NET 4.
更新
正如你可以看到下拉列表“Ups”没有溢出窗口,没有阴影(ComboBox
s下拉列表都有),甚至当我点击ComboBox
下拉列表时“Ups”仍然是可见。
答案 0 :(得分:4)
您正在将您的控件呈现给表单,该表单将其限制为该父表单ClipRectangle
。
删除TopLevel
标识,删除父项,计算屏幕坐标中的位置,最后显示菜单:
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);
}