For a windows form application how do I access the label control from a different class than the code hehind.

I have written a Windows form application using .Net.
I have one web form so the application is pretty simple.  On the web form I have a lable control which I use to display status messages. I can update the label in the code behind file for my web form MainForm.cs

 The code behind for the web form MainForm.cs calls a C# class "Scoper". I would also like to be able to update the label control within that class. How can i do that since the label control can be accessed from the main form code behind file MainForm.cs . I just don't know how to get a handle on the label control from my class Scoper
LVL 2
brgdotnetcontractorAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Todd GerbertIT ConsultantCommented:
Is this a Windows Forms project, or a Web Forms/Website project?
....either way same basic concept applies... you'd need to pass a reference to your form with the label to Scoper. There are a number of ways to do that, the first one that comes to mind is to have Scoper's constructor take a label control as a parameter.

public class MainForm
{
    private Label mainFormLabel = new Label();
    public void SomeMethodOfMainForm()
    {
        Scoper s = new Scoper(this.mainFormLabel);
        s.SomeScoperMethod("Hello World");
    }
}

public class Scoper
{
    private Label theLabel;
    public Scoper(Label label)
    {
        theLabel = label;
    }
    public void SomeScoperMethod(string Message)
    {
        theLabel.Text = Message;
    }
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
BIOGENIUSCommented:
it's not a good programatic way to access the label direct from another class.
but the good way is to make a method like:
internal void UpdateLabel(string new_value)
{
label1.Text=new_value;
}
then call this method from the other class.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.