提供NumericUpDown的向上和向下按钮处理程序的派生NumericUpDown






4.20/5 (4投票s)
一个派生自 NumericUpDown 的类,为 NumericUpDown 的向上和向下按钮提供处理程序。
引言
本文介绍了一个非常简单的类,它派生自 NumericUpDown
类。它重写了 NumericUpDown
的 UpButton
和 DownButton
方法,并触发相应的事件。
背景
我一直在寻找一种方法来处理 NumericUpDown
的向上和向下按钮的点击事件。基本的 NumericUpDown
类没有公开任何这样的事件,但幸运的是,它允许派生类重写 UpButton
和 DownButton
方法。所以,我做了,我从 NumericUpDown
类派生了一个类,并重写了 UpButton
和 DownButton
方法。我在这些方法中触发相应的事件,以便订阅者可以处理它们。
Using the Code
NumericUpDnWithUpDnBtnEventHandlers
是一个基于 NumericUpDown
类的非常简单的类。只需将其添加到与项目相同的命名空间中的项目中。然后,实例化一个新的 NumericUpDnWithUpDnBtnEventHandlers
对象,并根据需要设置其大小和位置。根据需要订阅基类 (NumericUpDown
) 公开的任何事件,最后,如果需要,订阅 "UpButtonClicked
" 和 "DownButtonClicked
" 事件。不要忘记为这些事件提供处理程序。现在,当用户点击控件中的 UpButton
时,将调用 "UpButtonClicked
" 处理程序,当点击 DownButton
时,将调用 "DownButtonClicked
" 处理程序。您可以在这些处理程序中执行任何您需要做的事情。
最后,这是一个您如何订阅事件的示例(其中 numericUpDownVSize
派生自 NumericUpDnWithUpDnBtnEventHandlers
)
this.numericUpDownVSize.UpButtonClicked +=
new EventHandler(numericUpDownSize_UpButtonClicked);
this.numericUpDownVSize.DownButtonClicked +=
new EventHandler(numericUpDownSize_DownButtonClicked);
这是该类
// Here is the class
public class NumericUpDnWithUpDnBtnEventHandlers : NumericUpDown
{
public event EventHandler UpButtonClicked = null;
public event EventHandler DownButtonClicked = null;
public void OnUpButtonClicked(EventArgs e)
{
EventHandler eventCopy = UpButtonClicked;
if (eventCopy != null)
eventCopy(this, e);
}
public void OnDownButtonClicked(EventArgs e)
{
EventHandler eventCopy = DownButtonClicked;
if (eventCopy != null)
eventCopy(this, e);
}
public NumericUpDnWithUpDnBtnEventHandlers()
{
}
public override void DownButton()
{
try
{
OnDownButtonClicked(new EventArgs());
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
base.DownButton();
}
public override void UpButton()
{
try
{
OnUpButtonClicked(new EventArgs());
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
base.UpButton();
}
}
关注点
在处理这个类的过程中,我了解到,如果我需要提供一个通用的 EventHandler
,它不需要传递任何特殊的参数,并且可以使用基本的 EventArgs
类,我不需要在项目的命名空间中定义一个 delegate
。我可以简单地在我的类中定义类型为 EventHandler
的 public
事件。这也确保了该类是独立的,并且不依赖于任何外部数据。您可以进行一些小的变化,并为 UpButton
和 DownButton
提供单个事件,前提是您传递 UpDownEventArgs
类,并将 ButtonID
值设置为 1
表示 UpButton
,2
表示 DownButton
。这是一个例子
public event EventHandler UpDnButtonClicked = null;
public void OnUpDnButtonClicked(UpDownEventArgs e)
{
EventHandler eventCopy = UpDnButtonClicked;
if (eventCopy != null)
eventCopy(this, e)
}
public override void UpButton()
{
try
{
OnUpDnButtonClicked(new UpDownEventArgs (1));
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
base.UpButton();
}
我没有这样做,因为文档对 UpDownEventArgs
类构造函数说:"此构造函数支持 .NET Framework 基础结构,不应直接从您的代码中使用。"
更新
以下是在您的项目中使用此派生控件的一些简单步骤
- 创建一个 Visual C# Windows 窗体项目。
- 将 NumericUpDnWithUpDnBtnEventHandlers.cs 类复制到您的项目目录。
- 现在从 VS 菜单中选择“项目 -> 添加现有项”,并将该类添加到您的项目中。
- 成功添加该类后,构建您的项目/解决方案。
- 您应该在工具箱中看到一个组件
NumericUpDnWithUpDnBtnEventHandlers
。 - 只需将新控件拖放到您的窗体上即可。
- 点击控件并设置其属性。除了
CallBase
之外,所有属性都是标准的,它默认为true
,但您可以将其设置为false
,如果您愿意。 - 现在在属性窗口中,单击“事件”。
- 您将在所有标准
NumericUpDown
事件旁边看到UpButtonClicked
和DownButtonClicked
事件。 - 双击每一个,将为您添加事件处理程序。
- 现在您可以在这些事件处理程序中执行任何您需要做的事情。
- 提供了一个非常简单的演示项目。
历史
- 2008 年 9 月 10 日:第一个版本
- 2009 年 3 月 10 日:向文章添加了演示项目和“更新”部分