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

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

问题描述

在我用 C# 编写的 Windows 窗体应用程序中,我有一堆按钮.当用户的鼠标悬停在按钮上时,我希望按钮的边框发生变化.

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

由于我有很多按钮,更改按钮边框颜色的代码占用了大量空间.

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

有没有更简单的方法可以做到这一点?

推荐答案

您应该将单个 MouseEnterMouseLeave 连接到需要此功能的每个控件(而不是而不是为每个控件编写每个方法的新版本).假设您使用的是 Visual Studio,这可以通过在每个 Button 的属性窗格中更改事件的目标方法名称来完成.如果你先写下面的代码,那么这个方法就会出现在属性的MouseEnterMouseLeave事件的下拉列表中.

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.

然后代码需要检查从哪个按钮触发事件的,如下所示:

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

我认为 oldColor 是全局的?如果在捕获相应的 MouseLeave 之前为另一个按钮触发 MouseEnter 事件时发生奇怪"的事情,这可能会不同步.为了使它更健壮,我会考虑将旧颜色存储在 Button 的 .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;
}

(标签基本上是一个钩子,用于标记与控件的特定实例相关的任何东西",还没有一个属性.它的类型是 Object 这意味着你可以在那里标记任何东西,但是当您从中读取时,您首先需要将其转换回您放置在那里的任何类型.但是因为它是一个 Object,您可以在那里放置任何东西,包括例如自定义包含多个属性或数组等的类,如果您需要用不止一个东西标记控件).

(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天全站免登陆