Change label when variable changes

polaughlin
polaughlin used Ask the Experts™
on
This is probably a very simple question, but its something I never got around to learning.  How would I make it so that when a certain global variable changes, so does a label on a form without making a timer.  I had this for my timer:

Private Sub Timer1_Timer()

Label1.Caption = TheVar

end sub

But I remember seeing somewhere that timers shouldn't be used unless completely necessary because of the resources they CAN take up and I was calling this timer every second.  If I'm wrong, and I can leave the timer the way it is because it does so little, then let me know.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Author

Commented:
Edited text of question.
Dr. Kamal MehdiIT Director / IT Project Manager

Commented:
The solution depends on how you change your global variable.
For example, if the variable is an input from the user (in a text box), then simply you can use the Text1_Changed event to synchronize the label's caption. If you change your variable within your code, then you can simply add a single line of code to change the label caption.
Hope that helps.
Commented:
You can add public properties instead of the public variable. For example like this..

Private m_PubVar As Long

Public Property Let PubVar(New_Value As Long)

  m_PubVar = New_Value
  Form1.Label1.Caption = m_PubVar    <-- the value has changed update the label caption, or whatever
 
End Property

Public Property Get PubVar() As Long

  PubVar = m_PubVar  
 
End Property
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Author

Commented:
The problem is, kamall, the variable is changed often in many different places and I'm looking for an easier way to do it rather than going through and finding all of the places that it is changed and adding another line of code.  Thanks anyway.  I appreciate it. :-)

Author

Commented:
This is exactly what I was looking for.  Thanks.

Author

Commented:
Actually, after thinking about this and trying to work this into my program, I found out its not going to work the way I want it.  The variable is not only set in my program, its also referred to in various functions.  Example:

Not just this:

TheVar = "This this and this"

but also:

MsgBox TheVar, vbCritical

etc.

VB returns the following error: Invalid use of property.

Is there some other way to do this and still be able to use the variable in a function, is there a workaround, or am I just doing something completely wrong? :P

Commented:
Have you added the "Public Property Get" procedure? The MsgBox works just fine for me, but when I remove the "Public Property Get" procedure I get the 'Invalid use of property' error. Make sure you add both Public Property Let and Public Property Get.

Author

Commented:
Ok, now I feel like a moron.  That worked.  Thanks alot.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial