Home > front end >  C# How to access label element (defined in designer) from a different class using background worker
C# How to access label element (defined in designer) from a different class using background worker

Time:07-18

I don't know how to acces a label element (myLabel) from a different class outside my Control Class while using background worker.

I really don't know the correct syntax.

Thanks a lot.

This is my code:

    public partial class BasicControl : UserControl
    {
        // ...
        private void bw_DoWork(object sender, DoWorkEventArgs e)
        {
            this.Invoke(new MethodInvoker(delegate { myLabel.Text = "THIS WORKS!"; }));
            var moc = new MyOtherClass();
            string result = moc.myMethod();

        }
    }

    internal class MyOtherClass
    {

       public void myMethod()
       {
           myLabel.Text = "THIS DOES NOT WORK!"; // NOT WORKING
       }
    }

CodePudding user response:

The initial state

Toggling the checkbox starts and stops the worker after instantiating CustomDoWorkContext.

continuous events

  • Related