locked
How to clear numeric updown control and retained the value of the label? RRS feed

  • Question

  • 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.

    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

    Thursday, April 9, 2020 10:02 AM

Answers

  • Hi lelouch_vi,

    >>When I pressed clear button the label should have the last value before the NUD modifies 

    It seems that you need the following code:

        Shared num As Integer = 100
        Private Sub numCategory_ValueChanged(sender As Object, e As EventArgs) Handles numCategory.ValueChanged
            lblpoints.Text = num - numCategory.Value
        End Sub
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Static oldvalue As String
            oldvalue = lblpoints.Text
            numCategory.Value = 0
            lblpoints.Text = oldvalue
            num = Convert.ToInt32(oldvalue)
        End Sub

    Result of my test:

    >>How can I do this video clip?

    You need a tool to do it.

    5 Free Tools To Screen Capture to Gif on Windows

    Note: This response contains a reference to a third party World Wide Web site. Microsoft is providing this information as a convenience to you. Microsoft does not control these sites and has not tested any software or information found on these sites; Therefore, Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there. There are inherent dangers in the use of any software found on the Internet, and Microsoft cautions you to make sure that you completely understand the risk before retrieving any software from the Internet.

    Best Regards,

    Xingyu Zhao


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by lelouch_vi Friday, April 17, 2020 12:30 PM
    Wednesday, April 15, 2020 6:20 AM

