Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

.net

winforms

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.

like image 759
mouthpiec Avatar asked Jun 01 '10 13:06

mouthpiec


People also ask

Can you call a function from another class?

In Java, a method can be invoked from another class based on its access modifier. For example, a method created with a public modifier can be called from inside as well as outside of a class/package. The protected method can be invoked from another class using inheritance.

How do you call a function from one class from another class in C++?

Class B inherits from Class A, and I want class A to be able to call a function created in class B. using namespace std; class B; class A { public: void CallFunction () { B b; b. bFunction(); } }; class B: public A { public: virtual void bFunction() { //stuff done here } };


2 Answers

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

like image 152
bebosh Avatar answered Sep 30 '22 02:09

bebosh


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.

like image 42
Paul Talbot Avatar answered Sep 30 '22 02:09

Paul Talbot