C# 按钮单击事件在禁用时发生

C# 按钮单击事件在禁用时发生,c#,events,button,click,C#,Events,Button,Click,我是C#的新手。但是有C、Python等方面的经验。 这是我的问题 我有一个GUI,它有两种形式。 第一种形式要求提供装置的序列号,第二种形式是进行主要测试的地方 在第二种形式中,有一个阶段是等待控制器(DUT)启动,以便获得启动信息。 我让大部分代码正常工作,但问题出在这里 我禁用“下一步”按钮,直到用户重启设备。但是,即使在禁用按钮的情况下,也会发生单击事件(当用户单击禁用的按钮时),软件会根据用户的单击次数验证单击并跳过下一阶段 我怎样才能解决这个问题?或者,有没有其他更好的方法来编写代码

我是C#的新手。但是有C、Python等方面的经验。 这是我的问题

我有一个GUI,它有两种形式。 第一种形式要求提供装置的序列号,第二种形式是进行主要测试的地方

在第二种形式中,有一个阶段是等待控制器(DUT)启动,以便获得启动信息。 我让大部分代码正常工作,但问题出在这里

我禁用“下一步”按钮,直到用户重启设备。但是,即使在禁用按钮的情况下,也会发生单击事件(当用户单击禁用的按钮时),软件会根据用户的单击次数验证单击并跳过下一阶段

我怎样才能解决这个问题?或者,有没有其他更好的方法来编写代码

