简体   繁体   中英

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.

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.

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.

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

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

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