2016-11-02 2 views
2

Ich denke, dass dies einfach zu tun sein sollte. Aber ich habe es nicht gesehen. Ich möchte etwas tun wie:Wie fügen Sie ToolStripComboBox und Separator horizontal zu einem ToolStripDropMenuItem hinzu?

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

Ich verwende ToolStripDropDownButton in einem ToolStrip. Ich programmiere w/C#. Danke für Ihre Ratschläge.

Etwas wie: https://s18.postimg.org/nd9r35jpl/c89a195a3b6e8dac6e7753af6b0b8a6c.png

Mit freundlichen Grüßen

+0

Machst du dies durch Code oder durch den Designer? https://i.gyazo.com/c89a195a3b6e8dac6e7753af6b0b8a6c.png – Jim

+0

Hallo Jim, ich habe von Designer und ich habe meine Drop-Down wie img. Aber ich lege gerne die zweite combobox2 neben combobox1. Ich weiß nicht, ob du es bekommst. –

+0

Meinen Sie, Sie möchten 2 Kombinationsfelder in demselben Dropdown-Element haben? –

Antwort

2

Es scheint, Sie suchen für eine solche Gestaltung:

enter image description here

Um s zu tun o, Sie müssen kein benutzerdefiniertes Steuerelement erstellen. Verwenden Sie einfach die allgemeinen Funktionen von . Sie müssen die Eigenschaft LayoutStyle auf einen geeigneten Wert setzen.

Beispiel

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

Das funktioniert genauso mit ToolstripDropDownButton anstelle von ToolstripDropDown? –

+0

Ich habe einen 'ToolStripDropDownButton', wie Sie sehen können. Dann habe ich ein 'ToolStripDropDown' mit einem benutzerdefinierten Layout erstellt und es als' DropDown' dieses 'ToolStripDropdownButton' festgelegt. –

+0

Oh Entschuldigung, ich hatte deinen Code bis zum Ende nicht gesehen. Danke, Reza. –

1

den Assistenten verwenden und sehen Sie die endgültige Code:

// 
// 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"; 
+0

Hi mcNets, ich habe meinen Code so. Aber ich mag es, die zweite combobox2 neben combobox1 zu platzieren, ich möchte nicht combobox1 über combobox2 (horizontale Ausrichtung auf Objekte nicht vertikal). Ich weiß nicht, ob du es bekommst. –

+0

Ändern Sie die Reihenfolge der Elemente in der 'this.toolStrip1.Items.AddRange()' Methode. – McNets

+0

Mcnets etwas wie: https://s18.postimg.org/nd9r35jpl/c89a195a3b6e8dac6e7753af6b0b8a6c.png –

Verwandte Themen