Answered How to clear numeric updown control and retained the value of the label?

LMCKC207

Member
Joined
Apr 9, 2020
Messages
17
Programming Experience
Beginner
I have a NumericUpDown in my Form and a Label which changes the value depending on the NumericUpDown value change event. I also have a clear Button which clears the NumericUpDown and a save Button because my form is connected to a database.

What is happening is when I press the up in NumericUpDown the value of the Label decreases and vice versa. It works like what I wanted to. Now, what I'm trying to do is whenever I click the clear Button I want the last value of the Label to be retained. Let's say, the last value of the Label is 80 and I changed the value of the NumericUpDown to 20 so the value of the Label now is 60 when I press the clear I want the value of the Label to be 80 again and clear the NumericUpDown since I didn't click the save button I just cleared the NumericUpDown.

Below is my code for the NumericUpDown value changed.

VB.NET:
Static num As Integer = 100
Static oldvalue As Integer

If numCategory.Value > oldvalue Then

    num -= 1

    lblpoints.Text = num.ToString

ElseIf numCategory.Value < oldvalue Then
    num += 1
    lblpoints.Text = num.ToString

Else

    lblpoints.Text = ""

End If

oldvalue = numCategory.Value
 
What does "clear" mean in the case of a NumericUpDown? Do you actually mean reset it to zero? If so then what's the problem? Just display the original number less the NUD value and it will just work. The original number less zero is the original number.
VB.NET:
Private defaultValue As Decimal = 80D

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Label1.Text = defaultValue.ToString()
End Sub

Private Sub NumericUpDown1_ValueChanged(sender As Object, e As EventArgs) Handles NumericUpDown1.ValueChanged
    Label1.Text = (defaultValue - NumericUpDown1.Value).ToString()
End Sub

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    NumericUpDown1.Value = 0D
End Sub
 
Thanks for the reply. The default value of the label is 100. whenever I increase the value of the NUD it should subtract 1 from the label and whenever I decrease the value of NUD it should add 1 to the label. The minimum value of NUD is 0 and Max is 100 I got that working pretty right.

My problem is like this, let's say for example I increase the value of the NUD to 20 so the label should become 80 and I got it working BUT if I click the "clear" button which has this code numCategory.ResetText() to clear the NUD the label stays at 80. What I want to happen is it should also reset to whatever value it has before the numCategory_ValueChanged event not to its original value because I'm using the value of the NUD as the points to be inserted in a database and I'm using the label as a display on how many points are left for the user.
 
Hi jmcilhinney,

I tried your code above and some other code to achieve what I'm trying to do. Now it's working like it intended to but I just want to clarify things because I have some confusion here.

Below is my original code to track down the up and down button of the NUD. So the label should be subtracted by add when the NUD is pressed up ad added by 1 when pressed down.
VB.NET:
If numCategory.Value > oldvalue Then

    num -= 1

    lblpoints.Text = num.ToString

ElseIf numCategory.Value < oldvalue Then
    num += 1
    lblpoints.Text = num.ToString

Else

    lblpoints.Text = ""

End If

I replace it with this from you.
VB.NET:
Private Sub NumericUpDown1_ValueChanged(sender As Object, e As EventArgs) Handles NumericUpDown1.ValueChanged
    Label1.Text = (defaultValue - NumericUpDown1.Value).ToString()
End Sub

Now I'm confused. I don't get it. How did the NUD know when I pressed up or down so it should add or subtract from the label? I'm really confused.Because based on what I can understand from this line
Label1.Text = (defaultValue - NumericUpDown1.Value).ToString()
is that, it subtracts from the defaultValue depending on the NUD's value. How does it add by 1 back to the defaultValue? the operation is subtraction how come it can do addition? I hope you can understand what I'm trying to point out here and thank you in advance.
 
The NumericUpDown isn't doing any arithmetic and there is no adding or subtracting 1. The NumericUpDown and the Label know nothing about each other. The code I provided is part of the form. The form knows about both controls and is responsible for moving any data into or out of them. Any time the Value property of the NumericUpDown changes, it raises its ValueChanged event. All we care about is that the Value property has changed and what its current value is. Nothing else matters. The Label should ALWAYS display the result of subtracting the current Value of the NumericUpDown from the default value. What happened before is irrelevant. The fact that you're not incrementing or decrementing each time means that, if you were to type an arbitrary value into the NumericUpDown, the code would handle that gracefully, because it simply gets the current Value with no care for what the old value was.
 
