简体   繁体   中英

ToolStripMenuItem - Restricted

I have a ToolStripMenuItem and what i want to do is that if a flag is false not to allow the user to click it.

Ho can i do that? When i add a click event it still opens the menu.

this.logsToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.ToolStripMenuItem1,
this.ToolStripMenuItem2,
this.toolStripSeparator1,
this.getReportToolStripMenuItem});
this.logsToolStripMenuItem.Name = "logsToolStripMenuItem";
this.logsToolStripMenuItem.Size = new System.Drawing.Size(44, 20);
this.logsToolStripMenuItem.Text = "Logs";
this.logsToolStripMenuItem.Click += new System.EventHandler(this.onlog_click);

Use the "Enabled" property of your item and set it to false to deactivate any behaviour... Alternativley remove your click event handler if flag is false (.click -= myClickHandler;)... Or use the DropDownOpening event and cancel it. This is fired when about to open but still has not opened yet...

You can use the Enabled property of the ToolStrip to enable/disable menu.

to enable it (allow user to click)

 toolStrip1.Enabled = true;

and to disable it (not to allow the user to click)

 toolStrip1.Enabled = false;

ToolstripMenuItem is the abstract base class on which the ToolStrip or ToolStripDropDown classes are implemented. These classes offer the functionality that you require.

For example, the ToolStripDropDownItem has an event called Opening receiving a CancelEventHandlerArguments that can be used to cancel the Opening of the Menu

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