If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Accessing Form Label from Seperate Class

I am trying to update a labels text field from a class that is separate from the form class. I have created a UpdateStats class file to handle all the updating of stats, querying the database, etc. This class also runs as a background worker so it is on a separate thread from the form.

As the stat values are updated by UpdateStats I would like to update the label's text field in statsForm. I have implemented events/listeners to do this but I can not find a way to reference the label that I wish to update.

Re: Accessing Form Label from Seperate Class

Re: Accessing Form Label from Seperate Class

Also forgot to mention that there will be a number of these labels that I will be updating so I would prefer not to have to setup a separate listener or method for each label. I would prefer to have one listener that I could somehow reference the label via a parameter.

Re: Accessing Form Label from Seperate Class

You are approaching this in the wrong way. The UI code should be as centralized as possible; data classes should not be responsible for updating the UI, it creates a tight coupling between logically disparate areas of code, which in turn creates a maintenance headache.

Your data classes can simply return values and/or expose events that the UI (i.e., the Form class) can handle. The Form would then update the labels as needed. This way you do not have to maintain several areas of code when you wish to change the UI. Your data classes can remain intact and you only need edit your UI code.

Re: Accessing Form Label from Seperate Class

What BigEd says is right, but you can do it by setting the "Modifiers" property of the label to "public"

--------------------------------------------------------------------------------------------------------------------------
Disclaimer - Most likely any code I have posted as an answer was most likely written free hand and may have some minor compile errors, and is merely intended to give you the idea.

* The Perfect Platform for Game Developers: Android
Developing rich, high performance Android games from the ground up is a daunting task. Intel has provided Android developers with a number of tools that can be leveraged by Android game developers.

* The Best Reasons to Target Windows 8
Learn some of the best reasons why you should seriously consider bringing your Android mobile development expertise to bear on the Windows 8 platform.