Click here to Skip to main content
15,908,444 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, I'm really frustrated about this. I can't find any solution about this. I have a label and a numeric updown in my form and what I'm trying to do is to decrease the value of the label whenever the numeric updown is pressed up and increase the value of the label when the numeric updown is pressed down.

The label has a value of 100 and I want to decrease it whenever the numeric updown is pressed up and vice versa.

What I have tried:

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

        Dim oldvalue As Integer


        If numCategory.Value > oldvalue Then

            lblpoints.Text -= 1

        ElseIf numCategory.Value < oldvalue Then


            lblpoints.Text += 1
        Else

            lblpoints.Text = ""

        End If

        oldvalue = numCategory.Value



    End Sub


I tried the above code it seems that it just make everything a mess.
Posted
Updated 5-Apr-20 0:52am

Look at your code:
VB
Dim oldvalue As Integer
Declares oldvalue as a local variable, the value of which will be discarded when the method returns.

Move the declaration outside the method, and it'll start to work better. (Not perfect, but that's a bug you'll need to find and fix later - testing properly will show it up).
 
Share this answer
 
Comments
lelouch_vi 2 5-Apr-20 4:33am    
Wow! thanks a lot. It works just like what I wanted. I'm wondering how does it behave that way? I put the oldvalue on a class level and it works fine now. Kindly please explain what did just happened?
OriginalGriff 5-Apr-20 4:59am    
It's gets complicated, but I'll try to simplify it as much as I can. There are two type of memory in .NET: the heap, and the stack. Heap is where all the items you create with the New keyword are stored, the stack is much smaller, and holds information that is created and destroyed while your app runs. You call a method, it stores where to Return to on the stack, and allocates space on the stack for each of the variables the method will use (generally to store references to items on the Heap). When the method returns, that information is discarded (though the Heap data persists, the local references to it are discarded), and control is passed back to the place the method was called from.
It's like a stack of coins: put a piece of paper on the top, then add five coins: in order to read what is on the paper you have to remove the coins first. The paper is the return address, the coins are local variables.
So each time you call your method, the local variables are created all over again, qand have no idea what the previous content might have been - they start again from scratch.
Placing the definition of a variable outside any methods makes it part of the class, so it is not local any more, and isn't destroyed when the method exits.

That's a big (you no idea how big!) oversimplification, but without being able to see when your eyes start to glaze over it's hard to be any more precise! :laugh:
lelouch_vi 2 5-Apr-20 5:05am    
haha!thank you for the effort to summarize half of my semester. I'm sorry for asking too many questions but I just want to clarify things so I can learn more.
lelouch_vi 2 5-Apr-20 4:39am    
is it gonna be the same if I declare oldvalue as static inside the method?
lelouch_vi 2 5-Apr-20 4:47am    
May I know what exactly happens with this line? lblpoints.Text -= 1 I have a "100" text on that "lblpoints" and I'm a little bit confuse with this expression
"lblpoints.Text -= 1" because it looks like it's decrementing the text value of the label even though it's a string?
this is by far my best solution to my own problem. If there are still improvements that are needed please correct me.
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


and to clear the value of the numeric updown control without the label being crazy after the method I just discover this:

VB
numCategory.ResetText()


that is the reason why my label incrementing 1 after the method has done its job because I used
VB
numCategory.value = nothing 
to clear the numeric control and so it messes with my code.

Thank you OriginalGriff for helping and patience haha.
 
Share this answer
 
v2

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