Click here to Skip to main content
15,881,757 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,

Can anybody tell me how to make a button appear pressed. Like if i right click on the button i should get a small pop up like it appears when we right click on the desktop, asking either to enable or disable the button. If i have select disable option the button should appear pressed & if i select enable it should show button in normal condition. Also Once disabled it should continue to be in that state until i enable it by again right clicking on the button & selecting enable option.
Posted
Comments
StM0n 28-Mar-13 6:27am    
Are you looking for a toggle button? And for what platform... plain winform or something else?
Jagadisha_Ingenious 28-Mar-13 6:38am    
Similar to toggling stuff in a single button. Windows form platform is what i am working on...
Erik Rude 28-Mar-13 6:28am    
What is your environment? WinForms or WPF. Have you had a look at googling this. There are many good resources even on this site.
Jagadisha_Ingenious 28-Mar-13 6:39am    
I am working on windows form platform.
Jonathan [Darka] 28-Mar-13 6:30am    
The small pop up is called a "Context Menu" and you'll need to implement it on the right click handler for your button, you should then be able to set the state of the button based on the menu item chosen by the user, i.e. "Enable/Disable"

1 solution

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!

C#
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;

      // if the button is disabled the mouse event is sent to its container control
      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;
   }
}
 
Share this answer
 
Comments
Jagadisha_Ingenious 28-Mar-13 9:00am    
Dear Sir, I have implemented your code it works fine. Basically i have an array of buttons in a panel in which i wanted this enable/disable option to be included. As per the code u have given i am able to deactivate the button individually & enable it by clicking on any other button. The problem is if i deactivate all the buttons i am left with no choice to activate it. I cannot activate anyone. So how to achieve this..?
toATwork 28-Mar-13 9:37am    
You need to enable the "activate" ToolStripMenuItem when you disable the button
Jagadisha_Ingenious 29-Mar-13 0:49am    
Sir, I have actually hid the toolstrip menuitem so any other way to achieve this...?
toATwork 29-Mar-13 9:56am    
I think I do not understand.
If you deactivate all buttons then enable (or set visible) the activate items.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900