在C#中临时更改按钮点击按钮颜色 [英] Change a button color on button click temporarily in C#

查看:249
本文介绍了在C#中临时更改按钮点击按钮颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是在赢形式

在按钮点击我要改变按钮的颜色只能暂时说1秒,然后按钮的颜色应该回到previous颜色。我用的lambda前pression和定时器这一点。

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

后面的code我从串口读取数据,显示它等问题是按钮的颜色不列入变化我按一下按钮。这需要一些时间,不会得到期望的感觉我想要的。有些时候,甚至不改变颜色。可能是什么原因呢?

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?

推荐答案

一个简单的解决办法是使用的鼠标悬停事件和​​鼠标离开事件

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

使用这种方式:

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

这一点儿也不需要你的code的任何变化,一定会让你的功能。看它是否有助于!

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

这篇关于在C#中临时更改按钮点击按钮颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