简体   繁体   中英

Show checkbox for Context MenuStrip or Context Menu of a button

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:单击按钮时的上下文菜单

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??

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 .

Change the property CheckOnClick to True

在此处输入图片说明

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.

如果您没有看到复选标记但焦点矩形与 ToolStripMenuItem 文本部分重叠,则将 ImageScaling 设置为 None 可能会有所帮助(在我的情况下,似乎焦点矩形实际上是一个与文本重叠的非常宽的复选标记)。

您可能需要在 Opening 事件中设置 Checked 属性。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM