這在贏形成更改按鈕點擊按鈕顏色暫時在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