I have a C# project with many winforms in it. I would like to have a function available to all of these forms. The solution I have right now is a separate class with this function, and then in the forms like this:
Exit quitter = new Exit();
quitter.exitProgram(sender, e);
Is there a better way for my fnction to be available inside every form without having to initialize a class with this function every time?
You can write a helper
class, which will contain all common functionalities shared by all winforms and write static method to avoid object instantiation.
Something like,
public class WinformHelper
{
public static ExitProgram(object sender, System.EventArgs e)
{
//Your business logic
}
}
Use in winforms,
WinformHelper.ExitProgram(sender, e);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.