如何将两个ToolStripComboBox 和Separator Horizo​​ntally 添加到一个ToolStripDropMenuItem?

How to add two ToolStripComboBox and Separator Horizontally to one ToolStripDropMenuItem?

我认为这应该很容易做到。但我没见过。我想做类似的事情:

DropDown -> DropDownItem1 [ComboBox1 [|] ComboBox2], DropDownItem2 [...], DropDownItem3 [...];

我在 ToolStrip 中使用 ToolStripDropDownButton。我正在用 C# 编程。谢谢你的建议。

类似于:https://s18.postimg.org/nd9r35jpl/c89a195a3b6e8dac6e7753af6b0b8a6c.png

此致

使用向导并查看最终代码:

// 
// toolStrip1
// 
this.toolStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.toolStripComboBox1,
this.toolStripComboBox2,
this.toolStripSeparator1,
this.toolStripDropDownButton1});
this.toolStrip1.Location = new System.Drawing.Point(0, 0);
this.toolStrip1.Name = "toolStrip1";
this.toolStrip1.Size = new System.Drawing.Size(771, 25);
this.toolStrip1.TabIndex = 2;
this.toolStrip1.Text = "toolStrip1";
// 
// toolStripComboBox1
// 
this.toolStripComboBox1.Name = "toolStripComboBox1";
this.toolStripComboBox1.Size = new System.Drawing.Size(121, 25);
// 
// toolStripComboBox2
// 
this.toolStripComboBox2.Name = "toolStripComboBox2";
this.toolStripComboBox2.Size = new System.Drawing.Size(121, 25);
// 
// toolStripSeparator1
// 
this.toolStripSeparator1.Name = "toolStripSeparator1";
this.toolStripSeparator1.Size = new System.Drawing.Size(6, 25);
// 
// toolStripDropDownButton1
// 
this.toolStripDropDownButton1.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image;
this.toolStripDropDownButton1.Image = ((System.Drawing.Image)(resources.GetObject("toolStripDropDownButton1.Image")));
this.toolStripDropDownButton1.ImageTransparentColor = System.Drawing.Color.Magenta;
this.toolStripDropDownButton1.Name = "toolStripDropDownButton1";
this.toolStripDropDownButton1.Size = new System.Drawing.Size(29, 22);
this.toolStripDropDownButton1.Text = "toolStripDropDownButton1";

看来您正在寻找这样的布局:

为此,您不需要创建任何自定义控件。只需使用 ToolStrip 的共同特征。您需要将 LayoutStyle 属性 设置为合适的值。

例子

private void Form1_Load(object sender, EventArgs e)
{
    var dropdown = new ToolStripDropDown();

    //Define style
    dropdown.LayoutStyle = ToolStripLayoutStyle.Table;
    var settings = (dropdown.LayoutSettings as TableLayoutSettings);
    settings.ColumnCount = 3;

    //First Item    
    var item1 = new ToolStripMenuItem("Some Sub Menu");
    dropdown.Items.Add(item1);
    settings.SetColumnSpan(item1, 3); //Set column span to fill the row

    //First Combo
    var combo1 = new ToolStripComboBox("combo1");
    combo1.Items.AddRange(new string[] { "Item1", "Item2", "Item3" });
    dropdown.Items.Add(combo1);

    //Separator
    dropdown.Items.Add("-");

    //Second Combo
    var combo2 = new ToolStripComboBox("combo2");
    combo2.Items.AddRange(new string[] { "Item1", "Item2", "Item3" });
    dropdown.Items.Add(combo2);

    //Last item
    var item2 = new ToolStripMenuItem("Some Othe Sub Menu");
    dropdown.Items.Add(item2);
    settings.SetColumnSpan(item2, 3); //Set column span to fill the row

    toolStripDropDownButton1.DropDown = dropdown;
}