Click here to Skip to main content
13,052,696 members (65,113 online)
Click here to Skip to main content
Add your own
alternative version

Stats

25.5K views
13 bookmarked
Posted 10 Sep 2008

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

, 11 Mar 2009
Rate this:
Please Sign up or sign in to vote.
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Rafique Sheikh
Software Developer
United States United States
Although an electrical engineer by major, I slowly transformed into a software developer as I realized I liked working with logical AND and ORs more than the physical ones on chips. I have taught C++ at ITT tech and worked on videoconferencing as a programmer. A summer class at Harvard got me into windows programming and I have enjoyed it ever since.

I like hiking, camping, walking and swimming. Tennis gets me going too as does discussing human psychology.

You may also be interested in...

Pro

Comments and Discussions

 
GeneralFull working copy Pin
pierrecor4-Mar-09 20:22
memberpierrecor4-Mar-09 20:22 
AnswerRe: Full working copy Pin
Rafique Sheikh6-Mar-09 14:41
memberRafique Sheikh6-Mar-09 14:41 
AnswerRe: Full working copy Pin
Rafique Sheikh11-Mar-09 13:55
memberRafique Sheikh11-Mar-09 13:55 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170713.1 | Last Updated 11 Mar 2009
Article Copyright 2008 by Rafique Sheikh
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid