Click here to Skip to main content
14,638,192 members
Rate this:
Please Sign up or sign in to vote.
Hello I have a numeric updown control in my form and a label which changes the value depending on the numeric updown value change event. I also have a clear button which clears the numeric updown and a save button because my form is connected to a database.

What happening is when I press the up in numeric updown control 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 numeric updown 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 numeric updown control since I didn't click the save button I just cleared the numeric updown control.

What I have tried:

Below is my code for the numeric updown value changed. Please help.

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


            lblpoints.Text = ""

        End If

        oldvalue = numCategory.Value

this is the code for clear button but the problem with this is after I click the save button it fires my if statement above so after a save has been done it increments the label by one.

numCategory.Value = nothing

So I used this instead
Updated 18-Apr-20 15:23pm
Rate this:
Please Sign up or sign in to vote.

Solution 1

You can't do that without faffing - when you clear the NumericUpDown, the value is changed and that triggers the update.

What you could do is have two class level variables (don't use Static, that's not what you want at all) an integer which stores the last value, and a boolean which "overrides" the value.
Then in your change event, check the override: if it's set, clear it and use the stored value. If it isn't, use the value from the NumericUpDown.
When you what to clear the control, set the stored value and the override first, then clear the control.
lelouch_vi 2 9-Apr-20 4:15am
hello again. You're here again to save me. Since you posted this answer I'm really trying my best what you meant by that override. I can at least understand what you're trying to explain but the problem is I don't know where to start. I tried to declare a variable like what you've said in a class level as a boolean. and that's it. I don't know what should I do next. I'm helpless.
OriginalGriff 9-Apr-20 4:42am
In the handler, use an If statement to see if the boolean override value is True or False. I'm sure you know how to do that, if you stop panicking!
lelouch_vi 2 9-Apr-20 5:16am
haha, thanks for the moral support maybe I'm just overlooking things here. I'll let you know when I already have progress.
lelouch_vi 2 9-Apr-20 8:58am
Hi, I'm really lost now. I don't know what to do next.

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.
OriginalGriff 9-Apr-20 10:28am
Carefully read what I said in my original answer!
lelouch_vi 2 10-Apr-20 3:34am
Hello, OriginalGriff I managed to get around with my problem here. I honestly don't know how to do your suggestions and I'm sorry about that.

I made another button named capture to capture the value of the label before the NUD can change the value.
This the code I'm using to capture the value of the label.
retainedValue = Convert.ToInt32(lblpoints.Text)

and in the clear button, I added this code

numCategory.Value = 0lblpoints.Text = retainedValue.ToString()

Now it's working like what I intended to, but I need to click the capture button before I can do anything else with the NUD. I tried inserting the code to different events of the NUD just to avoid clicking the "capture" button first.
Please help me again. I want to avoid clicking the "capture" button.
Rate this:
Please Sign up or sign in to vote.

Solution 2

After weeks and days finding the solution I already found this:
I'll just leave this here for anyone seeking the same solution as mine.

This is now the code I'm using for the NUD_VALUECHANGED EVENT:
Shered num As Integer = 100

Private Sub numCategory_ValueChanged(sender As Object, e As EventArgs) Handles numCategory.ValueChanged

        lblpoints.Text = (num - numCategory.Value).ToString

    End Sub

Below is the code for my SAVE button because my project is connected to a database.
What it does is, it retains the value of the LABEL and changed the value of the NUD to 0 again while the LABEL stays to whatever value it has before the SAVE button is clicked
Static oldvalue As String
     oldvalue = lblpoints.Text
     numCategory.Value = 0
     lblpoints.Text = oldvalue
     num = Convert.ToInt32(oldvalue)

and for my CLEAR button:
If the user just want to revert the NUD to it's default value.
numCategory.Value = numCategory.Minimum

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

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100