How to change the checked state of a ToolStripItem in Winforms?

18.5k views Asked by At

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?

3

There are 3 answers

0
Jeff Ogata On BEST ANSWER

You need to cast to ToolStripMenuItem:

((ToolStripMenuItem)menuItem).Checked = true;
0
Łukasz Włodarczyk On

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;
}
0
John B On

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