在鼠标离开时更改按钮颜色并输入 [英] Change buttons colors on mouse-leave and enter

查看:60
本文介绍了在鼠标离开时更改按钮颜色并输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正试图在鼠标离开时更改按钮颜色。我为每个按钮单独做了,但我试图改变鼠标离开时的颜色,只使用两个事件处理程序进入。



我尝试过:



我的意思是:



Hi, I am trying to change the buttons colors on mouse leave. I did it separately for each button but I was trying to change the colors on mouse leave and enter using only two event handlers.

What I have tried:

What I mean by that is this:

private void ChangeColourOnMouseEnter(object sender, EventArgs e)
{
    this.btnNewGame.BackColor = Color.blue;
    this. btnHelp.BackColor   = Color.blue;
    this.btnExit.BackColor   = Color.blue;
}

private void ChangeColourOnMouseLeave(object sender, EventArgs e)
{
    btnNewGame.BackColor = Color.Yellow;
    btnHelp.BackColor   = Color.Yellow;
    btnExit.BackColor   = Color.Yellow;
}





看起来,只要鼠标进入它就会改变每个按钮的颜色。我在想如何做到这一点,只有一个按钮只用一种方法突出显示不同的颜色。 (如果你知道我的意思)



任何帮助都将不胜感激,谢谢!。



As it look like, it whenever the mouse enters it will change the colour of every button. I was thinking how could it be done so that only one button will be highlighted in different color using only one method for it. (if you know what I mean)

Any help would be appreciated, thanks!.

推荐答案

如果我理解正确,你只想更改悬停按钮的颜色吗?

你可以这样做:

If I understand correctly, you want to change the color only of the button which is hovered?
You can do it like this:
private void ChangeColourOnMouseEnter(object sender, EventArgs e)
{
   ((Button)sender).BackColor = Color.blue;
}
 
private void ChangeColourOnMouseLeave(object sender, EventArgs e)
{
   ((Button)sender).BackColor = Color.Yellow;
}



sender 参数保存对引发事件的控件的引用。 />
此代码首先将 sender 变量(声明为 object )转换回其实际类型( 按钮),然后调用其 BackColor 属性为其指定不同的颜色。

最后,选择要具有该行为的所有按钮,然后在属性面板中,单击事件按钮(位于顶部,位于控件的名称和类型下);将事件处理程序分配给相应的事件。然后,相同的事件处理程序将能够处理每个控件的事件。



希望这会有所帮助。


The sender parameter holds a reference to the control which has raised the event.
This code first casts the sender variable (declared as object) back to its real type (Button), then call its BackColor property to assign it a different color.
Finally, select all the buttons you want to have that behaviour, and in the Properties panel, click on the Events button (at the top, just under the name and type of the control); assign the event handlers to corresponding events. The same event handler will then be able to handle the event for each of these controls.

Hope this helps.


Make你自己的Button控件。启动一个新类并从Button继承。



重写OnMouseEnter和OnMouseLeave事件,并在这些事件中适当设置按钮颜色。一个简短的样本:

Make your own Button control. Start a new class and inherit from Button.

Override the OnMouseEnter and OnMouseLeave events and set your button colors appropriately in those. A short sample:
public class ButtonEx : Button
{
    protected override void OnMouseEnter(EventArgs e)
    {
        BackColor = Color.Yellow;
        base.OnMouseEnter(e);
    }

    protected override void OnMouseLeave(EventArgs e)
    {
        BackColor = Color.LightBlue;
        base.OnMouseLeave(e);
    }
}



当然,您可以添加属性来控制高亮颜色。



您可以将此代码放入其自己的Class项目中,并在现有项目中添加对它的引用,或者只是将其作为类添加到当前应用程序并编译应用程序。编译完成后,控件将显示在ToolBox中。


Of course, you can add properties to control the highlight color as appropriate.

You can either put this code in it's own Class project and add a reference to it in your existing project, or just add it as a class to your current app and compile the app. Once compiled, the control shows up in the ToolBox.


这篇关于在鼠标离开时更改按钮颜色并输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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