如何在Windows窗体中向Form类添加方法?

时间:2022-08-25 23:58:09

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

我想在Windows.Forms.Form类中添加一个新方法..

Please help how to do this if anyone knows..

如果有人知道,请帮助如何做到这一点..

2 个解决方案

#1


1  

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.

您无法修改.NET Framework。你可以扩展它。在Visual Studio中添加新表单时,您将创建一个派生自System.Windows.Forms.Form的类。在该课程中,您可以添加所有您喜欢的方法。

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

此外,ASP.NET用于创建基于Web的应用程序,而不是Windows窗体应用程序。这两者几乎没有任何关系。

#2


0  

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

在.NET 3.5中,您可以为Form类创建扩展方法,如下所示:

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

然后你可以调用(当然在Form的代码中):

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

#1


1  

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.

您无法修改.NET Framework。你可以扩展它。在Visual Studio中添加新表单时,您将创建一个派生自System.Windows.Forms.Form的类。在该课程中,您可以添加所有您喜欢的方法。

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

此外,ASP.NET用于创建基于Web的应用程序,而不是Windows窗体应用程序。这两者几乎没有任何关系。

#2


0  

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

在.NET 3.5中,您可以为Form类创建扩展方法,如下所示:

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

然后你可以调用(当然在Form的代码中):

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