tags:

views:

244

answers:

4

Hi,

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.

+1  A: 

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.

Amitabh
+2  A: 

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);
Justin Niessner
thanks, but how I would call the DoSomethingElse Function? My problem is what to type in the "form"? I am trying to insert the form name but I am getting an error that the form name is a type but used as a variable
mouthpiec
@mouthpiec - If you post some of your existing code, we might be able to give you a better idea of what you need to do.
Justin Niessner
Post your code, you must bhe doing something very wrong to get that compiler error.
Ben Robinson
code added above
mouthpiec
thanks Justin ... still not working :(I am getting an error in ImageProcessing IP = new ImageProcessing(this); saying that "this" is an uknown word. (but this constructor needs to have 1 argument.
mouthpiec
+1  A: 

If the method is not related to the UI, an from your example i understand that it's not, you can create another class that will contain this method and then use it in your Form class and in any other places where you want to use it.

Adrian Faciu
A: 

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.

Sres
thanks! i tried your option but i am getting an error in "OnNewStatusMessage(strMessage);" saying that OnNewStatusMessage is not set to an instance of an object
mouthpiec
i forgot to copy part of your code!issue solved! ;)
mouthpiec