How to change the checked state of a ToolStripItem in Winforms?
-
10-02-2021 - |
Domanda
When I look it up, they list it as having a .Checked
property. But both in Visual Studio and on msdn, it doesn't list any kid of Checked
property.
ContextMenuStrip menu = new ContextMenuStrip ( );
var menuItem = menu.Items.Add ( "CheckedItem" );
//menuItem.Checked?
Is there a way to do this?
Soluzione
You need to cast to ToolStripMenuItem
:
((ToolStripMenuItem)menuItem).Checked = true;
Altri suggerimenti
I had 2 checked items on or off, so I used this format:
private void onToolStripMenuItem_Click(object sender, EventArgs e)
{
offToolStripMenuItem.Checked = false;
}
private void offToolStripMenuItem_Click(object sender, EventArgs e)
{
onToolStripMenuItem.Checked = false;
}
This code will change StripMenuItem checked state after every mouse click.
Note: Tool Strip menu item name is: uruchomZSystememToolStripMenuItem
private void uruchomZSystememToolStripMenuItem_Click(object sender, EventArgs e)
{
uruchomZSystememToolStripMenuItem.Checked = !uruchomZSystememToolStripMenuItem.Checked;
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow