Level 200
DevForce Express
July 11, 2006
There’s a nifty “split button” control among the .NET 2.0 controls that combines the functions of a regular button and a drop down. Clicking the main button runs the current “Default” command. Clicking the drop down button reveals alternative variants on that command. The user can pick any one of the listed items to run instead.
We thought you might enjoy an example to supplement the somewhat obscure documentation from Microsoft.
The Funhouse employee page places a split button on a ToolStrip. The default action of the split button’s main button reverses all pending changes to all employees.
The drop down offers two flavors of undo:
- “Undo All”, the default
- “Undo Current” which reverses changes to the current employee only
We decided that an item selected from the drop down should become the new default item. The illustration shows the user picking “Undo Current”. After selection, this becomes the main button’s default
The coding game plan is simple:
- Add menu items to the SplitButton’s DropDownItems collection
- Attach a click handler to each menu item; the handler performs the corresponding command
- Write a method (SetUndoButtonDefaultItem) to (re)set the SplitButton’s default menu item and update its appearance
- Call that default set method once during configuration and in each of the menu item handlers
C#:
private void ConfigureUndoSplitButton() {
ToolStripItemCollection dropDownItems =
Page.UndoSplitButton.DropDownItems;
ToolStripItem item;
item = dropDownItems.Add("Undo All");
item.Click += UndoAllHandler;
SetUndoButtonDefaultItem(item); // initial default.
item = dropDownItems.Add("Undo Current");
item.Click += UndoCurrentHandler;
}
private void SetUndoButtonDefaultItem(ToolStripItem item) {
ToolStripSplitButton button = Page.UndoSplitButton;
button.DefaultItem = item;
button.Text = item.Text;
// ToDo: handle item images if decide to use them.
}
private void UndoAllHandler(object sender, EventArgs e) {
ToolStripItem item = (ToolStripItem)sender;
SetUndoButtonDefaultItem(item);
PerformUndo(item.Text, GetAllChangedEmployees());
}
VB.NET:
Private Sub ConfigureUndoSplitButton()
Dim dropDownItems As ToolStripItemCollection = _
Page.UndoSplitButton.DropDownItems
Dim item As ToolStripItem
item = dropDownItems.Add("Undo All")
AddHandler item.Click, AddressOf UndoAllHandler
SetUndoButtonDefaultItem(item) ' initial default.
item = dropDownItems.Add("Undo Current")
AddHandler item.Click, AddressOf UndoCurrentHandler
End Sub
Private Sub SetUndoButtonDefaultItem(ByVal item As ToolStripItem)
Dim button As ToolStripSplitButton = Page.UndoSplitButton
button.DefaultItem = item
button.Text = item.Text
' ToDo: handle item images if decide to use them.
End Sub
Private Sub UndoAllHandler( _
ByVal sender As Object, ByVal e As EventArgs)
Dim item As ToolStripItem = CType(sender, ToolStripItem)
SetUndoButtonDefaultItem(item)
PerformUndo(item.Text, GetAllChangedEmployees())
End Sub
Remember: Pressing a ToolStrip button does not complete a currently open data binding action. The user’s last data entry could be lost.
This does not matter for undo – we are going to discard pending changes anyway. But if this were a different button – a Save button, for example – we would have to close the uncompleted binding explicitly by calling Page.Validate() in each of the handlers.
Check out our other Tech Tips for the full story on this issue and for other nifty clues to programming .NET applications.