So I have come up with a custom control that is based on a toolstrip and thus fully customizable with text and images, having its own click event for each button/action. And it can be designed in the winform editor. There are a few minor layout issues like the alignment of the dropdown items, but nothing really serious.
The button will make a once clicked drop item the main item, this can be changed in the OnActions_DropDownItemClicked()
method


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.Design.Serialization;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
//System.Windows.Forms.Design.ControlDesigner
namespace YourNamespace
{
/// <summary>
/// Implements a drop button using only standard winform controls
/// </summary>
[DesignerSerializer("System.Windows.Forms.Design.ToolStripCodeDomSerializer, System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", "System.ComponentModel.Design.Serialization.CodeDomSerializer, System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")]
[Designer("System.Windows.Forms.Design.ControlDesigner, System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")]
public class DropButton : ToolStrip
{
#region Private Fields
private List<ActionButtonInfo> _actionButtons = new List<ActionButtonInfo>();
private ToolStripLayoutStyle _layoutStyle = ToolStripLayoutStyle.Flow;
private int _splitButtonWidth = 30;
private System.Windows.Forms.ToolStripDropDownButton btnActions;
private System.Windows.Forms.ToolStripButton btnMainAction;
#endregion Private Fields
#region Public Properties
/// <summary>
/// Gets or sets the action buttons.
/// </summary>
public List<ActionButtonInfo> ActionButtons
{
get
{
return this._actionButtons;
}
set
{
this._actionButtons = value;
SetupActionButtons();
}
}
/// <summary>
/// Gets or sets the drop down direction.
/// </summary>
public ToolStripDropDownDirection DropDownDirection
{
get; set;
}
/// <inheritdoc/>
[Browsable(false)]
public new ToolStripGripStyle GripStyle => ToolStripGripStyle.Hidden;
/// <inheritdoc/>
[Browsable(false)]
public new ToolStripItemCollection Items
{
get
{
return base.Items;
}
}
/// <inheritdoc/>
[Browsable(false)]
public new ToolStripLayoutStyle LayoutStyle => _layoutStyle;
public new ToolStripLayoutStyle LayoutStyle1 => ToolStripLayoutStyle.Flow;
/// <summary>
/// Gets or sets the split button width.
/// </summary>
public int SplitButtonWidth
{
get
{
return _splitButtonWidth;
}
set
{
if(value < 10 || value > this.Width)
{
throw new ArgumentOutOfRangeException();
}
_splitButtonWidth = value;
ResizeButtons();
}
}
#endregion Public Properties
#region Private Methods
/// <summary>
/// The actual implementation that adds a button to the button list
/// </summary>
/// <param name="abi">The abi.</param>
private void AddActionButtonImpl(ActionButtonInfo abi)
{
ToolStripItem tsi = new ToolStripButton
{
AutoSize = false,
Text = abi.Text,
Image = abi.Image,
Tag = abi,
Height = btnMainAction.Height,
Width = btnMainAction.Width + btnActions.Width,
TextImageRelation = TextImageRelation.ImageBeforeText,
TextAlign = ContentAlignment.MiddleLeft,
Padding = new System.Windows.Forms.Padding(2, 2, 2, 2)
};
btnActions.DropDownItems.Add(tsi);
}
private void OnActions_DropDownItemClicked(object sender, ToolStripItemClickedEventArgs e)
{
if(e.ClickedItem != null && !String.IsNullOrEmpty(e.ClickedItem.Text))
{
ActionButtonInfo abi = e.ClickedItem.Tag as ActionButtonInfo;
if(abi != null)
{
SetMainButton(abi);
abi.Clicked?.Invoke(this, null);
}
}
}
private void OnbtnActions_DropDownOpening(object sender, EventArgs e)
{
ToolStripDropDownMenu tdd = btnActions.DropDown as ToolStripDropDownMenu;
tdd.DefaultDropDownDirection = ToolStripDropDownDirection.BelowLeft;
tdd.ShowCheckMargin = false;
tdd.ShowImageMargin = false;
tdd.MinimumSize = btnMainAction.Size;
}
/// <summary>
/// Resizes the buttons.
/// </summary>
/// <param name="suspend">If true, suspend.</param>
private void ResizeButtons(bool suspend = true)
{
if(btnActions is null || btnMainAction is null)
return;
if(suspend)
this.SuspendLayout();
int marginX = (this.Margin.Left + this.Margin.Right);
int marginY = (this.Margin.Top + this.Margin.Bottom);
btnMainAction.Width = this.Width - _splitButtonWidth - marginX;
btnActions.Width = _splitButtonWidth - marginX - 1;
btnMainAction.Height = this.Height - marginY;
btnActions.Height = this.Height - marginY;
if(suspend)
this.ResumeLayout(true);
}
/// <summary>
/// Sets the main button.
/// </summary>
/// <param name="abi">The abi.</param>
private void SetMainButton(ActionButtonInfo abi)
{
btnMainAction.Image = abi.Image;
btnMainAction.Text = abi.Text;
// btnMainAction.Click += abi.Clicked;
btnMainAction.Tag = abi;
}
/// <summary>
/// Setups the action buttons.
/// </summary>
private void SetupActionButtons()
{
if(_actionButtons.Count == 0)
{
btnActions.Enabled = false;
return;
}
btnActions.Enabled = true;
SetMainButton(_actionButtons[0]);
foreach(ActionButtonInfo abi in _actionButtons)
{
AddActionButtonImpl(abi);
}
btnActions.DropDownOpening += OnbtnActions_DropDownOpening;
}
#endregion Private Methods
#region Protected Methods
/// <inheritdoc/>
protected override void OnCreateControl()
{
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(DropButton));
base.OnCreateControl();
this.btnMainAction = new System.Windows.Forms.ToolStripButton();
this.btnActions = new System.Windows.Forms.ToolStripDropDownButton();
this.SuspendLayout();
this.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.btnMainAction,
this.btnActions});
this.MinimumSize = new Size(100, 40);
base.GripStyle = System.Windows.Forms.ToolStripGripStyle.Hidden;
base.LayoutStyle = System.Windows.Forms.ToolStripLayoutStyle.Flow;
this.AutoSize = false;
this.Dock = DockStyle.None;
// this.ItemClicked += new System.Windows.Forms.ToolStripItemClickedEventHandler(this.toolStripAction_ItemClicked);
//
// btnMainAction
//
this.btnMainAction.AutoSize = false;
this.btnMainAction.BackColor = System.Drawing.Color.Gainsboro;
this.btnMainAction.ForeColor = System.Drawing.Color.Black;
this.btnMainAction.Font = new System.Drawing.Font("Segoe UI", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point);
this.btnMainAction.ImageScaling = System.Windows.Forms.ToolStripItemImageScaling.None;
this.btnMainAction.ImageTransparentColor = System.Drawing.Color.Magenta;
this.btnMainAction.Name = "btnMainAction";
this.btnMainAction.Size = new System.Drawing.Size(this.Width, this.Height);
this.btnMainAction.Text = "Test";
//
// btnActions
//
this.btnActions.AutoSize = false;
this.btnActions.AutoToolTip = false;
this.btnActions.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image;
this.btnActions.BackColor = System.Drawing.Color.Gainsboro;
this.btnActions.ForeColor = System.Drawing.Color.Black;
this.btnActions.Font = new System.Drawing.Font("Segoe UI", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point);
this.btnActions.Image = Properties.Resources.DropButtonArrow;
this.btnActions.ImageScaling = System.Windows.Forms.ToolStripItemImageScaling.None;
this.btnActions.Name = "btnActions";
this.btnActions.ShowDropDownArrow = false;
this.btnActions.Size = new System.Drawing.Size(_splitButtonWidth, this.Height);
this.btnActions.TextImageRelation = System.Windows.Forms.TextImageRelation.Overlay;
btnActions.DropDownDirection = ToolStripDropDownDirection.BelowLeft;
btnActions.DropDownItemClicked += OnActions_DropDownItemClicked;
ResizeButtons(false);
this.ResumeLayout(false);
this.PerformLayout();
}
/// <summary>
/// Propagate font changes to the child controls
/// </summary>
/// <param name="e"></param>
protected override void OnFontChanged(EventArgs e)
{
base.OnFontChanged(e);
if(btnActions is null || btnMainAction is null)
return;
btnMainAction.Font = this.Font;
btnActions.Font = this.Font;
}
/// <inheritdoc/>
protected override void OnLayout(LayoutEventArgs e)
{
ResizeButtons(false);
base.OnLayout(e);
}
#endregion Protected Methods
#region Public Methods
/// <summary>
/// Adds an action button.
/// </summary>
/// <param name="actionButtonInfo">The action button info.</param>
public void AddActionButton(ActionButtonInfo actionButtonInfo)
{
_actionButtons.Add(actionButtonInfo);
if(_actionButtons.Count == 1)
SetupActionButtons();
else
AddActionButtonImpl(actionButtonInfo);
}
#endregion Public Methods
}
}