当某个值来自串口时,我想在固定时间内更改Button
的BackColor
。我在这里设置了Timer
对象:
public formLabel()
{
InitializeComponent();
// ...
timerColor.Tick += timerColor_Tick;
timerColor.Interval = 3000;
}
然后,当我从串行端口接收信号时:
private void DataReceivedHandler(object sender, SerialDataReceivedEventArgs e)
{
SerialPort sp = (SerialPort)sender;
string indata = sp.ReadExisting();
MessageBox.Show("Dati Ricevuti: " + indata);
if (indata.CompareTo("K") == 0)
{
timerColor.Enabled = true;
btnEsito.BackColor = Color.Green;
btnEsito.Text = "GOOD";
// Do something
}
if (indata.CompareTo("O") == 0)
{
timerColor.Enabled = true;
btnEsito.BackColor = Color.Red;
btnEsito.Text = "NO GOOD";
}
}
下面是停止计时器的方法:
private void timerColor_Tick(object sender, EventArgs e)
{
MessageBox.Show("HERE!");
timerColor.Enabled = false;
btnEsito.BackColor = Color.White;
}
根据我收到的信号类型,btnEsito
的BackColor
会变成绿色或红色,但程序不会显示消息"HERE!"
,按钮也不会返回白色。
有人能帮帮我吗?
https://stackoverflow.com/questions/52079525
复制相似问题