Click here to Skip to main content
15,909,953 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How do you check for up or down button in NumericUpDown?

NumericUpDown nMonth;
if (nMonth.UpButton)
{
code
}


Does not work

Thank you

e. simons
Posted
Updated 24-Jun-10 3:44am
v2

1 solution

UpButton and DownButton, and methods to actually perform an increment and decrement operation, not to determine what button was pressed.

What you can do is make use of the NumericUpDown.Tag to hold a copy of the value and use it for comparison on a ValueChangeEvent, this negates the need to make use of any local instance variables.

What you would do is on the Form initialisation is set the Tag to equal the Value and then use it for comparison later;

C#
public Form1()
{
    InitializeComponent();

    //On Form Initialisation, Set the NumericUpDown.Tag to Match the Value
    numericUpDown1.Tag = numericUpDown1.Value;
}

private void numericUpDown1_ValueChanged(object sender, EventArgs e)
{
    decimal newValue = numericUpDown1.Value;
    decimal oldValue = (decimal)numericUpDown1.Tag;

    //Test if incremented or decremented
    if (newValue > oldValue )
    {
        //Incremented
    }
    else
    {
        if (newValue.Equals(oldValue))
        {
            //No Change
        }
        else
        {
            //Decremented
        }
    }

    //Set the old to the new
    numericUpDown1.Tag = numericUpDown1.Value;
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900