Hi jmcilhinney,

I appreciate your effort to explain to me what's happening behind the code but can you explain it in a more simplified way?
I'm sorry I didn't understand the whole part of your explanation. ???

What I can't understand is this
The Label should ALWAYS display the result of subtracting the current Value of the NumericUpDown from the default value.

so it ALWAYS dispaly the result AFTER subtracting the current value of the NUD from the DEFAULTVALUE. So how does it perform the increment to the label when it's a subtraction? I'm sorry I'm really confused.

The oldvalue thing is already solved. I already found a solution to do it. I just want to understand this
VB.NET:
Private Sub NumericUpDown1_ValueChanged(sender As Object, e As EventArgs) Handles NumericUpDown1.ValueChanged
    Label1.Text = (defaultValue - NumericUpDown1.Value).ToString()
End Sub

Like what you've said
Any time the Value property of the NumericUpDown changes, it raises its ValueChanged event.

I can understand this that every time the value is changed in the NumericUpDown
this line of code is executed Label1.Text = (defaultValue - NumericUpDown1.Value).ToString().

From this, I can understand that when I pressed the up or down button of the NumericUpDown the ValueChanged event is fired and the code above is executed. My question is how does it perform addition? I pressed down and the subtraction is performed. I pressed up and addition is performed?

You said:
The NumericUpDown isn't doing any arithmetic and there is no adding or subtracting 1
The NumericUpDown isn't doing any arithmetic itself but because of the code inside the ValueChanged event it does perform now? Am I right?
 
So how does it perform the increment to the label when it's a subtraction?
It doesn't.
My question is how does it perform addition?
It doesn't.

You are trying to complicate something that is very simple. Stop trying to work out how what I say is resulting in what you think is happening. It's not. Read the words I'm writing. The point of the Label is to display the result of the default value less the value in the NumericUpDown so that's what the code does. You don't care about whether the value in the NumericUpDown has been increased or decreased and by how much. You only care about what the current value is so that you can subtract that value from the default value and display the result. That's what my code does: it subtracts the current value from the default value and displays the result. I'm not going to explain this any further.
 
Hi jmcilhinney,

For clarity of my question I have attached this so you can understand fully what is bugging me.

NUD AND LABEL.gif


So above when I pressed up it subtracts 1 from the label.
When I pressed down it adds 1 to the label.

I'm using this code for the NumericUpDown:
VB.NET:
Private Sub NumericUpDown1_ValueChanged(sender As Object, e As EventArgs) Handles NumericUpDown1.ValueChanged
    Label1.Text = (defaultValue - NumericUpDown1.Value).ToString()
End Sub

Question is how come the label is adding 1? and since the value change event is fired whether I pressed up or down how does the label know when to add by 1 or subtract by 1?
 
Hi jmcilhinney,

Like what you've said
You are trying to complicate something that is very simple.

Maybe it's true. can't help it it's bugging me for days now. ? ? ?

Well before closing this thread because you said
I'm not going to explain this any further.

I add a breakpoint to where this line is located: Label1.Text = (defaultValue - NumericUpDown1.Value).ToString()
and study what's happening behind. Now I fully understand this. The defaultValue always stays to whatever value it has say it's 100. Everytime the
NumericUpDown1_ValueChanged event is fired, the value of the NumericUpDown is subtracted from defaultValue which is 100 ALWAYS. Now it makes sense to me. It makes the illusion that it looks like adding and subtracting by 1 to the label.

Thank you so much jmcilhinney and I'm sorry because it seems that I just wasted your time.
 
As a moderator, I'm not always sure what other users can see. Your first post looks like this to me:

1587528287778.png


You won't be able to see all those options but you may be able to see the Edit option. Use that if it's available and then change the title prefix. If it's not available because you have too few posts so far, I can do it for you.
 
Back
Top