Question

I am designing a logging feature in which User can select which event he wants to log. On clicking button, I am showing such type of menu: Context Menu on Button click

User can select multiple Events so I need to show "Check Mark" infront of the selected option when user clicks on it.

I am unable to find any options like "Checked" or "CheckOnClick" as mentioned in this question.

I tried with ContextMenu and ContextMenuStrips but couldn't achieve Checkboxes. Any Suggestions??

Was it helpful?

Solution

Don't see any of your code so I don't know how you create this menu. But in the most general terms, here is how you access the Checked property.

((ToolStripMenuItem)contextMenuStrip.Items[0]).Checked = true; //false;
((ToolStripMenuItem)contextMenuStrip.Items[1]).Checked = true; //false;
((ToolStripMenuItem)contextMenuStrip.Items[2]).Checked = true; //false;

You can assign them as either true or false. If you have named your ToolStripItems, then you can access them directly rather than going to the Items array.

contextMenuStrip.event1.Checked = true; //false;

As you can see, I am using a ContextMenuStrip.

OTHER TIPS

Change the property CheckOnClick to True

enter image description here

In order for this to work visually, you need to ensure the "ShowCheckMargin" property is ON. Otherwise the ".Checked" property will silently do nothing.

While you could use:

((ToolStripMenuItem)contextMenuStrip.Items[0]).Checked = true; //false;

this can be dangerous because at a later date you might reorder your menu items and then the code won't match.

Instead, in the designer click on the menu then the item (like Event1) to see the properties and set the item Modifiers to Public or Internal. Then in your code you can type the name of the item and set the check:

event1_ToolStripMenuItem.Checked = true; //false;

Note: you don't need to type name of the contet menu strip. Just the item name.

If you don't see a checkmark but a focus rectangle partially overlapping the ToolStripMenuItem Text, then setting ImageScaling to None might help (in my case what seemed to be the focus rectangle was actually a very wide checkmark overlapping the text).

you might need to set the Checked property in Opening event.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top