【问题标题】:Button Click event occurs when it is disabledButton Click 事件在它被禁用时发生
【发布时间】:2015-07-13 15:43:48
【问题描述】:

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

    }
}

干杯, 垫子

【问题讨论】:

  • 请使用switch语句而不是else if else if else if。
  • 感谢@PhilipStuyck。好点子。我会调查的。垫子
  • @PhilipStuyck,感谢您建议使用 switch 语句。它有效:D

标签: c# events button click


【解决方案1】:

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

 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

}

【讨论】:

  • 我禁用按钮的意思是向用户显示按钮在那里,但在他们重新启动设备之前它被禁用。其次,我无法获得您描述的方法。我正在使用 VS2013 Express。我唯一可用的方法是 'code' but_test_next.enabled = false; '代码' but_test_next.visible = false
  • 他似乎使用的是 winforms 而不是 WPF
  • @LabMat。检查按钮是否在您的点击事件处理程序中启用,如果它没有退出它。
  • @Mark Hall 嗨,马克,我不明白你在说什么。我在(该按钮本身的)点击事件中启用和禁用按钮
  • 我不太清楚你的意思。我在答案中编辑了另一个选项。
【解决方案2】:

抱歉回复晚了。

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

为你的帮助干杯

垫子 :D

【讨论】:

    猜你喜欢
    • 2015-10-18
    • 2011-04-05
    • 1970-01-01
    • 2011-11-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多