简体   繁体   中英

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

Can someone please let me know by some code how I can call a function located in the Form class from another class?

Some code will be of great help!

thanks

EDIT: This is my current code

 public partial class frmMain : Form
 {
    //*******Class Instances*******
    ImageProcessing IP = new ImageProcessing();


    //********************
    public void StatusUpdate(string text)
    {
        tlsStatusLabel.Text = text;
    }//


    public frmMain()
    {
        InitializeComponent();            
    }//
}


class ImageProcessing
{

    private void UpdateStatusLabel(frmMain form, string text)
    {
        form.StatusUpdate(text);
    }//

   private UpdateLabel()
   {
        UpdateStatusLabel(frmMain, "Converting to GreyScale");
   }
}

the problem i am having is with frmMain.

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:)

You will have to create a new Form instance and then call it using the instance. If it is static then you can just call it by calling Form1.Method().

Form1 form1 = new Form1();
form1.Method();

I would suggest you move this common method to some other class.

If it is common method to be used in the UI then create a base Form class and derive all your forms with this base form class. Now move this common method to the base form. You can then use the method from any of the Form derived from it.

如果该方法与UI无关,那么从您的示例中我知道它不是,您可以创建另一个包含此方法的类,然后在Form类和您要使用它的任何其他位置使用它。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM