简体   繁体   English

C#.NET-如何从现有的Windows窗体控件继承

[英]C#.NET - How to inherit from an existing windows forms control

I'm used to working in visual C++. 我习惯于在可视C ++中工作。 In learning C#.NET I tend to try to find equivalent methods to how things are done in VC++. 在学习C#.NET时,我倾向于尝试找到与VC ++中的工作方式等效的方法。

When I have a button in C++, to get full control over it I create a class derived from CButton (the equivalent of System.Windows.Forms.Button in .NET) and then set this class as the type for my button in order to get all events and overrides routed into my code. 当我在C ++中有一个按钮时,要完全控制它,我创建了一个派生自CButton的类(等效于.NET中的System.Windows.Forms.Button),然后将该类设置为我的按钮的类型,以便获取所有事件和替代路由到我的代码。

In C# I haven't really found an equivalent way to do this. 在C#中,我还没有真正找到一种等效的方法来执行此操作。 There seems to be no way to set which class a control should be created from, and I haven't even bothered trying to replace the auto-generated code where the object is created since it is likely to be discarded the next minute. 似乎没有办法设置应从哪个类创建控件,并且我什至没有费心尝试替换创建对象的自动生成的代码,因为它很可能在下一分钟被丢弃。

What's the correct way of using my own class to front a user control? 使用我自己的类来呈现用户控件的正确方法是什么?

EDIT: I should clarify that I'm interested in adding my custom class to the existing project. 编辑:我应该澄清一下,我有兴趣将自定义类添加到现有项目中。 Most of the responses so far either provide non working ways of adding it to the existing project, or working ways of creating an external library with the component (DLL), but that's a bit overkill for my particular case. 到目前为止,大多数响应都提供了将其添加到现有项目中的无效方法,或者提供了使用组件(DLL)创建外部库的有效方法,但是对于我的特殊情况而言,这有点过头了。

There isn't any trick to it. 没有任何窍门。 Create a new .cs source file or class using Visual Studio, remember to include the proper using System.Windows.Forms statement at the top of the file, then inherit as you normally would (note: in .NET, there are no classes with the naming convention CWhatever). 使用Visual Studio创建一个新的.cs源文件或类,请记住在文件顶部using System.Windows.Forms语句包括适当的名称,然后像往常一样继承(注意:.NET中没有类,命名约定(无论如何)。

If you derive from System.Windows.Forms.Button, for instance, your new control will be available in the toolbox. 例如,如果您从System.Windows.Forms.Button派生,则新控件将在工具箱中可用。 A new tab should appear at the top and will be named [Project Name] Components. 一个新的选项卡应该出现在顶部,并将被命名为[Project Name] Components。 You can just drag and drop it on a form. 您可以将其拖放到表单上。

As far as changing the type of a control already on a form, I've had success with changing the type in the designer generated code .cs file. 至于更改窗体上已经存在的控件的类型,我已经成功更改了设计器生成的代码.cs文件中的控件类型。

Here's a great article on creating custom .net controls: 这是有关创建自定义.net控件的精彩文章

The article discusses evey possible scenario, including using inheritance, and provides lots of sample code. 本文讨论了可能的情况,包括使用继承,并提供了许多示例代码。

EDIT: 编辑:

Add this to your project: 将此添加到您的项目:

public class DerivedTextBox: System.Windows.Forms.TextBox
{
}

And then build the project. 然后构建项目。 DerivedTextBox should then be available in the ToolBox. 然后,DerivedTextBox应该在工具箱中可用。

You may either override the functions you need with your own code, or simply create a new user control which derives from the System.Windows.Forms.Button. 您可以使用自己的代码覆盖所需的功能,也可以简单地创建一个派生自System.Windows.Forms.Button的新用户控件。

It depends on if you need to create a new control which will be used many times or if this is something you need to do for specific cases only. 这取决于您是否需要创建将被多次使用的新控件,还是仅在特定情况下需要执行此操作。

Edit: Have a look at the MSDN documentation: 编辑:看一下MSDN文档:

http://msdn.microsoft.com/en-us/library/5h0k2e6x(VS.71).aspx http://msdn.microsoft.com/en-us/library/5h0k2e6x(VS.71).aspx

abstract class MyClass :  Windows.Form
{
....
}


public class WindowsFormClass : MyClass
{
....
}

Similary you can create any custom class and derive it from the Control Class (Button, Textbox, Grid etc.), override its methods to add your functionality, and then create a custom object out of it. 类似地,您可以创建任何自定义类并从控件类(按钮,文本框,网格等)派生它,重写其方法以添加功能,然后从中创建一个自定义对象。 It will available to you in the toolbox and you can start using it in your project. 它会在工具箱中提供给您,您可以在项目中开始使用它。

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

相关问题 如何在C#.net(2010)Windows窗体应用程序中获取TreeView控件中的所有复选框值? - how to get the All the Checkbox values in TreeView control in C#.net(2010) Windows Forms Application? 如何重新调整图像C#.net Windows窗体的大小 - How To Re-size An Image C#.net Windows Forms 如何使用 USB 电缆 C#.net 将文件从 Windows 表格申请传输到 Xamarin Forms - How to transfer the file from Windows Form Application to Xamarin Forms using USB cable C#.net 在C#.NET和Windows窗体中将照片转换为灰度 - Photo to grayscale in C#.NET and Windows Forms 如何从C#.NET中的Windows服务调用Windows服务? - How to call Windows Service from Windows Service in C#.NET? 如何从C#.net中的控件中删除所有事件处理程序 - How to remove all Event handler from a Control in C#.net .NET:从MyControl for Windows.Forms继承 - .NET: Inherit from MyControl for Windows.Forms 如何使用c#.net代码基于credentails从Windows中现有的多个管理员帐户登录到一个管理员帐户 - how to login to one admin account from existing multiple admin accounts in windows, based on credentails using c#.net code C#.Net Panel Control和MDI Child表单 - 问题 - C#.Net Panel Control and MDI Child forms - issue 加速在Windows窗体(c#.net)应用程序中从磁盘加载图像 - Speed up loading an image from disk in a windows forms (c#.net) app
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM