【发布时间】: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