[Solved] How to use form’s function in other class C# [duplicate]


I think you’re meaning to pass a method as parameter, so the method can execute it as a callback. You should pass a method (without the parenthesis) to the other class and It must match the Action<> definition.

public partial class Form1 : Form
{

    public void PaintGui(int percent)
    {
        Label1.Text = percent.ToString() + "% completed";
        Label1.Update();
    }

    private void btnCalc_Click(object sender, EventArgs e)
    {
        //object of some class
        OtherClass other = new OtherClass();
        other.DoWork(PaintGui);
    }
}

// FOR EXAMPLE
public class OtherClass
{
    public void DoWork(Action<int> action)
    {
        for(int i=0;i<=100;i++)
        {
            action(i);
            Thread.Sleep(50);
        }
    }
}

solved How to use form’s function in other class C# [duplicate]