Introduction
This article presents a very simple class which derives from the NumericUpDown
class. It overrides the NumericUpDown
's UpButton
and DownButton
methods and fires the corresponding events.
Background
I was looking for a way to handle the NumericUpDown
's up and down button click events. The basic NumericUpDown
class does not expose any such events, but luckily, it lets a derived class override the UpButton
and DownButton
methods. So, that's what I did, I derived a class from the NumericUpDown
class and overrode the UpButton
and DownButton
methods. I fire the corresponding events in these methods so they can be handled by the subscriber.
Using the Code
NumericUpDnWithUpDnBtnEventHandlers
is a very simple class based on the NumericUpDown
class. Simply add it to your project in the same namespace as your project. Then, instantiate a new instance of the NumericUpDnWithUpDnBtnEventHandlers
object, and set its size and position as desired. Subscribe to any events exposed by the base (NumericUpDown
) class as needed, and finally, subscribe to the "UpButtonClicked
" and "DownButtonClicked
" events, if needed. Don't forget to provide handlers for these events. Now, when the user clicks on the UpButton
in your control, the "UpButtonClicked
" handler will be called, and when the DownButton
is clicked, the "DownButtonClicked
" handler will be called. You can do whatever you need to in these handlers.
Finally, here is an example of how you would subscribe to the events (where numericUpDownVSize
is derived from NumericUpDnWithUpDnBtnEventHandlers
):
this.numericUpDownVSize.UpButtonClicked +=
new EventHandler(numericUpDownSize_UpButtonClicked);
this.numericUpDownVSize.DownButtonClicked +=
new EventHandler(numericUpDownSize_DownButtonClicked);
And, 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();
}
}
Points of Interest
While working on this class, I learned that if I need to provide a generic EventHandler
which doesn't need to pass any special arguments and can work with the basic EventArgs
class, I don't need to define a delegate
for it in the project's namespace. I can simply define public
events of type EventHandler
in my class. This also ensures that the class is self-contained and doesn't depend on any outside data. You can make a slight variation and provide a single event for both UpButton
and DownButton
, provided that you pass the UpDownEventArgs
class with the ButtonID
value set to 1
for UpButton
and 2
for DownButton
. Here is an example:
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();
}
I didn't do this because the documentation says this for the UpDownEventArgs
class constructor: "This constructor supports the .NET Framework infrastructure and is not intended to be used directly from your code."
Update
Here are some steps to easily use this derived control in your project:
- Create a Visual C# Windows Form project.
- Copy the NumericUpDnWithUpDnBtnEventHandlers.cs class to your project directory.
- Now choose “Project->Add existing item” from the VS menu and add the class to your project.
- Once the class is successfully added, build your project/solution.
- You should see a component
NumericUpDnWithUpDnBtnEventHandlers
in the toolbox.
- Just drag and drop the new control onto your form.
- Click on the control and set its properties. All properties are standard except
CallBase
which defaults to true
but you can set it to false
if you want.
- Now in the properties window, click on Events.
- You will see
UpButtonClicked
and DownButtonClicked
events besides all the standard NumericUpDown
events.
- Double click on each one and the event handlers will be added for you.
- Now you can do whatever you need in these event handlers.
- A very simple demo project is provided.
History
- 10th September, 2008: First version
- 10th March, 2009: Added demo project and "Update" section to the article