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

前端 未结 5 553
悲&欢浪女
悲&欢浪女 2020-12-28 21:28

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

相关标签:
5条回答
  • 2020-12-28 21:58

    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.

    0 讨论(0)
  • 2020-12-28 21:59

    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);
    
    0 讨论(0)
  • 2020-12-28 22:07

    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.

    0 讨论(0)
  • 2020-12-28 22:11

    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.

    0 讨论(0)
  • 2020-12-28 22:19

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

    0 讨论(0)
提交回复
热议问题