简体   繁体   English

显示按钮的上下文菜单条或上下文菜单的复选框

[英]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.我尝试使用 ContextMenu 和 ContextMenuStrips 但无法实现复选框。 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.但在最一般的情况下,以下是您如何访问Checked属性。

((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.您可以将它们指定为 true 或 false。 If you have named your ToolStripItems , then you can access them directly rather than going to the Items array.如果您已命名ToolStripItems ,那么您可以直接访问它们,而不是转到Items数组。

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

As you can see, I am using a ContextMenuStrip .如您所见,我使用的是ContextMenuStrip

Change the property CheckOnClick to True将属性CheckOnClick更改为True

在此处输入图片说明

In order for this to work visually, you need to ensure the "ShowCheckMargin" property is ON.为了使其直观地工作,您需要确保“ShowCheckMargin”属性为ON。 Otherwise the ".Checked" property will silently do nothing.否则“.Checked”属性将不做任何事情。

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 .相反,在设计器中单击菜单然后单击项目(如 Event1)以查看属性并将项目Modifiers设置为PublicInternal 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 属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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