bright, fresh software
Downloads  |  Buy

SplitButton control, written in C#

SplitButton control, written in C#

Postby Wyatt » July 11th, 2007, 11:06 pm

A week ago I posted a short article describing the process of fixing a SplitButton control. The control was based on this control by a former MS employee.

My SplitButton fixes many of the bugs in her implementation, but owes to her stable base.

Comments, suggestions, and bug-fixes are welcome.

Update July 21, 2007: v1.4 is out, which fixes a null exception if the SplitMenu isn't set. In this version, if you don't set the SplitMenu property, the SplitButton will act like a regular button.

Update July 28, 2007: v1.5 is out. This fixes the small bug of the focus rectangle (i.e. the dotted line) being shown in all cases of focus, when it's only supposed to be shown when the button receives focus from the keyboard.

Update February 8, 2008: v1.6 is out. I've added support for image display and both image and text alignment. There are also many small fixes.

Update May 16, 2008: v2.0 is out. The SplitButton now works with 'ContextMenu' controls in addition to the existing support for 'ContextMenuStrip' controls. This was added so you can use the newly released VistaMenu with the Split Button.


More information, the latest changes, and the 'How-to-use' info.

Image Download the SplitButton now. It works with all .NET 2.0 and above languages (C#, VB.NET, etc.)
User avatar
Wyatt
Site Admin
 
Posts: 3097
Joined: July 11th, 2007, 10:30 pm
Location: New Hampshire

Re: SplitButton control, written in C#

Postby Fare » July 12th, 2007, 9:04 am

This doesn't exactly fall into the category of "Comments, suggestions, and bug-fixes", but more or less a question. I like the feature of a splitbutton, but I am unable to get it to do anything besides throw down a menu and make as selection (and click it). How do you set up the event handler so that when you click the button (after the selection is made) to read the selection (from the context menu). I've been really frustrated trying to figure this out and I figure your a good enough person to ask, since you've implemented it and everything.

Thank you,

Fare
Fare
 
Posts: 5
Joined: July 12th, 2007, 9:06 am

Re: SplitButton control, written in C#

Postby Wyatt » July 12th, 2007, 11:58 am

Fare wrote:This doesn't exactly fall into the category of "Comments, suggestions, and bug-fixes", but more or less a question. I like the feature of a splitbutton, but I am unable to get it to do anything besides throw down a menu and make as selection (and click it). How do you set up the event handler so that when you click the button (after the selection is made) to read the selection (from the context menu). I've been really frustrated trying to figure this out and I figure your a good enough person to ask, since you've implemented it and everything.

Thank you,

Fare


Your question, if I understand it correctly, is how to get event handlers for the menu items. If this is your question, you can do it two ways.

1. The easy way

In Visual Studio designer, click the menu you want to add the even to, then double click one of the sub-items. Visual Studio will automatically generate the click method for that sub-item.

2. The more flexible way

You can also handle more than one of the sub-items in the same method:
multimnuclick.png
Event handler for the submenu to handle multiple clicks.
multimnuclick.png (3.82 KiB) Viewed 10039 times


After you double click that event handler from the properties window, you can use this code to figure out which item was selected (where menuUpload is the name of my sub-menu):

Code: Select all
        private void menuUpload_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            int clickedIndex = menuUpload.Items.IndexOf(e.ClickedItem);

            //clickedIndex is the index of the click menu Item
            //now use clickedIndex to determine you action
        }


I hope I've answered your question.
User avatar
Wyatt
Site Admin
 
Posts: 3097
Joined: July 11th, 2007, 10:30 pm
Location: New Hampshire

Re: SplitButton control, written in C#

Postby Fare » July 12th, 2007, 12:35 pm

yes and no...

Yes,in a sense because you showed me that I am setting it up wrong.

Your menuupload sub-menu is what your designing, but it appears that I was designing the actual split button class.

I have the designer set up like...

