WinForms中的Control vs UserControl?Control、WinForms、UserControl、vs

2023-09-07 14:45:46 作者:过分热情!

.NET WinForms 中的 Control 和 UserControl 有什么区别?我想创建一个自定义控件,但我应该从哪个继承?我过去一直使用 Control 没有任何问题,但是创建自定义控件的推荐"方式是什么?

What is the difference between a Control and a UserControl in .NET WinForms? I want to create a custom control, but which one should I inherit from? I've always used Control in the past without any issues, but is the "recommended" way of creating a custom control?

推荐答案

这里有一篇关于这个问题的好文章.用户控件

Here is a good article about this question. user controls

总之

控件要么是继承的,要么是完全自定义的.您自己编写和处理许多事件.您甚至可以通过使用 GDI+ 来控制绘制控件的方式和时间绘图.

A Control is either inherited or completely custom. You write and handle many of the events yourself. You can even control how and when the control is drawn thru the use of GDI+ drawing.

UserControl 是一组控件,这些控件放在一起以某种方式使用.例如,您可以放置​​一个包含文本框、复选框等的 GroupBox.当您必须在多个表单或选项卡上/中放置同一组控件时,这很有用.注意:您也可以为 UserControls 编写一些自定义事件和绘图.

A UserControl is a collection of controls placed together to be used in a certain way. For example you can place a GroupBox that contains Textbox’s, Checkboxes, etc. This is useful when you have to place the same group of controls on/in multiple forms or tabs. Note: you can write some custom events and drawing for UserControls also.