Click here to Skip to main content
15,911,890 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello, I was looking for a solution but couldn't find one. Like the heading is saying, I want to change the value of a textbox if user decrements the value of a up numeric up down control.

For example, if a user increment the value it will add 1 to the textbox value but if he wants to decrements the number up down control value the text value should subtract.

What I have tried:

I was trying to do it using the value changed event, it worked for incrementing but every time I decrement the value of nup it increments rather than decrements. :
C#
       decimal totalCost; //variable
private void nupWhiteC_ValueChanged(object sender, EventArgs e)
        {
            if (nupWhiteC.Increment == 1)
            {
                totalCost = totalCost + 0.50m;
                textBox1.Text = totalCost.ToString();
            }
            else if (nupWhiteC.Increment == -1)
            {
                totalCost = totalCost - 0.50m;
                textBox1.Text = totalCost.ToString();
            }
        } 


Thanks in advance.
Posted
Updated 11-May-17 4:49am
v2

First I would suggest learning how to debug because you'll figure things out like this much faster. The Increment property is how much the value changes when clicking up or down. So, in your case, Increment is set to 1 and so the first if statement is always running. Again, just put a breakpoint and you'll see what is happening.

All you need to do is check numWhite.Value and then set your Textbox accordingly.
 
Share this answer
 
The NumericUpDown.Increment Property (System.Windows.Forms)[^] specifies by how much a value will change if the user clicks either of the up or down buttons - not how much it changed by.

So regardless of whether the user clicked the up or down button, the value you get from the Increment property will not change (unless your code specifically changes it).

What I would suggest is that you use the Tag property of the control to contain the last value: you can then tell if the use incremented or decremented it (and by how much) by subtracting the last value from the current one (and then storing the current value into the Tag before you exit the handler).
I'm sure you know, but just for those less educated: the Tag property is an object - to you will need to cast it to a decimal in order to do the subtraction.
 
Share this answer
 
v2
Comments
Member 13081540 11-May-17 12:27pm    
Do you mean creating a decimal for example decimal oldValue and then
if (nupWhiteC.Value > oldValue)
{
totalCost = totalCost + 0.50m;
textBox1.Text = totalCost.ToString();
oldValue = nupWhiteC.Value;
}
else
{
totalCost = totalCost - 0.50m;
textBox1.Text = totalCost.ToString();
}
oldValue = nupWhiteC.Value;
OriginalGriff 11-May-17 13:58pm    
Pretty much, yes - but store the oldValue in the NumericUpDown.Tag property, so it's "part of" the control.

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