[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 137
  • Last Modified:

Change label when variable changes

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.
0
polaughlin
Asked:
polaughlin
  • 5
  • 2
1 Solution
 
polaughlinAuthor Commented:
Edited text of question.
0
 
kamallCommented:
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.
0
 
VbmasterCommented:
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
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
polaughlinAuthor 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. :-)
0
 
polaughlinAuthor Commented:
This is exactly what I was looking for.  Thanks.
0
 
polaughlinAuthor 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
0
 
VbmasterCommented:
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.
0
 
polaughlinAuthor Commented:
Ok, now I feel like a moron.  That worked.  Thanks alot.
0

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

  • 5
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now