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

前端 未结 5 596
悲&欢浪女
悲&欢浪女 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 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.

提交回复
热议问题