Access form component from another class

32,980

Solution 1

Use a delegate for setting your label

public class MyClass {
    Action<String> labelSetter;

    public MyClass(Action<String> labelSetter) {
        this.labelSetter = labelSetter;
    }

    public void MyMethod() {
        labelSetter("outside");
    }
}

.

public void buttonOut_Click(object sender, EventArgs e) {
    var outside = new MyClass(UpdateLabel);
    outside.MyMethod();
}

Solution 2

a bit unsure because the example actually leaves some bits unclear... but here is a try:

public class MyClass
{
    public void MyMethod(Form1 F)
    {
         F.UpdateLabel("outside");
    }
}

this works as long as MyClass is NOT running on a different thread - otherwise the call to UpdataLabel must be synchronized with the UI thread...

EDIT:

private void buttonOut_Click(object sender, EventArgs e)
{
    MyClass Outside = new MyClass();
    Outside.MyMethod(this);
}

Solution 3

Either go with Yahia's way (it has been updated and will work correctly) or try the following (probably overkill for what you're trying to do... whatever that is).

UPDATE:

Based on your comment in the question, you are also doing the work in MyClass on a different thread. Code change is below.

public partial class Form1 : Form
{
    // keep a reference to a MyClass object for your Form's lifetime
    private MyClass _myClass;

    public Form1()
    {
        InitializeComponent();

        // Intstantiate your MyClass object so you can use it.
        _myClass = new MyClass();

        // Register to the MyClass event called UpdateLabel.
        // Anytime MyClass raises the event, your form will respond
        // by running the UpdateLabelFromMyClass method.
        _myClass.UpdateLabel += UpdateLabelFromMyClass;
    }

    private void button1_Click(object sender, EventArgs e)
    {
        // Call MyMethod in your MyClass object. It will raise
        // the UpdateLabel event.

        // update, you are calling this on a background thread?
        _myClass.MyMethod();
    }

    void UpdateLabelFromMyClass(string message)
    {
        // Update your label with whatever message is passed in
        // from the MyClass.UpdateLabel event.

        // UPDATE: If called from a background thread you'll need this:
        this.BeginInvoke( (Action) (()=>
        {
            label1.Text = message;
        }));            
    }
}

public class MyClass
{
    // An event that can be raised, allowing other classes to
    // subscribe to it and do what they like with the message.
    public event Action<string> UpdateLabel;

    public void MyMethod()
    {
        // Raise the UpdateLabel event, passing "Outside" as
        // the message.
        UpdateLabel("Outside");
    }
}
Share:
32,980
cozzy
Author by

cozzy

Updated on November 28, 2020

Comments

  • cozzy
    cozzy over 3 years

    I hope that the title and this simple example says everything.

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
    
        public void UpdateLabel(string str)
        {
            label1.Text = str;
           MessageBox.Show("Hello");
        }
    
        private void buttonIn_Click(object sender, EventArgs e)
        {
            UpdateLabel("inside");
        }
    
        private void buttonOut_Click(object sender, EventArgs e)
        {
            MyClass Outside = new MyClass();
            Outside.MyMethod();
        }
    }
    
    public class MyClass
    {
        public void MyMethod()
        {
             Form1 MyForm1 = new Form1();
             MyForm1.UpdateLabel("outside");
        }
    }
    

    When I'm trying to change lable1 from MyClass it does nothing. But I can get to the UpdateLable method from outside, it says Hello to me, it just doesn't change the label.