Change a button color on button click temporarily in C#

纵饮孤独 提交于 2019-12-12 00:13:06

问题


This is in Win forms

On button click I want to change the color of the button temporarily say only for 1 second and then the button color should get back to the previous color. I used lambda expression and timer for this.

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

Later in this code I am reading the data from serial port, displaying it, etc. The problem is that the button color doesnot change as I click the button. It takes some time and does not give the desired feel I want. Some times doesn't even change the color. What could be the reason?


回答1:


A simple solution would be using mouse hover event and mouse leave event

Use it this way:

    private void btn_Read_MouseHover(object sender, EventArgs e)
    {
        btn_Read.BackColor = Color.AliceBlue;
    }

    private void btn_Read_MouseLeave(object sender, EventArgs e)
    {
        btn_Read.BackColor = Color.AntiqueWhite;
    }

This does'nt require any change in your code and definitely will give you the functionality. See if it helps!




回答2:


you should avoid having work-intensive code on the UI thread

to get the desired effect, sepperate the code for the UI from the code that does the work ...

when the button is clicked, change its appearence and start some background task (threadpool,backgroundworker,etc) that does the work

be aware that you can interact with a control only from the thread it was created on, so to display your data or interact with the UI, you will have to invoke the UI thread (see Control.Invoke(...))

if you have a lot of UI reseting stuff like that, you should think about a timer on the form, to check every let's say 200ms if there is something to be reset/done

you could use a sorted list with tuples (Datetime,delegate) that are executed and removed once the time has come ...




回答3:


Write the rest of code in thread and fire that thread. this will make your UI responsive and will give you the desired output you want for button. Or use btnedit.Refresh() just after changing color to force button to redraw itself



来源:https://stackoverflow.com/questions/11807365/change-a-button-color-on-button-click-temporarily-in-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!