Realtime update of Label
-
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
-
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
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.
-
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.
-
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
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
-
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
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.
-
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.