【问题标题】:How Can I Enable a a disabled button by right-click on it in C# forms?如何通过右键单击 C# 表单中的禁用按钮来启用它?
【发布时间】:2018-12-16 09:27:20
【问题描述】:

在我的 C# 迷你应用程序中,我有一个按钮,我想通过右键单击它来启用和禁用它。即当通过右键单击启用按钮时,它会变为禁用,当它被禁用时,右键单击它会将其状态更改为启用。禁用启用按钮既简单又简单,但无法通过右键单击启用它;因为按钮被禁用并且没有事件被发送到消瘦代码。 我该怎么办?

【问题讨论】:

    标签: c# winforms button isenabled


    【解决方案1】:

    禁用控件的鼠标事件被传递给它的Parent

    你可以在那里捕捉它们并测试光标是否在按钮上。

    例子:

    if (button1.Bounds.Contains(e.Location)) button1.Enabled = true;
    

    如果您有多个按钮,则需要全部测试..:

    private void panel1_MouseDown(object sender, MouseEventArgs e)
    {
        foreach (Control ctl in panel1.Controls)
        {
            if (ctl is Button && ctl.Bounds.Contains(e.Location)) 
                ctl.Enabled = true;
        } 
    }
    

    如果应该只启用鼠标右键,请为其添加测试,可能是这样:

    if (e.Button.HasFlag(MouseButtons.Right) && 
        ctl is Button && ctl.Bounds.Contains(e.Location)) 
    

    【讨论】:

    • 如果您对答案感到满意,请考虑考虑accepting it..! - 我看到你从来没有这样做过:去左上角的(不可见的)复选标记,在答案的投票下方,然后单击它!它变成绿色并为我们俩赢得了一点声誉。..
    【解决方案2】:

    右键单击以启用和禁用按钮不是标准或推荐的按钮行为,因此 API 完全忽略了它的简单实现。一旦按钮被禁用,它就不再捕获鼠标事件,但表单会。

    public partial class Form1 : Form
    {
        private Boolean _button1IsRightClicked;
        public Form1()
        {
            InitializeComponent();
            _button1IsRightClicked = false;
        }
    
        private void button1_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Hi");
        }
    
        private void Form1_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.X >= button1.Location.X && e.X <= (button1.Location.X + button1.Width) &&
                e.Y >= button1.Location.Y && e.Y <= (button1.Location.Y + button1.Height))
            {
                _button1IsRightClicked = true;
            }
            else
            {
                _button1IsRightClicked = false;
            }
        }
    
        private void Form1_MouseUp(object sender, MouseEventArgs e)
        {
            if (e.X >= button1.Location.X && e.X <= (button1.Location.X + button1.Width) &&
                e.Y >= button1.Location.Y && e.Y <= (button1.Location.Y + button1.Height))
            {
                _button1IsRightClicked = false;
                enableDisableButton1(button1.Enabled);
            }
            else
            {
                _button1IsRightClicked = false;
            }
        }
    
        private void enableDisableButton1(Boolean isEnabled)
        {
            if (isEnabled)
            {
                button1.Enabled = false;
                isEnabled = false;
            }else
            {
                button1.Enabled = true;
                isEnabled = true;
            }
        }
    
        private void button1_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                _button1IsRightClicked = true;
            }
            else
            {
                _button1IsRightClicked = false;
            }
        }
    
        private void button1_MouseUp(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right && _button1IsRightClicked == true)
            {
                _button1IsRightClicked = false;
                enableDisableButton1(button1.Enabled);
            }
            else
            {
                _button1IsRightClicked = false;
            }
        }
    }
    

    【讨论】:

    • 它是否为其他答案增加了任何价值?
    猜你喜欢
    • 2013-07-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-11
    • 2021-04-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多