New Posts New Posts RSS Feed: Tech Tip: Split Button Configuration
  FAQ FAQ  Forum Search   Calendar   Register Register  Login Login

Tech Tip: Split Button Configuration

 Post Reply Post Reply
Author
IdeaBlade View Drop Down
Moderator Group
Moderator Group
Avatar

Joined: 30-May-2007
Location: United States
Posts: 353
Post Options Post Options   Quote IdeaBlade Quote  Post ReplyReply Direct Link To This Post Topic: Tech Tip: Split Button Configuration
    Posted: 06-Jun-2007 at 2:53pm

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.

Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down