右键单击如何按下按钮 [英] How to make button appear pressed with right click

查看:57
本文介绍了右键单击如何按下按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



任何人都可以告诉我如何按下按钮。就像我右键单击按钮一样,当我们右键单击桌面时,我会看到一个小弹出窗口,要么启用或禁用按钮。如果我有选择禁用选项按钮应该按下,如果我选择启用它应该在正常情况下显示按钮。此外,一旦被禁用,它应该继续处于该状态,直到我通过再次右键单击按钮并选择启用选项来启用它。

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.

推荐答案

这应该可以解决问题。



备注:如果按钮被禁用,鼠标事件由其容器控件处理,因此附加事件处理程序!



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;

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


这篇关于右键单击如何按下按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