共享多个控件的事件处理程序 [英] Share an event handler across multiple controls

查看:78
本文介绍了共享多个控件的事件处理程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Windows窗体应用程序用C#编写我有一堆按钮。当用户的鼠标悬停在一个按钮,我想按钮的边框来改变。

In my Windows forms application written in C# I have a bunch of buttons. When the user's mouse hovers over a button, I want the button's border to change.

目前我(为每个按钮复印件)以下的多个实例:

Currently I have multiple instances of the following (a copy for each button):

private void btnStopServer_MouseEnter(object sender, EventArgs e)
{
    oldColor = btnStopServer.FlatAppearance.BorderColor;
    btnStopServer.FlatAppearance.BorderColor = mouseOverColor;
}

private void btnStopServer_MouseLeave(object sender, EventArgs e)
{
    btnStopServer.FlatAppearance.BorderColor = oldColor;
}

因为我有很多的按钮时,code键更改按钮的边框的颜色占用了大量的空间。

Since I have a lot of buttons, the code to change the color of the button's border takes up a lot of space.

有什么我可以做简单的方式?

推荐答案

您应该电线组成一个的MouseEnter 鼠标离开来,需要此功能(而不是写每个方法的一个新的版本为每个控制),每个控制。您正在使用Visual Studio的假设,这可以通过事件改变目标方法名,在每个按钮的属性面板来完成。如果你先写下面的code,则此方法将出现在酒店的的MouseEnter 鼠标离开事件下拉列表中。

You should wire-up a single MouseEnter and MouseLeave to each control that needs this functionality (rather than writing a new version of each method for each control). Assuming you're using Visual Studio, this can be done by changing the target method name for the event, in each Button's property pane. If you write the following code first, then this method will appear in the property's MouseEnter and MouseLeave events' drop-down lists.

然后code需要检查的按钮从该事件被触发,如下所示:

The code would then need to check which button from which the event was fired, as follows:

private void btnWithHoverBorder_MouseEnter(object sender, EventArgs e)
{
    Button eventButton = (Button) sender;
    oldColor = eventButton.FlatAppearance.BorderColor;
    eventButton.FlatAppearance.BorderColor = mouseOverColor;
}

private void btnWithHoverBorder_MouseLeave(object sender, EventArgs e)
{
    Button eventButton = (Button) sender;
    eventButton.FlatAppearance.BorderColor = oldColor;
}

我presume oldColor 是一个全球性的?这可能不同步,如果有什么奇发生之前相应的鼠标离开的MouseEnter 事件是另一个按钮开火, C>被捕获。为了使这更健壮,我会考虑在存储按钮的 .TAG 属性旧的颜色,以便它自成体系。

I presume oldColor is a global? This might get out of sync if something "odd" happens where your MouseEnter event is fired for another button, before the corresponding MouseLeave is caught. To make this more robust, I'd consider storing the old color on the Button's .tag property, so that it's self-contained.

例如:

private void btnWithHoverBorder_MouseEnter(object sender, EventArgs e)
{
    Button eventButton = (Button) sender;
    eventButton.tag = eventButton.FlatAppearance.BorderColor;
    eventButton.FlatAppearance.BorderColor = mouseOverColor;
}

private void btnWithHoverBorder_MouseLeave(object sender, EventArgs e)
{
    Button eventButton = (Button) sender;
    eventButton.FlatAppearance.BorderColor = (Color)eventButton.tag;
}

(标签基本上是在其上标记什么相关控制的特定实例钩,有没有一个属性,它是类型对象这意味着你可以在那里标记任何东西,但是当你从它读,你需要将它转换回任何类型您在第一时间放在那里,但因为它是一个对象你可以把任何东西存在,包括例如包含多个属性的自定义类,或数组等等,如果你需要标记有超过一件事控制)。

(The tag is basically a hook on which to tag "anything" relevant to a specific instance of a control, that there is not already a property for. It's of type Object which means you can tag anything there, but when you read from it, you need to cast it back to whatever type you put there in the first place. But because it's an Object you can put anything there, including eg a custom class that contains multiple properties, or an array, etc if you need to tag a control with more than one thing).

这篇关于共享多个控件的事件处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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