如何在按钮单击并将鼠标悬停在事件后更改按钮背景颜色。 [英] How to change button backcolor after button click and mouse over event.

查看:227
本文介绍了如何在按钮单击并将鼠标悬停在事件后更改按钮背景颜色。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想做的是

1)每当我将鼠标移到按钮上时,它的颜色应为蓝色

2)当我的鼠标离开按钮时,它的颜色应该变为正常

3)每当我将鼠标移到按钮上时,它的颜色应为蓝色

4)然后当我点击该按钮时,它的颜色应该改为红色

5)按钮点击后我的鼠标离开按钮的颜色应该红色本身。

6)下次当我将鼠标移到按钮上时,它的颜色应该变为蓝色

7)然后,当我的鼠标离开按钮时,它的颜色应该是红色本身



这里我得到了所有6个步骤,现在我需要迈出第7步......我该怎么做。



提前谢谢。



我尝试过的事情:


$ 1,2 $ 5 1,2,3,5步骤b
private void btnDateTime_MouseHover(object sender,EventArgs e)
{
if(btnDateTime.BackColor!= Color.Empty&& btnDateTime.BackColor!= Color.Red)
{
btnDateTime.BackColor = Color.Blue;
}

if(btnDateTime.BackColor == Color.Red)
{
btnDateTime.BackColor = Color.Blue;
}
}




第四步


 private void btnDateTime_Click(object sender,EventArgs e)
{
btnHi.Visible = true;
if(btnDateTime.BackColor == Color.Blue)
{
btnDateTime.BackColor = Color.Red;
}
else
{
btnDateTime.BackColor = default(Color);
}
}



第7步我该怎么办?

解决方案

试试



 bool isclicked = false; 
private void button1_Click(object sender,EventArgs e)
{
(发送者为Button).BackColor = System.Drawing.Color.Red;
isclicked = true;
}

private void button1_MouseHover(object sender,EventArgs e)
{
(发送者为Button).BackColor = System.Drawing.Color.Blue;
}

private void button1_MouseLeave(object sender,EventArgs e)
{
System.Drawing.Color defaultColor = System.Drawing.SystemColors.Control;
(发件人为Button).BackColor = isclicked? System.Drawing.Color.Red:defaultColor

}


what i am trying to do is
1)Whenever i move my mouse over button it's color should be Blue.
2)when my mouse leaves the button it's color should become normal.
3)Whenever i move my mouse over button it's color should be Blue
4)Then when i click that button its color should be changed to Red.
5)after button click when my mouse leaves the button its color should be red itself.
6)next time when i move my mouse over the button it's color should become blue.
7)Then when my mouse leaves the button its color should be red itself

Here i got all the 6 steps and now i need to get the 7th step.. how do i do it.

Thank you in advance.

What I have tried:

for 1,2,3,5 and 6 steps

 private void btnDateTime_MouseHover(object sender, EventArgs e)
{
if (btnDateTime.BackColor != Color.Empty && btnDateTime.BackColor != Color.Red)
            {
                btnDateTime.BackColor = Color.Blue;
            }
            
            if (btnDateTime.BackColor == Color.Red)
            {
                btnDateTime.BackColor = Color.Blue;
            }
}



for 4th step

private void btnDateTime_Click(object sender, EventArgs e)
       {
           btnHi.Visible = true;
           if(btnDateTime.BackColor == Color.Blue)
           {
               btnDateTime.BackColor = Color.Red;
           }
           else
           {
               btnDateTime.BackColor = default(Color);
           }
       }


for 7th step what should i do??

解决方案

try

bool isclicked = false;
       private void button1_Click(object sender, EventArgs e)
       {
           (sender as Button).BackColor = System.Drawing.Color.Red;
           isclicked = true;
       }

       private void button1_MouseHover(object sender, EventArgs e)
       {
           (sender as Button).BackColor = System.Drawing.Color.Blue;
       }

       private void button1_MouseLeave(object sender, EventArgs e)
       {
           System.Drawing.Color defaultColor = System.Drawing.SystemColors.Control;
           (sender as Button).BackColor = isclicked ? System.Drawing.Color.Red: defaultColor

       }


这篇关于如何在按钮单击并将鼠标悬停在事件后更改按钮背景颜色。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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