这是我的密码:

    using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WIFI_DMX_TEST
{
    public partial class form_test : Form
    {
        int x = 0;
        int MAX_TEST_STATES = 15;                                                //Set the number of test states.
        string Rx_String;


        test_instructions test_intsructions = new test_instructions();          //Create a new instance of the class test_instructions

        public form_test()
        {
            InitializeComponent();
            but_test_next_Click(null, null);                                    //Call the but_test_next_Click event
            Serial_INIT();
        }

        public void but_test_next_Click(object sender, EventArgs e)
        {

            if ((x <= MAX_TEST_STATES) && (but_test_next.Enabled == true))
            {
                if (x == 0)
                {
                    textBox1.Text = test_intsructions.check_unit;
                    //load picture here showing unit
                }
                else if (x == 1)
                {
                    textBox1.Text = test_intsructions.connect_cables;
                    //load picture here showing cables to connect
                }
                else if (x == 2)
                {
                    textBox1.Text = test_intsructions.powerup_unit;
                    //load picture here showing unit powerup
                }
                else if (x == 3)
                {
                    but_test_next.Enabled = false;                              //Disable the Next button

                    if (Rx_String == null)                                      //check if the Rs232 info is empty
                    {
                        MessageBox.Show("No DUT info available. Please powercycle the unit", "Error: No data", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        while (Rx_String == null) ;                             //Sit here doing nothing and wait till info is available
                        but_test_next.Enabled = true;
                    }

                    textBox1.Text = test_intsructions.program_unit;           //Show next instruction.
                    //put programming function here.
                    //Put info here showing the unit has been programmed successfully or not.
                    //if failed, then log this error and WIFI controller info in the Log file.
                }
                else if (x == 4)
                {
                    textBox1.Text = test_intsructions.reset_unit;
                    //if failed, then log this error and WIFI controller info in the Log file.
                }
                else if (x == 5)
                {
                    textBox1.Text = test_intsructions.query_colour_R;
                    //if failed, then log this error and WIFI controller info in the Log file.
                }
                else if (x == 6)
                {
                    textBox1.Text = test_intsructions.query_colour_G;
                    //if failed, then log this error and WIFI controller info in the Log file.
                }
                else if (x == 7)
                {
                    textBox1.Text = test_intsructions.query_colour_B;
                    //if failed, then log this error and WIFI controller info in the Log file.
                }
                else if (x == 8)
                {
                    textBox1.Text = test_intsructions.query_colour_W;
                    //if failed, then log this error and WIFI controller info in the Log file.
                }
                else if (x == 9)
                {
                    textBox1.Text = test_intsructions.acclerometer_mode;
                    //if failed, then log this error and WIFI controller info in the Log file.
                }
                else if (x == 10)
                {
                    textBox1.Text = test_intsructions.STL_mode_Sens_Low;
                    //if failed, then log this error and WIFI controller info in the Log file.
                }
                else if (x == 11)
                {
                    textBox1.Text = test_intsructions.STL_mode_Sens_High;
                    //if failed, then log this error and WIFI controller info in the Log file.
                }
                else if (x == 12)
                {
                    textBox1.Text = test_intsructions.test_mode;
                    //if failed, then log this error and WIFI controller info in the Log file.
                }
                else if (x == 13)
                {
                    textBox1.Text = test_intsructions.control_output;
                    //if failed, then log this error and WIFI controller info in the Log file.
                }
                else if (x == 14)
                {
                    textBox1.Text = test_intsructions.powerdown_unit;

                }
                else if (x == 15)
                {
                    textBox1.Text = test_intsructions.disconnect_cables;

                }
                //x++;
                if (!((x == 3) && (but_test_next.Enabled == false)))
                {
                    x++;
                    //but_test_next.Enabled = true;
                }

                //but_test_next.Enabled = true;

            }
        }

        private void but_test_exit_Click_1(object sender, EventArgs e)
        {
            Serial_Close();
            this.Close();
            this.Hide();
            form_startup f1 = new form_startup();
            f1.ShowDialog();

        }

        private void program_unit()
        {

        }

        class test_instructions
        {
            public string check_unit
            {
                get { return "Check DUT for any obvious faults"; }
                set { }
            }
            public string connect_cables 
            { 
                get {return "Connect cables to the DUT";}
                set {}
            }
            public string powerup_unit
            {
                get { return "Powerup the DUT"; }
                set {}
            }
            public string program_unit
            {
                get { return "Programming the DUT"; }
                set { }
            }
            public string reset_unit
            {
                get {return "Reset the unit";}
                set {}
            }
            public string query_colour_R
            {
                get { return "Is the LED RED ON?"; }
                set {}
            }
            public string query_colour_G
            {
                get {return "Is the LED GREEN ON?";}
                set {}
            }
            public string query_colour_B
            {
                get {return "Is the LED BLUE ON?";}
                set {}
            }
            public string query_colour_W
            {
                get {return "Is the LED WHITE ON?";}
                set {}
            }
            public string acclerometer_mode
            {
                get { return "Accelerometer mode: Move the unit and check if the Lights change colour" ;}
                set { }
            }
            public string STL_mode_Sens_Low
            {
                get { return "Set sensitivity to Low"; }
                set { }
            }
            public string STL_mode_Sens_High
            {
                get { return "Set sensitivity to High"; }
                set { }
            }
            public string test_mode
            {
                get { return "Press the test mode and check if lights are moving between R,G,B and W"; }
                set { }
            }
            public string control_output
            {
                get { return "Check if Control output is working as expected."; }
                set { }
            }
            public string powerdown_unit
            {
                get { return "Switch OFF the jig"; }
                set { }
            }
            public string disconnect_cables
            {
                get { return "Disconnect cables and remove DUT"; }
                set { }
            }

        }

        private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
        {
            Rx_String = serialPort1.ReadExisting();
            this.Invoke(new EventHandler(DisplayText));
        }

        private void DisplayText(object sender, EventArgs e)
        {
            textBox2.AppendText(Rx_String);
        }

        private void Serial_INIT ()
        {
            serialPort1.PortName = "COM3";
            serialPort1.BaudRate = 9600;
            serialPort1.Open();
        }

        private void Serial_Close()
        {
            serialPort1.Close();
        }

        private void form_test_FormClosing(object sender, FormClosingEventArgs e)
        {
            Serial_Close();
        }

    }
}
使用系统;
使用System.Collections.Generic;
使用系统组件模型;
使用系统数据;
使用系统图;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用System.Windows.Forms;
命名空间WIFI\u DMX\u测试
{
公共部分类form_测试:form
{
int x=0;
int MAX_TEST_STATES=15;//设置测试状态的数量。
字符串Rx_字符串;
test_instructions test_intsructions=new test_instructions();//创建类test_指令的新实例
公共表格测试()
{
初始化组件();
but_test_next_Click(null,null);//调用but_test_next_Click事件
串行_INIT();
}
公共无效但\u测试\u下一步\u单击(对象发送方,事件参数e)
{

如果((x我不完全理解你禁用按钮的意思。 如果您执行了以下操作,它应该可以工作:

 NextBtn.IsEnabled = false;
 NextBtn.Visibility = Visibility.Collapsed;
否则,您只需通过创建bool来禁用该方法:

bool hasOccured;
Private void Reason_Occured(EventArgs)
{

     hasOccured = true;

}

private void NextBtn_Click(Args)

{

     if(hasOccured) return;
     //code chunk

}

很抱歉迟了答复

我设法解决了这个问题。似乎我必须使用Switch、Case语句而不是If语句

为你的帮助干杯

垫子
:D

我禁用按钮的意思是向用户显示按钮在那里,但在他们关闭设备电源之前按钮处于禁用状态。其次,我无法获得您描述的方法。我使用的是VS2013 Express。我仅有的可用方法是“code”但“test”\u next.enabled=false;“code”但“test”\u next.visible=false他似乎是不使用winformsWPF@LabMat.检查您的单击事件处理程序中是否启用了该按钮,如果没有退出该按钮。@Mark Hall嗨Mark,我不明白您在说什么。我正在(该按钮本身的)单击事件中启用和禁用该按钮我不太清楚你的意思。我在回答中编辑了另一个选项。请使用switch语句,而不是else if if。谢谢@PhilipStuyck。说得好。我会调查一下。Mat@PhilipStuyck,感谢您提出switch语句。它可以工作:D