禁用时会发生按钮单击事件

时间:2015-07-13 15:43:48

标签: c# events button click

我对C#很陌生。但是有C,Python等经验。 这是我的问题。

我有一个有两种形式的GUI。 第一种形式要求提供单元的序列号,第二种形式是主要测试的位置。

在第二种形式中,有一个阶段,我等待控制器(DUT)启动,以便我可以获取启动信息。 我得到了大部分代码,但问题就在这里。

我禁用“下一步”按钮,直到用户为设备供电。但是,即使禁用了按钮,也会发生点击事件(当用户点击禁用按钮时),并根据他点击的次数,SW会验证点击次数并跳过下一个阶段。

我该如何解决这个问题?或者,还有另一种更好的方法来编写代码吗?

这是我的代码:

    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();
        }

    }
}

欢呼声, 垫

2 个答案:

答案 0 :(得分:0)

我没有完全明白禁用按钮的含义。 如果你做了以下它应该工作:

 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

}

答案 1 :(得分:0)

很抱歉迟到的回复。

我成功解决了这个问题。好像我必须使用Switch,Case语句而不是If语句。

为你的所有帮助干杯

垫 :d