简体   繁体   English

如何在Visual Studio C#中禁用和启用按钮上的click事件

[英]How to disable and enable a click event on a button in Visual Studio C#

I have a form with 10 buttons. 我有一个带有10个按钮的表格。

When I click any of the buttons, it should be disabled so that I can't click on it again, but it should be re- enabled again when I click on any other button. 当我单击任何一个按钮时,应将其禁用,以使我无法再次单击它,但是当我单击任何其他按钮时,应再次将其重新启用

I know there is a way to do it, disabling and enabling the control (button) but I don't want to use this method because I want to change the colour of the button when it is in use (when its clicked), and if the button gets disable it will get its original colour. 我知道有一种方法可以禁用和启用控件(按钮),但是我不想使用此方法,因为我想在使用按钮时(单击时)更改按钮的颜色,并且如果该按钮被禁用,它将恢复其原始颜色。

So I want to know if there is a way to do it. 所以我想知道是否有办法做到这一点。 Thanks 谢谢

Just use a variable 只需使用一个变量

private bool isClicking = false;

public void SomeEvent()
{
   if (isClicking) return;
   try
   {
      isClicking = true;
      // do some codes
   }
   finally
   {
      isClicking = false;
   }
}

Though in truth, the usual course of events for this situation is you disable your button not color it 尽管实际上,这种情况下的通常事件是您禁用按钮而不是为其着色

@JohnB @DaniDev @JohnB @DaniDev

This is the code I'm using 这是我正在使用的代码

bool btn1,btn2,btn3;

private void button1_Click(object sender, EventArgs e)
{
     AvtivateButtons(1,btn1);
}

private void ActivateButtons(int btn, bool btnState)
{

            switch (btn)
            {
                case 1:
                    button1.Click -= button1_Click;
                    btnEstate = false;
                    break;
                case 2:
                    button2.Click -= button2_Click;
                    btnEstate = false;
                    break;
                case 3:
                    button3.Click -= button3_Click;
                    btnEstate = false;
                    break;
            }

            if (!btn1)
            {
                button1.Click += button1_Click;
            }
            if (!btn2)
            {
                button2.Click += button2_Click;
            }
            if (!btn3)
            {
                button3.Click += button3_Click;
            }


            btnState = true;

}

You should put a javascript method to return true or false. 您应该放置一个javascript方法以返回true或false。 When a javascript method returns false, the post back will be skipped. 当javascript方法返回false时,回发将被跳过。

<asp:Button ID="mybutton" OnClientClick="return JS_Method();" OnClick="PostBackMethod_Click" />

Finally I found a way to do it. 最终,我找到了一种方法。

bool[] buttons = new bool[10];

private void button1_Click(object sender, EventArgs e)
{
     if (buttons[0])
     {
         // Code.....

         buttonToggle(0);

         // Here I change the color based on the estate of the correspondent bool variable
     }
}

private void buttonToggle(int btn)
{
    buttons[btn] = false;

    for(int i = 0; i<10; i++)
    {
        if (i != btn)
        {
            buttons[i] = true;
        }
    }
}

Thanks @TheGeneral for the idea 感谢@TheGeneral的想法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM