If you have ever used an image editing application, you will know there is a group of buttons for selecting the current tool. One tool must be selected at all times, and when开发者_开发问答 a tool is selected, the button stays pressed until a different tool is selected.
I have created a ToolStrip control and added my ToolStripButton items to it, and set the images. For each button I have set the CheckOnClick property to true.
What I was going to do next is subscribe to the Checked event of each button and set the other buttons CheckState to unchecked. It seems like a kind of hack, though.
Does .NET have a way to accomplish this with the ToolStrip control, or is there a better control to use?
I am using C# and VS 2010, targeting .NET framework 4
I thinks there is no way to have tfully automated radio buttons on a ToolStrip.
However, it could be made semi-automated:
In each of your click events call SetToolButtonsChecked(sender);
private void toolStripButton1_Click(object sender, EventArgs e)
{
SetToolButtonsChecked(sender);
}
which is implemented as:
private static void SetToolButtonsChecked(object sender)
{
ToolStripButton btn = sender as ToolStripButton;
ToolStrip strip = btn.GetCurrentParent();
foreach (ToolStripItem item in strip.Items)
{
if (!(item is ToolStripButton)) continue;
ToolStripButton btnTemp = item as ToolStripButton;
if (!btnTemp.CheckOnClick) continue;
btnTemp.Checked = btnTemp.Equals(btn);
}
}
This method iterates all buttons on the same toolstrip and sets the Checked property accordingly, if the button has property CheckOnClick = true
.
精彩评论