简体   繁体   English

C#将复选框添加到WinForms上下文菜单

[英]C# Add Checkbox To WinForms Context Menu

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?! 甚至是组合?!

The menu items have a Checked property ( MenuItem.Checked , ToolStripMenuItem.Checked ) that you can use for this purpose. 菜单项有一个Checked属性( MenuItem.CheckedToolStripMenuItem.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. 关于将上下文菜单CheckOnClick复选框的可能性,如果使用ContextMenuStrip并将CheckOnClick属性设置为true ,则可以将CheckedChanged事件连接到应该“链接”的ToolStripMenuItemCheckBox控件的相同事件处理程序。 ,并在该事件处理程序内部确保同步控件的Checked属性并执行任何其他所需的操作。

Well, a menu item has the "Checked" property, which can make it behave like a checkbox. 好吧,一个菜单项有“Checked”属性,这可以使它像一个复选框。 When you click a menu item, you can programmatically toggle the state of the corresponding checkbox on your form. 单击菜单项时,可以以编程方式切换窗体上相应复选框的状态。

You can also use the Opening event of the context menu to set the Checked state of the menu items based on the checked state of the checkboxes. 您还可以使用上下文菜单的“打开”事件,根据复选框的选中状态设置菜单项的“已检查”状态。

You can host standard as well as custom controls by wrapping them in a ToolStripControlHost 您可以通过将它们包装在ToolStripControlHost中来托管标准和自定义控件

http://msdn.microsoft.com/en-us/library/system.windows.forms.toolstripcontrolhost.aspx http://msdn.microsoft.com/en-us/library/system.windows.forms.toolstripcontrolhost.aspx

  //Create the combo box object and set its properties
  cmbFunctionArea               = new ComboBox();
  cmbFunctionArea.Cursor        = System.Windows.Forms.Cursors.Arrow;
  cmbFunctionArea.DropDownStyle=System.Windows.Forms.ComboBoxStyle.DropDownList;
  cmbFunctionArea.Dock          = DockStyle.Fill;
  //Event that will be fired when selected index in the combo box is changed
  cmbFunctionArea.SelectionChangeCommitted += new   EventHandlercmbFunctionArea_SelectedIndexChanged);

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

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