Accessing Form Label from Seperate Class
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: Accessing Form Label from Seperate Class

  1. #1
    Join Date
    Jul 2010
    Posts
    5

    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.

    What is the best way to handle this?

    Thanks,
    Brian Enderle

  2. #2
    Join Date
    Jul 2010
    Posts
    5

    Re: Accessing Form Label from Seperate Class

    Sorry, forgot to mention I am using VS 2008 (.NET 3.5).

    Brian Enderle

  3. #3
    Join Date
    Jul 2010
    Posts
    5

    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.

    Brian Enderle

  4. #4
    Join Date
    Jun 2008
    Posts
    2,477

    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.

  5. #5

    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.

  6. #6
    Join Date
    Jun 2008
    Posts
    2,477

    Re: Accessing Form Label from Seperate Class

    Quote Originally Posted by matthewsanford@gmail.com View Post
    What BigEd says is right, but you can do it by setting the "Modifiers" property of the label to "public"
    Sssshhhhhh...

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center