That should do the trick.
Remark: if the button is disabled the mouse events are handled by its container control, therefore the additional event handler!
public partial class Form1 : Form
{
ToolStripMenuItem activate = new ToolStripMenuItem("Activate");
ToolStripMenuItem deactivate = new ToolStripMenuItem("Deactivate");
public Form1()
{
InitializeComponent();
ContextMenuStrip cms = new ContextMenuStrip();
cms.Items.Add(activate);
activate.Click += new EventHandler(ActivateClick);
cms.Items.Add(deactivate);
deactivate.Click += new EventHandler(DectivateClick);
button1.ContextMenuStrip = cms;
activate.Enabled = false;
button1.Parent.MouseUp += new MouseEventHandler(Parent_MouseUp);
}
void Parent_MouseUp(object sender, MouseEventArgs e)
{
Control ctl = this.GetChildAtPoint(e.Location);
if (ctl != null && !ctl.Enabled && ctl.ContextMenuStrip != null)
ctl.ContextMenuStrip.Show(this, e.Location);
}
private void ActivateClick(object sender, EventArgs e)
{
button1.Enabled = true;
deactivate.Enabled = true;
activate.Enabled = false;
}
private void DectivateClick(object sender, EventArgs e)
{
button1.Enabled = false;
deactivate.Enabled = false;
activate.Enabled = true;
}
}