Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Realtime update of Label

Realtime update of Label

Scheduled Pinned Locked Moved C#
databasehelpannouncement
6 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    Dowse
    wrote on last edited by
    #1

    Hi all I am trying to display the value of a variable in a label. Easy enough I hear you say, but the variable can be changed by many different methods and functions, (some in dll's,) none of which trigger any specific event that I would normally use to update the label. I need the label to show the current value of the variable in 'real time.'

    public class MyClass
    {
    public DateTime datStart;
    public DateTime datNext; <======== This is the one I'm trying to show
    public int intParam1;
    public int intParam2;
    //Extra fields removed for clarity
    }

    And then in my form code:

    MyClass _myVar = new MyClass();

    I have a label called lblInformation that I would like to always show the value of _myVar.datNext.ToString() I've spent many hours trying to find a solution so any help will be very much appreciated. Thank you

    L W N 3 Replies Last reply
    0
    • D Dowse

      Hi all I am trying to display the value of a variable in a label. Easy enough I hear you say, but the variable can be changed by many different methods and functions, (some in dll's,) none of which trigger any specific event that I would normally use to update the label. I need the label to show the current value of the variable in 'real time.'

      public class MyClass
      {
      public DateTime datStart;
      public DateTime datNext; <======== This is the one I'm trying to show
      public int intParam1;
      public int intParam2;
      //Extra fields removed for clarity
      }

      And then in my form code:

      MyClass _myVar = new MyClass();

      I have a label called lblInformation that I would like to always show the value of _myVar.datNext.ToString() I've spent many hours trying to find a solution so any help will be very much appreciated. Thank you

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      Hi, this is what I do in such situations: - create a Windows.Forms.Timer that ticks a few times per second, say 10Hz - in its Tick handler, set Label.Text=latestValue.ToString() - done Thay isn't exactly real-time, however it is almost as fast as the human eye can cope with, and it does not waste CPU cycles on updating all the time assuming the variable could actually be changing at a much higher rate. BTW: by using the right timer, I also avoided cross-thread problems. :)

      Luc Pattyn [Forum Guidelines] [My Articles]


      The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.


      D 1 Reply Last reply
      0
      • L Luc Pattyn

        Hi, this is what I do in such situations: - create a Windows.Forms.Timer that ticks a few times per second, say 10Hz - in its Tick handler, set Label.Text=latestValue.ToString() - done Thay isn't exactly real-time, however it is almost as fast as the human eye can cope with, and it does not waste CPU cycles on updating all the time assuming the variable could actually be changing at a much higher rate. BTW: by using the right timer, I also avoided cross-thread problems. :)

        Luc Pattyn [Forum Guidelines] [My Articles]


        The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.


        D Offline
        D Offline
        Dowse
        wrote on last edited by
        #3

        Luc Thank you for a prompt and elegant solution to my problem. Regards Martyn

        L 1 Reply Last reply
        0
        • D Dowse

          Hi all I am trying to display the value of a variable in a label. Easy enough I hear you say, but the variable can be changed by many different methods and functions, (some in dll's,) none of which trigger any specific event that I would normally use to update the label. I need the label to show the current value of the variable in 'real time.'

          public class MyClass
          {
          public DateTime datStart;
          public DateTime datNext; <======== This is the one I'm trying to show
          public int intParam1;
          public int intParam2;
          //Extra fields removed for clarity
          }

          And then in my form code:

          MyClass _myVar = new MyClass();

          I have a label called lblInformation that I would like to always show the value of _myVar.datNext.ToString() I've spent many hours trying to find a solution so any help will be very much appreciated. Thank you

          W Offline
          W Offline
          Wendelldh
          wrote on last edited by
          #4

          Maybe I am understanding the question, so I will tell you what I would do... Is it the case that this is an existing class that is used in many different places already, so you don't want to change it to much. If you have access to the code, you can do this... First change the member variable to private and rename.

          private DateTime dateNext;

          The add an event.

          public event EventHandler datenextChanged;

          Then add a property with the existing name.

          public DateTime datNext
          {
          get{return dateNext;}
          set
          {
          if(!datNext.Equals(dateNext) && datenextChanged != null)
          datenextChanged(this, EventArgs.Empty);
          }
          }

          Then you just need to subscribe to this event on your object and update the label in the handler. Maybe this will work for you if I am understanding the problem. Thanks, Wendell

          1 Reply Last reply
          0
          • D Dowse

            Hi all I am trying to display the value of a variable in a label. Easy enough I hear you say, but the variable can be changed by many different methods and functions, (some in dll's,) none of which trigger any specific event that I would normally use to update the label. I need the label to show the current value of the variable in 'real time.'

            public class MyClass
            {
            public DateTime datStart;
            public DateTime datNext; <======== This is the one I'm trying to show
            public int intParam1;
            public int intParam2;
            //Extra fields removed for clarity
            }

            And then in my form code:

            MyClass _myVar = new MyClass();

            I have a label called lblInformation that I would like to always show the value of _myVar.datNext.ToString() I've spent many hours trying to find a solution so any help will be very much appreciated. Thank you

            N Offline
            N Offline
            Nagy Vilmos
            wrote on last edited by
            #5

            have you tried making the data bindable and then the form's label simply binds to the instance value. When the datNext changes the label is updated for free.


            Panic, Chaos, Destruction. My work here is done.

            1 Reply Last reply
            0
            • D Dowse

              Luc Thank you for a prompt and elegant solution to my problem. Regards Martyn

              L Offline
              L Offline
              Luc Pattyn
              wrote on last edited by
              #6

              you're welcome. :)

              Luc Pattyn [Forum Guidelines] [My Articles]


              The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.


              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups