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

时间:2021-07-21 01:10:19

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

我的问题是,是否可以将上下文菜单链接到这些复选框?甚至可以在上下文菜单中添加复选框?甚至是组合?!

4 个解决方案

#1


The menu items have a Checked property (MenuItem.Checked, ToolStripMenuItem.Checked) that you can use for this purpose.

菜单项有一个Checked属性(MenuItem.Checked,ToolStripMenuItem.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.

关于将上下文菜单项链接到复选框的可能性,如果使用ContextMenuStrip并将CheckOnClick属性设置为true,则可以将CheckedChanged事件连接到应该“链接”的ToolStripMenuItem和CheckBox控件的相同事件处理程序。 ,并在该事件处理程序内部确保同步控件的Checked属性并执行任何其他所需的操作。

#2


Well, a menu item has the "Checked" property, which can make it behave like a checkbox. When you click a menu item, you can programmatically toggle the state of the corresponding checkbox on your form.

好吧,一个菜单项有“Checked”属性,这可以使它像一个复选框。单击菜单项时,可以以编程方式切换窗体上相应复选框的状态。

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.

您还可以使用上下文菜单的“打开”事件,根据复选框的选中状态设置菜单项的“已检查”状态。

#3


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

#4


  //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);

#1


The menu items have a Checked property (MenuItem.Checked, ToolStripMenuItem.Checked) that you can use for this purpose.

菜单项有一个Checked属性(MenuItem.Checked,ToolStripMenuItem.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.

关于将上下文菜单项链接到复选框的可能性,如果使用ContextMenuStrip并将CheckOnClick属性设置为true,则可以将CheckedChanged事件连接到应该“链接”的ToolStripMenuItem和CheckBox控件的相同事件处理程序。 ,并在该事件处理程序内部确保同步控件的Checked属性并执行任何其他所需的操作。

#2


Well, a menu item has the "Checked" property, which can make it behave like a checkbox. When you click a menu item, you can programmatically toggle the state of the corresponding checkbox on your form.

好吧,一个菜单项有“Checked”属性,这可以使它像一个复选框。单击菜单项时,可以以编程方式切换窗体上相应复选框的状态。

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.

您还可以使用上下文菜单的“打开”事件,根据复选框的选中状态设置菜单项的“已检查”状态。

#3


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

#4


  //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);