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

3

14

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?

Trounce answered 21/10, 2011 at 3:39 Comment(0)
W
25

You need to cast to ToolStripMenuItem:

((ToolStripMenuItem)menuItem).Checked = true;
Wooded answered 21/10, 2011 at 3:57 Comment(0)
S
0

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;
    }
Steamship answered 17/12, 2018 at 4:18 Comment(0)
V
-2

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;
}
Vestment answered 2/12, 2020 at 20:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.