Question:

In C Sharp, how do I overide the a numericUpDown's 'OnMouseWheel' method. PLease provide an example.?

by  |  earlier

0 LIKES UnLike

In C Sharp, how do I overide the a numericUpDown's 'OnMouseWheel' method. PLease provide an example.?

 Tags:

   Report

1 ANSWERS


  1. Basically, you need to capture the MouseWheel event.

    For example, let's say for a particular operation or interaction with the GUI, the user should only be able to move the mouse wheel two notches in either direction.

    //put this somewhere in your form setup code

    numericUpDown1.MouseWheel += new MouseEventHandler(numericUpDown1_MouseWh...

        

    //this method will respond to the MouseWheel event, which is fired when OnMouseWheel is called

    void numericUpDown1_MouseWheel(object sender, MouseEventArgs e)

            {

                if (e.Delta == 2)

                {

                    MessageBox.Show("you moved the mouse wheel two notches.");

                }

            }

    Now, if you specifically want to override the OnMouseWheel method, you need to create your own NumericUpDown class and inherit from the standard NumericUpDown class. Then you can override the OnMouseWheel method.

    For example:

    class MyNumericUpDown:NumericUpDown

        {

            protected override void OnMouseWheel(MouseEventArgs e)

            {

                if (e.Delta == 2)

                {

                    MessageBox.Show("you can only move the mouse wheel two notches for this operation");

                }

            }

        }

    Which solution is better? Well it depends on your program. If you will have a lot of NumericUpDown controls that exhibit the same behavior, create your own class and override the methods.

    If only one NumericUpDown control will do this, then capture the event.

Question Stats

Latest activity: earlier.
This question has 1 answers.

BECOME A GUIDE

Share your knowledge and help people by answering questions.