Calling a function in a form class from another class, C # .NET

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

Some code will be very helpful!

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 encountered is related to frmMain.

+7
c # winforms
source share
5 answers

A quick and dirty way is to create a MainForm link in the Program.cs file as described above.

Alternatively, you can create a static class to handle calls to the 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 the MainForm.cs file inside the namespace, but separate from your MainForm class.
Then put this call into the main class MainForm.cs.

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

Then, when you initialize MainForm.cs, add this event descriptor to your form.

  UpdateStatusBarMessage.OnNewStatusMessage += UpdateStatusBarMessage_OnNewStatusMessage; 

In any UserControl or form-related form (MDI) you want to invoke, we just follow ...

  UpdateStatusBarMessage.ShowStatusMessage ("Hello World!"); 

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

+10
source share

This is pretty easy. Either pass a link 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 do it with a static method, so you do not need to create an instance (but it will not be able to modify open form windows).

UPDATE

It seems that the ImageProcessing class never gets a link to the form. I would change your code a bit:

 class ImageProcessing { private frmMain _form = null; public ImageProcessing(frmMain form) { _form = form; } private UpdateStatusLabel(string text) { _form.StatusUpdate(text); } } 

And then one little tweek in the form constructor:

 ImageProcessing IP = new ImageProcessing(this); 
+3
source share

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

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

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

If the user interface uses the general method, create a base form class and display all your forms with this base form class. Now move this general method to the base form. Then you can use the method from any form derived from it.

+1
source share

If the method is not associated with the user interface, from your example, I understand that this is not so, 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 .

+1
source share

You can do it in a simple way:

1- create an open class and define a public static variable as follows:

 class Globals { public static Form1 form; } 

2- in the form load function 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 :)

+1
source share

All Articles