Code: Select all
            //
            // splitButton
            //
            this.splitButton.ContextMenuStrip = new System.Windows.Forms.ContextMenuStrip();
            this.splitButton.ContextMenuStrip.Items.Add("Item1");
            this.splitButton.ContextMenuStrip.Items.Add("Item2");
            this.splitButton.ContextMenuStrip.Items.Add("Item3");
            this.splitButton.AutoSize = true;
            yadda yadda yadda


This is the way that it was set up on the jfo's site...I think

Is there something else I need to declare? I noticed the path for the menuUpload was System.Windows.etc.etc.etc

Thank you again,

Fare
Fare
 
Posts: 5
Joined: July 12th, 2007, 9:06 am

Re: SplitButton control, written in C#

Postby Wyatt » July 12th, 2007, 1:05 pm

It almost looks like you're directly editing the "FormName.Designer.cs" file. Normally that's not recommended.

What you want to do is double click the the FormName:

solexplorer.png
In this case, double click Form1.cs
solexplorer.png (5.01 KiB) Viewed 10029 times


Then, in the ToolBox, click the ContextMenuStrip and drag it to the form.

dragtoform.png
Click, then drag the ContextMenuStrip to the form.
dragtoform.png (6.07 KiB) Viewed 10030 times


Visual Studio will automatically take care of all the namespaces (like using System.Windows.Forms; //etc.)
User avatar
Wyatt
Site Admin
 
Posts: 3097
Joined: July 11th, 2007, 10:30 pm
Location: New Hampshire

Re: SplitButton control, written in C#

Postby Fare » July 12th, 2007, 1:59 pm

lol, wow...

As you can probably tell, I'm new to the IDE scene...This is a long way from c++ in VIM... It almost seems like cheating...

I got it all to work, and I thank you for that. I was forced to go into the designer class source code to manually add the new contextmenu to the splitbutton, is there a way you can do it from the designer window?

Thank you again, and sorry for the inconvenience (I'm sure this wasn't the intent of your original post),

Fare
Fare
 
Posts: 5
Joined: July 12th, 2007, 9:06 am

Re: SplitButton control, written in C#

Postby Wyatt » July 12th, 2007, 2:22 pm

... is there a way you can do it from the designer window?


Sure, just click the contextMenuStrip you created, and click the "..." button in the Items property:

edititems.png
Edit menu items
edititems.png (5.04 KiB) Viewed 10020 times


From there you can Add, edit, and remove the menu items.

Thank you again, and sorry for the inconvenience (I'm sure this wasn't the intent of your original post)


It's no problem at all. I was glad to help.
User avatar
Wyatt
Site Admin
 
Posts: 3097
Joined: July 11th, 2007, 10:30 pm
Location: New Hampshire

Re: SplitButton control, written in C#

Postby Fare » July 12th, 2007, 2:27 pm

Wyatt wrote:Sure, just click the contextMenuStrip you created, and click the "..." button in the Items property:


I was talking more along the lines of adding the context menu to the splitbutton

Code: Select all
this.splitButton1.ContextMenuStrip = this.contextMenuStrip1;


I couldn't find that option in the Items field...
Fare
 
Posts: 5
Joined: July 12th, 2007, 9:06 am

Re: SplitButton control, written in C#

Postby Wyatt » July 12th, 2007, 2:45 pm

Oh, ok. Well, with my fixed version of the SplitButton it's the SplitMenu property you'll need to set:

sbmenu.png
SplitMenu property of the SplitButton
sbmenu.png (4.24 KiB) Viewed 10009 times


I hid the ContextMenuStrip property in my SplitButton because it has quirky behavior when you right click the SplitButton in runtime.
User avatar
Wyatt
Site Admin
 
Posts: 3097
Joined: July 11th, 2007, 10:30 pm
Location: New Hampshire

Re: SplitButton control, written in C#

Postby Fare » July 12th, 2007, 3:03 pm

ah, the splitmenu field ( I should have known that after going through that code a hundred times...)

thank you again,

Fare
Fare
 
Posts: 5
Joined: July 12th, 2007, 9:06 am

Next

Return to Open Source Support