这在赢形成更改按钮点击按钮颜色暂时在C#
在按钮点击我要改变按钮的颜色只能暂时说1秒钟,然后按钮的颜色应该找回到以前的颜色。我为此使用了lambda表达式和计时器。
private void btn_Read_Click(object sender, EventArgs e)
{
System.Windows.Forms.Timer t1 = new System.Windows.Forms.Timer();
t1.Interval = 1000;
t1.Tick += (src, ee) =>
{
btn_Read.BackColor = Color.Transparent; t1.Stop();
};
t1.Start();
btn_Read.BackColor = Color.YellowGreen;
lvwMessages.Items.Clear();
string strcommand = "AT+CMGL=\"ALL\"";
objShortMessageCollection = ReadSMS(strcommand); // Line wher I am reading messages from the port
foreach (ShortMessage msg in objShortMessageCollection)
{
ListViewItem item = new ListViewItem(new string[] { msg.Sender, msg.Message, msg.Sent, msg.Index });
item.Tag = msg;
lvwMessages.Items.Insert(0, item);
}
if (lvwMessages.Items.Count == 0)
{
status_other.Visible = true;
status_other.Text = "No messages";
lbl_total.Text = "Total: 0";
System.Windows.Forms.Timer timer1 = new System.Windows.Forms.Timer();
timer1.Interval = 2000;
timer1.Tick += (source, ex) => { status_other.Visible = false; timer1.Stop(); };
timer1.Start();
}
else
{
status_other.Visible = false;
chk_selectmsg.Visible = true;
btn_delete.Visible = true;
lbl_total.Text = "Total: " + lvwMessages.Items.Count.ToString(); ;
}
}
在此代码后,我从读串口数据,显示它等问题是按钮的颜色不列入变化,因为我按一下按钮。这需要一些时间,并没有给我想要的想要的感觉。有时甚至不会改变颜色。可能是什么原因?
也许尝试在mouseDown事件?也许在创建计时器之前使用'btn_Read.BackColor ='语句? – 2012-08-04 09:15:31
你在做串口读取以及其他需要时间在btn_Read_Click方法中的时间吗?如果是这样,它会解释你看到的是什么 – ekholm 2012-08-04 09:17:14
@ekholm:是的,但是这并不会阻止我的用户界面,并获取数据需要大约500毫秒 – Cdeez 2012-08-04 09:20:16