Calling a function in the Form Class from another Class, C# .NET

A quick and dirty way is to create a reference of the MainForm in your Program.cs file as listed above.

Alternatively you can create a static class to handle calls back to your main form:

    public delegate void AddStatusMessageDelegate (string strMessage);

    public static class UpdateStatusBarMessage
        {

        public static Form mainwin;

        public static event AddStatusMessageDelegate OnNewStatusMessage;

        public static void ShowStatusMessage (string strMessage)
            {
            ThreadSafeStatusMessage (strMessage);
            }

        private static void ThreadSafeStatusMessage (string strMessage)
            {
            if (mainwin != null && mainwin.InvokeRequired)  // we are in a different thread to the main window
                mainwin.Invoke (new AddStatusMessageDelegate (ThreadSafeStatusMessage), new object [] { strMessage });  // call self from main thread
            else
                OnNewStatusMessage (strMessage);
            }

        }

Put the above into your MainForm.cs file inside the namespace but separate from your MainForm Class.
Next put this event call into your MainForm.cs main class.

     void UpdateStatusBarMessage_OnNewStatusMessage (string strMessage)
     {
          m_txtMessage.Caption = strMessage;
     }

Then when you initialise the MainForm.cs add this event handle to your form.

     UpdateStatusBarMessage.OnNewStatusMessage += UpdateStatusBarMessage_OnNewStatusMessage;

In any UserControl or form associated with the form (MDI) that you want to call, just us the following...

     UpdateStatusBarMessage.ShowStatusMessage ("Hello World!");

Because it is static it can be called from anywhere in your program.


It's quite easy. Either pass a reference to an existing form in the call, or create a new instance of your form and then call your method just like any other:

public class MyForm : Form
{
    public void DoSomething()
    {
        // Implementation
    }
}

public class OtherClass
{
    public void DoSomethingElse(MyForm form)
    {
        form.DoSomething();
    }
}

Or make it a static method so you don't have to create an instance (but it won't be able to modify open form windows).

UPDATE

It looks like the ImageProcessing class never gets a reference to the form. I would change your code slightly:

class ImageProcessing
{
    private frmMain _form = null;    

    public ImageProcessing(frmMain form)
    {
        _form = form;
    }

    private UpdateStatusLabel(string text)
    {
        _form.StatusUpdate(text);
    }
}

And then one small tweek in the Form constructor:

ImageProcessing IP = new ImageProcessing(this);

You can do that in easy way:

1- create public class and define public static variable like this:

class Globals
{
   public static Form1 form;
}

2- in load function at the form write this line:

public partial class Form1 : Form
{
    private void Form1_Load(object sender, EventArgs e)
    {
        Globals.form= this;
    }

    public void DoSomthing()
    {
        ............
    }
}

3- finally, in your custom class you can call all public functions inside the form:

public class MyClass
{
    public void Func1()
    {
        Globals.form.DoSomthing();
    }
}

I hope this code will be useful to you:)

Tags:

C#

.Net

Winforms