All replies

  • Hi

    Try this stand alone example and see if it does what you want.

    Option Strict On
    Option Explicit On
    ' Form1 with Label1, NumericUpDown1
    ' and Button1
    Public Class Form1
      Dim hold As Decimal = 0
      Dim skip As Boolean = False
      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        With NumericUpDown1
          .Minimum = -999
          .Maximum = 999
          .Value = 60
        End With
        Label1.Text = "60"
      End Sub
      Private Sub NumericUpDown1_ValueChanged(sender As Object, e As EventArgs) Handles NumericUpDown1.ValueChanged
        If skip Then
          skip = False
          Exit Sub
        End If
        Dim v As Decimal = 0D
        If Decimal.TryParse(Label1.Text, v) Then
          If NumericUpDown1.Value > hold Then
            Label1.Text = (v - 1).ToString
          Else
            Label1.Text = (v + 1).ToString
          End If
        End If
        hold = NumericUpDown1.Value
      End Sub
      Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        skip = True
        NumericUpDown1.Value = 0
        hold = 0
      End Sub
    End Class


    Regards Les, Livingston, Scotland

    Thursday, April 9, 2020 3:03 PM
  • First of all you have to make a decision when what happens. 

    If you currently click on the numericupdown, the label value is changed to its newest value. And setting it back to that has no sense. 

    I think you cannot fulfil what you want without at least an "Acknowledge" button. 



    Success
    Cor

    Thursday, April 9, 2020 5:17 PM
  • Hello. Thank you so much for this but this is bit close to what I'm trying to do. I don't really know where to start and you gave me an idea. 

    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

    • Edited by lelouch_vi Friday, April 10, 2020 2:24 AM
    Friday, April 10, 2020 2:22 AM
  • Right. Thanks for giving another idea.
    Friday, April 10, 2020 2:25 AM
  • Hi lelouch_vi,

    Thank you for posting here.

    As Cor Ligthert suggested, you need to create a button to save value of the label.

    For example:

    Here's the code of my test.

        Private Sub NumericUpDown1_ValueChanged(sender As Object, e As EventArgs) Handles numCategory.ValueChanged
            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
        End Sub
        Shared retainedValue As Integer
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            retainedValue = Convert.ToInt32(lblpoints.Text)
            MessageBox.Show($"retained: {retainedValue}")
        End Sub
        Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
            numCategory.Value = 0
            lblpoints.Text = retainedValue.ToString()
        End Sub

    Hope it can help you.

    Besides, if I have any misunderstanding, please let me know.

    Best Regards,

    Xingyu Zhao


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, April 10, 2020 2:56 AM
  • Hi Xingyu Zhao,

    Thanks for the solution. That's exactly what I'm looking for But I don't really understand this line of code would you mind to give a little explanation about this please?

            MessageBox.Show($"retained: {retainedValue}")

    it looks like what Cor Ligthert what really meant. I'm sorry for such a noob question. I'm trying to post here my screenshot of my project as well to clarify what I'm really trying to do but unfortunately I need to verify my account first.

    From what your clip is showing it looks like I need to click the button1 first before I can do anything with the NUD? so that label can remember its last value? am I correct? 

    Friday, April 10, 2020 3:10 AM
  • Hi lelouch_vi,

    Yes, you first need to click button1 to save the value of the label, 

    MessageBox.Show($"retained: {retainedValue}")

    This will show the value you saved.

    After clicking button2, label's text will be the saved value.

    Best Regards,

    Xingyu Zhao


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, April 10, 2020 3:22 AM
  • Hi Xingyu Zhao,

    Thanks I already knew about what does that code mean. Maybe I'll do a little digging about the 

    ($"retained: {retainedValue}")

    right after I solved my problem.

    About my original problem, I played around the code you gave me. Can we avoid the clicking of a button before the label can remember its last value? I tried putting it in another event of the NUD but seems like it didn't work. Do you have other suggestion?


    Friday, April 10, 2020 3:39 AM
  • Hi lelouch_vi,

    The code is related to Interpolated Strings.

    In order to retain the value of the label, you can create a new field in your database to save the values.

    Every time you click the clear button, label's text will change to the value stored in the database, and when you save the data to database, you can update the value of the stored label field in the database as needed.

    Best Regards,

    Xingyu Zhao


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.



    Friday, April 10, 2020 5:54 AM
  • Hi Xingyu Zhao,

    I already thought about making another field just to store the value of that label but I want to try a different way to achieve what I'm trying to do  and also I don't want to go through about connections and stuff just for a label as much as possible I want to do it in the simplest way BUT if I don't have any options left then that would be my last resort.

    Do you think that I'm out of option now? I'm already having this problem for two days now and I already searched in the web about this and unfortunately I didn't about find any.

    Friday, April 10, 2020 6:53 AM

  • Hi leshay,

    I tried your code. after I click the clear button the text in the label is incrementing 1. I think  it's firing the else statement.

    Saturday, April 11, 2020 2:56 AM

  • Hi leshay,

    I tried your code. after I click the clear button the text in the label is incrementing 1. I think  it's firing the else statement.

    Hi

    It is doing what I made it do, and doing it correctly. However, if it is not doing what you want it to do, then just ignore it.

    However, here is another stab at guessing what you want. Give it a try

    Option Strict On
    Option Explicit On
    ' Form1 with Label1, NumericUpDown1
    ' and Button1
    Public Class Form1
      Dim hold As Decimal = 0
      Dim skip As Boolean = False
      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        With NumericUpDown1
          .Minimum = -999
          .Maximum = 999
          .Value = 60
        End With
        Label1.Text = "60"
      End Sub
      Private Sub NumericUpDown1_ValueChanged(sender As Object, e As EventArgs) Handles NumericUpDown1.ValueChanged
        If skip Then
          skip = False
          hold = NumericUpDown1.Value
          Exit Sub
        End If
        Dim v As Decimal = 0D
        If Decimal.TryParse(Label1.Text, v) Then
          If NumericUpDown1.Value > hold Then
            Label1.Text = (v - 1).ToString
          Else
            Label1.Text = (v + 1).ToString
          End If
        End If
        hold = NumericUpDown1.Value
      End Sub
      Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        skip = True
        NumericUpDown1.Value = 0
        hold = 0
      End Sub
      Private Sub NumericUpDown1_KeyDown(sender As Object, e As KeyEventArgs) Handles NumericUpDown1.KeyDown
        skip = True
      End Sub
    End Class


    Regards Les, Livingston, Scotland



    • Edited by leshay Saturday, April 11, 2020 6:35 PM updated and added some code
    Saturday, April 11, 2020 4:09 PM
  • Hi Leshay,

    I appreciate you're help but it's not what I'm trying to do.

    I'm using the NUD as the points to be inserted in a database and I'm using the label to display how many points are left for the user to allocate.

    I wished I can attached my project's screenshot here but I'm not yet verified yet. What I'm trying to do is here is like what Xingyu Zhao's example. But the problem is I don't want to click a button first to remember what is the last value of the label before I clear the value of the NUD.

    Please help.

    • Edited by lelouch_vi Sunday, April 12, 2020 12:54 AM
    Sunday, April 12, 2020 12:49 AM
  • Hi

    The quality of the answers depends solely on the quality of the question.

    *

    Presumably then, as the User enters 'points' then thoise are stored in some container such as a List(Of Point) or some such.

    What are you storing them in?

    Since they are Points, requiring both an X and a Y parameter, are you working with 2 Combobxes displaying the number of points remaining to be used?

    *

    I know you can't post images yet, but, you can explain better details of whar you want to do.


    Regards Les, Livingston, Scotland

    Sunday, April 12, 2020 1:17 PM
  • Hi lelouch_vi,

    Thanks for your feedback.

    >>I don't want to click a button first to remember what is the last value of the label

    By adding a reference to System.Configuration, you can also use ConfigurationManager class to save the vaue of label. 

        Shared num As Integer = 100
        Private Sub NumericUpDown1_ValueChanged(sender As Object, e As EventArgs) Handles numCategory.ValueChanged
            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
        End Sub
        Dim config As Configuration
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            config = ConfigurationManager.OpenExeConfiguration(Application.ExecutablePath)
            If config.AppSettings.Settings("LabelValue") Is Nothing Then
                config.AppSettings.Settings.Add("LabelValue", "80")
            End If
            config.Save(ConfigurationSaveMode.Minimal)
        End Sub
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            numCategory.Value = 0
            lblpoints.Text = ConfigurationManager.AppSettings("LabelValue").ToString()
        End Sub

    Result of my test:

    Hope it can help you.

    Best Regards,

    Xingyu Zhao


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, April 14, 2020 6:58 AM
  • Hello Xingyu Zhao,

    Here is the screenshot of my project. I hope this can help me explain what I'm trying to do.

    Here are the properties of the label and the NUD.

    lblpoints.Text = "100"
    numCategoty.minimum = 0
    numCategory.Maximum = 100

    1st pic. When I pressed up the NUD the value of the label is subtracting by 1.(This is working fine)

    2nd pic. When I pressed down the NUD the value of the label is adding by 1.(This is working fine)

    3rd pic. When I pressed clear button the label should have the last value before the NUD modifies it. It is really important for me to have this working because the label acts as a display on how many points are left for the user to allocate.

    • Edited by lelouch_vi Tuesday, April 14, 2020 3:02 PM
    Tuesday, April 14, 2020 2:52 PM


  • I'm just curious. How can I do this video clip? 
    Tuesday, April 14, 2020 3:07 PM
  • Hi lelouch_vi,

    >>When I pressed clear button the label should have the last value before the NUD modifies 

    It seems that you need the following code:

        Shared num As Integer = 100
        Private Sub numCategory_ValueChanged(sender As Object, e As EventArgs) Handles numCategory.ValueChanged
            lblpoints.Text = num - numCategory.Value
        End Sub
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Static oldvalue As String
            oldvalue = lblpoints.Text
            numCategory.Value = 0
            lblpoints.Text = oldvalue
            num = Convert.ToInt32(oldvalue)
        End Sub

    Result of my test:

    >>How can I do this video clip?

    You need a tool to do it.

    5 Free Tools To Screen Capture to Gif on Windows

    Note: This response contains a reference to a third party World Wide Web site. Microsoft is providing this information as a convenience to you. Microsoft does not control these sites and has not tested any software or information found on these sites; Therefore, Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there. There are inherent dangers in the use of any software found on the Internet, and Microsoft cautions you to make sure that you completely understand the risk before retrieving any software from the Internet.

    Best Regards,

    Xingyu Zhao


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by lelouch_vi Friday, April 17, 2020 12:30 PM
    Wednesday, April 15, 2020 6:20 AM
  • Hi Xingyu Zhao,

    I'm really grateful for your patience. Now I got it working like what I wanted. Thumbs up for you.

        Static oldvalue As String
            oldvalue = lblpoints.Text
            numCategory.Value = 0
            lblpoints.Text = oldvalue
            num = Convert.ToInt32(oldvalue)


    I put the above code in my SAVE button because that's what I really wanted to happen. It capture the last value of the label which serves the very right purpose because like I've said the label display the remaining points to be allocated by user.

    Below is the code for my CLEAR button since once the value of the NUD resets to minimun the ValueChange event fires. So it looks like the label is binded to the value of the NUD.

    Private Sub btnClear_Click(sender As Object, e As EventArgs) Handles btnClear.Click

    numCategory.Value = numCategory.Minimum End Sub

    One more thing. I'm just a little confuse about this code:

     Private Sub numCategory_ValueChanged(sender As Object, e As EventArgs) Handles numCategory.ValueChanged
            lblpoints.Text = num - numCategory.Value
        End Sub

    How does the label increment and decrement whenever I press up or press down the NUD? How does the label knew I'm incrementing or decrementing? I don't get it how this line of code works. I'm a bit confuse.

    Thank you again Xingyu Zhao.


    • Edited by lelouch_vi Friday, April 17, 2020 12:31 PM
    Friday, April 17, 2020 12:30 PM