Click here to Skip to main content
15,867,568 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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.

VB
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


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.

VB
numCategory.Value = nothing


So I used this instead
VB
numCategory.ResetText()
Posted
Updated 18-Apr-20 15:23pm

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.
 
Share this answer
 
Comments
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!
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:
VB
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
VB
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.
VB
numCategory.Value = numCategory.Minimum
 
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