I don't think that there is any built in support for that (like the UseMnemonic
property of the Button
control), unfortunately. One way to do it is to make a brute-force method that will walk the control tree on the form and perform a replace on all ToolStripMenuItems found:
public TheForm()
{
InitializeComponent();
FixAmpersands(this.Controls);
}
private static void FixAmpersands(Control.ControlCollection controls)
{
foreach (Control control in controls)
{
if (control is ToolStrip)
{
FixAmpersands((control as ToolStrip).Items);
}
if (control.Controls.Count > 0)
{
FixAmpersands(control.Controls);
}
}
}
private static void FixAmpersands(ToolStripItemCollection toolStripItems)
{
foreach (ToolStripItem item in toolStripItems)
{
if (item is ToolStripMenuItem)
{
ToolStripMenuItem tsmi = (ToolStripMenuItem)item;
tsmi.Text = tsmi.Text.Replace("&", "&&");
if (tsmi.DropDownItems.Count > 0)
{
FixAmpersands(tsmi.DropDownItems);
}
}
}
}
This is, of course, useful primarily if the menu structure is not build up dynamically and occasionaly during the lifetime of the form. If new items are added every now and then you will probably need to run them through some method that will perform the ampersand-doubling at on a single item.