简体   繁体   English

在C#/ VS2010中扩展UI组件类

[英]Extending UI Component Class in C#/VS2010

I've only just started using C#/VS this past week and wondered how to do something which, I think, should be quite simple: 上周我才刚刚开始使用C#/ VS,想知道如何做应该很简单的事情:

What I want to do is extend the class used by a UI component and therefore implement my own methods in it - just for one instance of a UI component though. 我想做的是扩展UI组件使用的类,并因此在其中实现我自己的方法-尽管仅针对UI组件的一个实例。 If I was using xcode/objective c I would normally just change the class name of the component in interface builder and it would become an instance of that class which would in turn extend the original UI class. 如果我使用的是xcode / objective c,通常只需要在界面生成器中更改组件的类名称,它将成为该类的实例,从而扩展原始UI类。

How do I do something comparable using C#/Visual Studio? 如何使用C#/ Visual Studio做类似的事情?

You can take any component class in Windows Forms and subclass it. 您可以采用Windows窗体中的任何组件类并将其子类化。 Visual controls all derive from the Control class and you can do so as well. 可视控件都派生自Control类,您也可以这样做。

If your component is a User Control (ie, it derives from System.Windows.Forms.UserControl ), it should automatically appear in the Toolbox after you build the project. 如果您的组件是用户控件(即,它派生自System.Windows.Forms.UserControl ),则在构建项目后,它应自动出现在工具箱中。 For other components, you can add them to the Toolbox by right-clicking on the Toolbox and select Customize Toolbox, selecting the .NET Framework Components tab, clicking the Browse button, and selecting the DLL with the control. 对于其他组件,可以通过右键单击“工具箱”并选择“自定义工具箱”,选择“ .NET Framework组件”选项卡,单击“ 浏览”按钮,然后选择带有控件的DLL,将它们添加到“工具箱”中。

Remember that all (or most) UI components are classes, so they can be "extended" just like any other class. 请记住,所有(或大多数)UI组件都是类,因此可以像其他任何类一样对其进行“扩展”。

Some will have virtual members you can override to take special actions. 有些将具有虚拟成员,您可以覆盖这些虚拟成员以执行特殊操作。 In all cases, you can add properties, methods, and events to the components. 在所有情况下,都可以向组件添加属性,方法和事件。

Once you've created and built them, you can use them from the Toolbox, just as though they were the "built-in" .NET components. 一旦创建并构建了它们,就可以从工具箱中使用它们,就像它们是“内置” .NET组件一样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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