开发者

How do I get a Winforms ToolStripSplitButton to remember the last button clicked?

I'm trying to use a ToolStripSplitButton for the "export" part of my UI, i.e.

Export to PDF...

Export to XLS...

Export to CSV...

It should default to "Export to PDF..." at startup, and show all the possible settings when the user clicks the dropdown arrow.

If the user selects another export setting, it should "remember" that one, and show it as the default.

Edit For example, if the user selects "Export to XLS..." from the dropdown - "Export to XLS..." will replace "Export to PDF..." as the text in the main button, and they can now click on that to create XLS files instead of using the dropdown.

The Visual Studio 2008 "Standard" toolbar has 2 controls that behave the way I want. The first o开发者_如何学JAVAne shows "New Project..." (as an icon, not as text), but if you select "New Website..." from the dropdown, that will become the default. \Edit

I thought the ToolStripSplitButton would do all this automatically, but it's not happening, and the help topic is almost useless.

Can anyone provide an example of how to do this?


I think I know what you are trying to do.

I created a Settings variable called LastExportButton from the Properties window.

And here is some code I threw together that "remembers" the last button "chosen":

private void Form1_Load(object sender, EventArgs e)
{
  string lastButton = Properties.Settings.Default.LastExportButton;
  if (ExportSplitButton.DropDownItems.ContainsKey(lastButton))
  {
    if (lastButton == ExportPDFButton.Name)
      ExportSplitButton.DefaultItem = ExportPDFButton;
    else if (lastButton == ExportXLSButton.Name)
      ExportSplitButton.DefaultItem = ExportXLSButton;
    else if (lastButton == ExportCSVButton.Name)
      ExportSplitButton.DefaultItem = ExportCSVButton;

    ExportSplitButton.Text = ExportSplitButton.DefaultItem.Text;
  }
}

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
  Properties.Settings.Default.LastExportButton = ExportSplitButton.DefaultItem.Name;
  Properties.Settings.Default.Save();
}

private void ExportSplitButton_DropDownItemClicked(object sender, ToolStripItemClickedEventArgs e)
{
  ((ToolStripSplitButton)sender).DefaultItem = e.ClickedItem;
  ((ToolStripSplitButton)sender).Text = e.ClickedItem.Text;
}


It would be nice if ToolStripItems could display a radio button. They don't, they only have an option for a check mark. Which is okayish, still kinda makes it obvious to the user what was last used. You can simulate a radio button with an image if you want to. Anyhoo, code would be similar to this:

    private void oneToolStripMenuItem_Click(object sender, EventArgs e) {
        // stuff
        //..
        setCheck((ToolStripMenuItem)sender);
    }
    private void twoToolStripMenuItem_Click(object sender, EventArgs e) {
        // stuff
        //..
        setCheck((ToolStripMenuItem)sender);
    }
    // Add more as needed
    //...

    private void setCheck(ToolStripMenuItem check) {
        foreach (ToolStripMenuItem item in toolStripSplitButton1.DropDown.Items) {
            item.Checked = item == check;
        }
    }

It is likely that you don't need the separate Click event handlers in this case, a single method could do it. You use the sender argument to find out which item was clicked.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