Microsoft 演练 "Inheriting from a Windows Forms Control" 是有效的程序吗?

Is the Microsoft walkthrough "Inheriting from a Windows Forms Control" a valid procedure?

已更新

我对 Visual Studio 和 C# 比较陌生。我遇到过许多 S.O 的帖子,其中的答案是指从 Microsoft 基础 class 派生(通过继承)并扩展它。我一直想弄清楚如何将自定义属性添加到基本 WinForms 控件并在 Visual Studio 中对其进行管理。

我偶然发现 Microsoft procedure 这样做。在看到总结这个过程的回复后,我正在更改问题并删除摘要(我的原始答案)

Ron Beyer 对 Microsoft 程序的评论如下。

最重要的是,只需从头开始构建派生的 class。结果是一样的

I really recommend against using a new "User Control" object and then changing the base class in the code-behind. This breaks the designer (you have to hand-edit the AutoScale property) and really there is no reason for the designer. Just start a new class file and derive the appropriate control, no need to do all the steps above. RonBeyer

@RonBeyer: For example, if I create a new class file (instead of a new project), how would I reuse the custom control in another project? If I don't use the procedure, how and where do I implement the Dispose method currently provided by the Designer.cs file? Do I need to re-implement the guts of the InitializeComponent method provided by the Designer.cs file, or will the InitializeComponent method in my standalone project take care of it? Is it really just as simple as creating a new class file? NovaSysEng

Yes, it is that simple. You don't need the Dispose method, the base class has it, unless you are using additional unmanaged resources, then you implement it normally (and call base.Dispose). You don't need the IntializeComponent call at all, you can add your own initialization code to the constructor if needed. Literally public class MyButton : Button { } is a perfectly valid derived control. RonBeyer