简体   繁体   中英

How do I add a method to the Form class in Windows Forms?

I want to add a new method in Windows.Forms.Form class..

Please help how to do this if anyone knows..

You can't modify the .NET Framework. You can extend it. When you add a new form in Visual Studio, you will be creating a class that derives from System.Windows.Forms.Form . In that class, you can add all the methods you like.

Also, ASP.NET is used to create web-based applications, not Windows Forms applications. The two have almost nothing to do with each other.

In .NET 3.5 you can create extension methods to the Form class like so:

 public static class MyExtensions
    {
        public static string Foo( this Form form, string param1 )
        {
            return param1;
        }
    }

Then later you can call (somewhere in the code for the Form of course):

var foo = this.Foo("bar");

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM