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 one 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;
}