Better SplitButton Properties

SplitSize property

This property indicates the width of the split part in pixels. The button changes appearance as you change the value. If you set the value to 0 the control will look and behave same as the classic Button class. If you set the value to be as high as the width of the whole control then only the split part will be drawn. You can see how this property affects the control in BetterSplitButtonSamplesCS sample project:

SplitImageList property

This property allows you to display images in the split part. You can display different image for each button state. It expects - if not null - a instance of the ImageList class with six images in it. Each index in the list coresponds to a state of the button so you can animate the images according to your prefference. The states and their coresponding indexes are as follows:

C#

Normal        = 0  // mouse is not in the control
ButtonHot     = 1  // mouse is in the button area
ButtonPressed = 2  // mouse down in the button area
SplitHot      = 3  // mouse is in the split area
SplitPressed  = 4  // mouse down in the split area
Disabled      = 5  // the control is disabled


If the control won't find an image in the coresponding index of the SplitImageList it will display Normal (index 0) image.



You can find an example in the BetterSplitButtonSamplesCS.cs code file:

C#

// create the image list for the button states
ImageList splitImageList = new ImageList();
splitImageList.ImageSize = new Size(48, 48);
splitImageList.ColorDepth = ColorDepth.Depth32Bit;

// load the images for all the button states
// THE INDEX IN THE LIST CORESPONDS TO THE STATE OF THE BUTTON
splitImageList.Images.Add(Image.FromStream(thisAssembly.GetManifestResourceStream("ComponentOwl.BetterSplitButtonSamplesCS.Resources.state_normal.png")));
splitImageList.Images.Add(Image.FromStream(thisAssembly.GetManifestResourceStream("ComponentOwl.BetterSplitButtonSamplesCS.Resources.state_button_hot.png")));
splitImageList.Images.Add(Image.FromStream(thisAssembly.GetManifestResourceStream("ComponentOwl.BetterSplitButtonSamplesCS.Resources.state_button_pressed.png")));
splitImageList.Images.Add(Image.FromStream(thisAssembly.GetManifestResourceStream("ComponentOwl.BetterSplitButtonSamplesCS.Resources.state_split_hot.png")));
splitImageList.Images.Add(Image.FromStream(thisAssembly.GetManifestResourceStream("ComponentOwl.BetterSplitButtonSamplesCS.Resources.state_split_pressed.png")));
splitImageList.Images.Add(Image.FromStream(thisAssembly.GetManifestResourceStream("ComponentOwl.BetterSplitButtonSamplesCS.Resources.state_disabled.png")));

// assign the list to the button
this.bsbSplitImage.SplitImageList = splitImageList;

Visual Basic

' create the image list for the button states
Dim splitImageList As New ImageList()
splitImageList.ImageSize = New Size(48, 48)
splitImageList.ColorDepth = ColorDepth.Depth32Bit

' load the images for all the button states
' THE INDEX IN THE LIST CORESPONDS TO THE STATE OF THE BUTTON
splitImageList.Images.Add(Image.FromStream(thisAssembly.GetManifestResourceStream("ComponentOwl.BetterSplitButtonSamplesCS.Resources.state_normal.png")))
splitImageList.Images.Add(Image.FromStream(thisAssembly.GetManifestResourceStream("ComponentOwl.BetterSplitButtonSamplesCS.Resources.state_button_hot.png")))
splitImageList.Images.Add(Image.FromStream(thisAssembly.GetManifestResourceStream("ComponentOwl.BetterSplitButtonSamplesCS.Resources.state_button_pressed.png")))
splitImageList.Images.Add(Image.FromStream(thisAssembly.GetManifestResourceStream("ComponentOwl.BetterSplitButtonSamplesCS.Resources.state_split_hot.png")))
splitImageList.Images.Add(Image.FromStream(thisAssembly.GetManifestResourceStream("ComponentOwl.BetterSplitButtonSamplesCS.Resources.state_split_pressed.png")))
splitImageList.Images.Add(Image.FromStream(thisAssembly.GetManifestResourceStream("ComponentOwl.BetterSplitButtonSamplesCS.Resources.state_disabled.png")))

' assign the list to the button
Me.bsbSplitImage.SplitImageList = splitImageList


Make sure that the dimensions of the controls and the SplitSize property are set such that the images in SplitImageList can fit in the resulting split area. The images won't scale down or up to fit.



To illustrate here is an example of how different images can be displayed in each button state:

SplitGlyphScaling property

If SplitImageList property is null or empty the default glyph is drawn in the split area. By setting this property you can control size of that glyph. If set to 0, no glyph will be drawn. You can see how this property affects the control in BetterSplitButtonSamplesCS sample project.

AlwaysDropDown property

This property allows you to instruct the control to display context menu after mouse click regardless of the mouse position. In other words, if set true the button will always display context menu after mouse click. Also, it won't display the line between the button area and the split area. You can see how this property affects the control in BetterSplitButtonSamplesCS sample project.

BlockEnterAndSpaceBar property

If set true, you wont be able to perform a mouse click by hitting Enter or Space key. You can see how this property affects the control in BetterSplitButtonSamplesCS sample project.

DoubleClickEnabled property

If set true, a SplitButtonDoubleClick event will occur if you double-click in the split area.

Setting Up The Context Menu

To display the context menu you have to assign an instance of ContextMenuStrip class to the ContextMenuStrip property. If this property is null or the menu is empty it won't display anything. To set up your ContextMenuStrip intance you can you the Visual Studio designer which makes the process very easy. However if yous wish to construct your menu in code by yourself there's no harm in doing that. We even provide you with special BeforeMenuShown event where you can construct your context menu dynamically. Good example of this technique can be found in BetterSplitButtonSamplesCS.cs code file:

C#

private void bsbBrowse_BeforeMenuShown(object sender, EventArgs e)
{
    // clear any previous items
    this.cmsBrowse.Items.Clear();

    // get all files in current directory
    string[] filesInCurDir = Directory.GetFiles(Environment.CurrentDirectory);

    foreach (string fileName in filesInCurDir)
    {
        // get some info
        FileInfo fi = new FileInfo(fileName);

        // add menu item for each file
        ToolStripMenuItem tsmiNewImageFile = new ToolStripMenuItem("   " + fi.Name);
        tsmiNewImageFile.Click += new EventHandler(tsmiNewImageFile_Click);
        cmsBrowse.Items.Add(tsmiNewImageFile);
    }

    // add label and separator to Browse context menu
    ToolStripLabel tslCurDir = new ToolStripLabel("Current Directory:");
    cmsBrowse.Items.Insert(0, tslCurDir);
    cmsBrowse.Items.Insert(1, new ToolStripSeparator());
}

Visual Basic

Private Sub bsbBrowse_BeforeMenuShown(sender As Object, e As EventArgs)
  ' clear any previous items
  Me.cmsBrowse.Items.Clear()

  ' get all files in current directory
  Dim filesInCurDir As String() = Directory.GetFiles(Environment.CurrentDirectory)

  For Each fileName As String In filesInCurDir
    ' get some info
    Dim fi As New FileInfo(fileName)

    ' add menu item for each file
    Dim tsmiNewImageFile As New ToolStripMenuItem("   " + fi.Name)
    tsmiNewImageFile.Click += New EventHandler(tsmiNewImageFile_Click)
    cmsBrowse.Items.Add(tsmiNewImageFile)
  Next

  ' add label and separator to Browse context menu
  Dim tslCurDir As New ToolStripLabel("Current Directory:")
  cmsBrowse.Items.Insert(0, tslCurDir)
  cmsBrowse.Items.Insert(1, New ToolStripSeparator())
End Sub