问题描述
我在表单上有一系列复选框.我希望能够从上下文菜单以及表单本身中选择这些.上下文菜单链接到应用程序的系统托盘图标.
I have a series of checkboxes on a form.I want to be able to select these from a context menu as well as the form itself. The context menu is linked to the system tray icon of the application.
我的问题是,是否可以将上下文菜单链接到这些复选框?或者甚至可以在上下文菜单中添加复选框?甚至是组合?!
My question is, is it possible to link the context menu to these checkboxes?Or even possible to add checkboxes to the context menu?Or even a combination?!
推荐答案
菜单项有一个 Checked
属性 (MenuItem.Checked
, ToolStripMenuItem.Checked
) 可用于此目的.
The menu items have a Checked
property (MenuItem.Checked
, ToolStripMenuItem.Checked
) that you can use for this purpose.
关于将上下文菜单项链接到复选框的可能性,如果您使用 ContextMenuStrip
并将 CheckOnClick
属性设置为 true
,您可以将 CheckedChanged
事件连接到应该链接"的 ToolStripMenuItem
和 CheckBox
控件的相同事件处理程序,并在该事件内处理程序确保同步控件的 Checked
属性并执行任何其他需要的操作.
Regarding the possibility to link the context menu items to the check boxes, if you use a ContextMenuStrip
and set CheckOnClick
property to true
, you can hook up the CheckedChanged
events to the same event handler for the ToolStripMenuItem
and CheckBox
controls that should be "linked", and inside that event handler make sure to synchronize the Checked
property of the controls and perform any other needed actions.
这篇关于C# 将复选框添加到 WinForms 上下文菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!