Question

I'm creating a ToolStripDropDownButton with three ToolStripButtons. And I want to add a Separator after the second button.

Here's the code I have.

using System;
using System.Drawing;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        internal ToolStripDropDownButton dropDownButton1;
        internal ToolStripDropDown dropDown;
        internal ToolStripButton buttonRed;
        internal ToolStripButton buttonBlue;
        internal ToolStripButton buttonYellow;

        public Form1()
        {
            InitializeComponent();

            dropDownButton1 = new ToolStripDropDownButton();
            dropDown = new ToolStripDropDown();
            dropDownButton1.Text = "A";

            dropDownButton1.DropDown = dropDown;
            dropDownButton1.DropDownDirection = ToolStripDropDownDirection.Right;
            dropDownButton1.ShowDropDownArrow = false;

            buttonRed = new ToolStripButton();
            buttonRed.ForeColor = Color.Red;
            buttonRed.Text = "A";

            buttonBlue = new ToolStripButton();
            buttonBlue.ForeColor = Color.Blue;
            buttonBlue.Text = "A";

            buttonYellow = new ToolStripButton();
            buttonYellow.ForeColor = Color.Yellow;
            buttonYellow.Text = "A";

            ToolStripSeparator s = new ToolStripSeparator();

            dropDown.Items.AddRange(new ToolStripItem[] { buttonRed, buttonBlue, s, buttonYellow });
            toolStrip1.Items.Add(dropDownButton1);
        }
    }
}

The problem is the Separator is displaying vertically.

enter image description here

How can I make it display horizontally?

Était-ce utile?

La solution

You need to set the ToolStripDropDown's LayoutStyle property. By default it's ToolStripLayoutStyle.Flow, but has to be set to ToolStripLayoutStyle.VerticalStackWithOverflow.

Alternatively, you can also skip creating and configuring a ToolStripDropDown instance and add the ToolStripItems directly to your ToolStripDropDownButton using its DropDownItems property:

dropDownButton1.DropDownItems.AddRange(
    new ToolStripItem[] { buttonRed, buttonBlue, s, buttonYellow });
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top