Results 1 to 8 of 8

Thread: Label content updating

  1. #1

    Thread Starter
    New Member
    Join Date
    Jun 2017
    Posts
    8

    Label content updating

    Hi,

    I am putting together something to help with day to day but have no real experience in VB.

    I want to be able to show data in a Label that I can refresh/update with a button click. The problem I am having is that after the first click the labels are populated, further clicks do not refresh/update the label again.

    Assume I would need some sort of reset or loop on the button? Any help would be very much appreciated.
    Code:
    Imports System
    Imports System.IO
    Imports System.Windows.Forms.VisualStyles.VisualStyleElement.TaskbarClock
    
    Public Class Monitor
    
        Dim oaTlogpath As String = "networkLocation"
        Dim oaTlogdateTime As DateTime = Directory.GetLastWriteTime(oaTlogpath)
        Dim oaTlogCount = My.Computer.FileSystem.GetFiles("networkLocation")
    
    
        Dim path As String = "networkLocation"
        Dim dateTime As DateTime = Directory.GetLastWriteTime(path)
        Dim DMWpath As String = "networkLocation"
        Dim DMWdateTime As DateTime = Directory.GetLastWriteTime(DMWpath)
      
        Private Sub btnRefresh_Click(sender As Object, e As EventArgs) Handles btnRefresh.Click
    
            'Variables for comparisons for Like statements.
            Dim x As Integer = 250
            Dim t As DateTime = DateTime.Now
    
            oaTlogdateTime = Format(oaTlogdateTime, "hh:mm:ss tt")
            DMWdateTime = Format(DMWdateTime, "hh:mm:ss tt")
    
            lblTTime.Text = (oaTlogdateTime)
    
            lblTlogBacklog.Text = (oaTlogCount.Count)
            If oaTlogCount.Count < x Then
                lblTlogBacklog.BackColor = Color.Green
    
            Else
                lblTlogBacklog.BackColor = Color.Red
    
            End If
    
            lblOrderProc.Text = (DMWdateTime)
    
            'btnRefresh.Refresh()
    
        End Sub
    
    End Class

  2. #2
    Lively Member
    Join Date
    Jun 2018
    Location
    Krähental
    Posts
    64

    Re: Label content updating

    I assume you're referring to the label lblTTime

    You get the datetime into t

    But then you don't use t

    Instead you use oaTlogdateTime which never gets changed.

    So you are updating the label but you just keep writing the same datetime into it.

  3. #3
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,930

    Re: Label content updating

    The problem is that you set the labels based on the contents of variables (oaTlogdateTime etc) that do not change... rather than declaring/setting those variables at class level, move those lines inside btnRefresh_Click.


    Also note that you should never store the result of Format("...") (or the modern version .ToString("...") ) into a variable/property that is not a String, because it causes various problems (conversion and formatting mistakes, and errors). In this case rather than these two lines:
    Code:
            oaTlogdateTime = Format(oaTlogdateTime, "hh:mm:ss tt")
            lblTTime.Text = (oaTlogdateTime)
    ...you should have this:
    Code:
            lblTTime.Text = Format(oaTlogdateTime, "hh:mm:ss tt")
    ...or this:
    Code:
            lblTTime.Text = oaTlogdateTime.ToString("hh:mm:ss tt")

  4. #4

    Thread Starter
    New Member
    Join Date
    Jun 2017
    Posts
    8

    Re: Label content updating

    Quote Originally Posted by Axcontrols2 View Post
    I assume you're referring to the label lblTTime

    You get the datetime into t

    But then you don't use t

    Instead you use oaTlogdateTime which never gets changed.

    So you are updating the label but you just keep writing the same datetime into it.
    Its all 3 labels, 2 populate with a timestamp and the third is a count from a directory. All update correctly on the first press of the btnRefresh, but I am not able to update a second time, without closing the form and reopening.


    Quote Originally Posted by si_the_geek View Post
    The problem is that you set the labels based on the contents of variables (oaTlogdateTime etc) that do not change... rather than declaring/setting those variables at class level, move those lines inside btnRefresh_Click.


    Also note that you should never store the result of Format("...") (or the modern version .ToString("...") ) into a variable/property that is not a String, because it causes various problems (conversion and formatting mistakes, and errors). In this case rather than these two lines:
    Code:
            oaTlogdateTime = Format(oaTlogdateTime, "hh:mm:ss tt")
            lblTTime.Text = (oaTlogdateTime)
    ...you should have this:
    Code:
            lblTTime.Text = Format(oaTlogdateTime, "hh:mm:ss tt")
    ...or this:
    Code:
            lblTTime.Text = oaTlogdateTime.ToString("hh:mm:ss tt")
    Thank you, will tidy that up

  5. #5
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,043

    Re: Label content updating

    The thing to do is to learn just a bit more about the IDE. It isn't much, but it will revolutionize how you approach an issue like the one you are having. The key is to learn breakpoints and code stepping, the single (except that I mentioned two things) most valuable tools you can have, whether you are starting out or have been doing this for years.

    To set a breakpoint, put the cursor on the line you want the breakpoint at and press F9. That will put a maroon ball in the left margin. In the future, you can then set/remove breakpoints by clicking where that ball is. The thing about breakpoints is that as the code is running, when it hits a breakpoint, you will be taken to the code itself. At that time, you can see what is in each variable by hovering over it, or highlighting it and pressing Shift+F9. You can then step forwards through the code using either F10 or F11. The two do different things, but I'll leave that to you to figure out. Therefore, you can pause the code, see what the variables hold, then step forwards through the code to see how variables change, which branches are taken, and so forth. At any time you can resume normal running with F5.

    With those tools, you could set a breakpoint early in your code, such as on this line:

    Dim x As Integer = 250

    Then step through the code looking at the variables. By doing this, you would see that the labels WERE being set, just not set to something different, so you didn't see a change because they weren't changing. You would then be able to see what they were being set to, and why, which would allow you to figure out a different approach....which might be wrong, but it would be different. Those exact steps are how I will be spending pretty much all of this day. Without debugging tools like that, you are left staring at the code wondering what went wrong. With those tools, you can actually see what it is doing as it is doing it.
    My usual boring signature: Nothing

  6. #6

    Thread Starter
    New Member
    Join Date
    Jun 2017
    Posts
    8

    Re: Label content updating

    Quote Originally Posted by Shaggy Hiker View Post
    The thing to do is to learn just a bit more about the IDE. It isn't much, but it will revolutionize how you approach an issue like the one you are having. The key is to learn breakpoints and code stepping, the single (except that I mentioned two things) most valuable tools you can have, whether you are starting out or have been doing this for years.

    To set a breakpoint, put the cursor on the line you want the breakpoint at and press F9. That will put a maroon ball in the left margin. In the future, you can then set/remove breakpoints by clicking where that ball is. The thing about breakpoints is that as the code is running, when it hits a breakpoint, you will be taken to the code itself. At that time, you can see what is in each variable by hovering over it, or highlighting it and pressing Shift+F9. You can then step forwards through the code using either F10 or F11. The two do different things, but I'll leave that to you to figure out. Therefore, you can pause the code, see what the variables hold, then step forwards through the code to see how variables change, which branches are taken, and so forth. At any time you can resume normal running with F5.

    With those tools, you could set a breakpoint early in your code, such as on this line:

    Dim x As Integer = 250

    Then step through the code looking at the variables. By doing this, you would see that the labels WERE being set, just not set to something different, so you didn't see a change because they weren't changing. You would then be able to see what they were being set to, and why, which would allow you to figure out a different approach....which might be wrong, but it would be different. Those exact steps are how I will be spending pretty much all of this day. Without debugging tools like that, you are left staring at the code wondering what went wrong. With those tools, you can actually see what it is doing as it is doing it.
    Thanks for the tip will have a play with breaks. Just to clarify the labels are populating correctly, however only the first time i click refresh I am looking to be able to refresh the labels again without needing to close and reopen the form as the variables the labels link to will change as data is passed around the network. (Excuse the graphics below, this is a 'feeler' app at the moment.

    Think i see the issue now, axcontrols said it earlier but i didnt twig. The variables are being set at the start of the code rather than on the Refresh click. Would need to look at creating the variables on button click as well as updating the labels.

    Name:  monitor.png
Views: 72
Size:  5.4 KB
    Last edited by Adamric; Jun 19th, 2018 at 11:18 AM.

  7. #7
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,043

    Re: Label content updating

    From the code you posted, the labels ARE being refreshed every time you click the button. So, if you are not seeing what you expect to see, then you need to figure out why not. The breakpoints and stepping are the key to understanding that, though I believe Si pointed out the utlimate reason.

    At the very least, the code in the refresh button IS executing every time you press the button, and the labels ARE being set to what the code instructs them to be set to each time. What you are seeing is that new value, each and every time you press the button. Clearly, what you are seeing is not what you were expecting to see, but those code is faithfully doing what you told it to do, so the code is not doing what you want.

    One other possibility exists, to me. If you are expecting some visible 'refresh' to let you know that something happened...you aren't going to get it. Computers are too fast these days. If the values in the labels don't change, you won't even see a flicker. I'm not sure whether the labels will even bother redrawing (probably not, if the text in them doesn't change), but even if they did you wouldn't see it unless the text changed.
    My usual boring signature: Nothing

  8. #8

    Thread Starter
    New Member
    Join Date
    Jun 2017
    Posts
    8

    Re: Label content updating

    Fixed, variables needed to be moved from the Main Class to the Sub. *Facepalm

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width