Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

A Derived NumericUpDown that Provides Handlers for NumericUpDown's Up and Down Button

0.00/5 (No votes)
11 Mar 2009 2  
A class derived from NumericUpDown that provides handlers for NumericUpDown's Up and Down buttons.

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:

// 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:

  1. Create a Visual C# Windows Form project.
  2. Copy the NumericUpDnWithUpDnBtnEventHandlers.cs class to your project directory.
  3. Now choose “Project->Add existing item” from the VS menu and add the class to your project.
  4. Once the class is successfully added, build your project/solution.
  5. You should see a component NumericUpDnWithUpDnBtnEventHandlers in the toolbox.
  6. Just drag and drop the new control onto your form.
  7. 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.
  8. Now in the properties window, click on Events.
  9. You will see UpButtonClicked and DownButtonClicked events besides all the standard NumericUpDown events.
  10. Double click on each one and the event handlers will be added for you.
  11. Now you can do whatever you need in these event handlers.
  12. 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

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here