我为什么要插入来自设计师的非UI组件Windows.Forms的? [英] Why should I insert a non-UI Windows.Forms component from the designer?
问题描述
在C#(和Visual Basic),你可以从直接添加一些非UI组件(那些没有从 System.Windows.Forms.Control的
继承)窗体设计器。这些组件的例子是 System.Windows.Forms.FolderBrowserDialog
和 System.Windows.Forms.Timer
。
In C# (and Visual Basic) you can add several non-UI components (those that don't inherit from System.Windows.Forms.Control
) directly from the form designer. Examples of those components are System.Windows.Forms.FolderBrowserDialog
and System.Windows.Forms.Timer
.
但是,什么是从加入非UI的利益与UI设计师控件,而不是直接从代码中实例化他们? ?还有背后的理由。
But what's the benefit from adding non-UI controls with the UI designer instead of instancing them directly from code? There's a rationale behind that?
推荐答案
我想使用非UI组件的设计,至少有以下好处:
I think using non-UI components in designer at least has these benefits:
设计时支持
对,你在Windows窗体有最强大的东西是用设计师安装组件的能力。结果
即使定时
不是一个UI组件,但您可以设置它的属性,如在设计的时间间隔。这适用于许多其他组件,如的BindingSource
, ErrorProvider
,...,您可以用非常友好的属性网格和类型。编辑和类型转换器配置在设计时的属性。
One of most powerful things that you have in windows forms is the ability to use designer to setup Components.
Even though Timer
is not a UI component, but you can set its properties like interval at design time. This applies to many other components like BindingSource
, ErrorProvider
, ... that you can use very friendly property grid and type editors and type converters to configure properties at design time.
- 当你想配置其他控件相关的性能;例如添加
的BindingSource
来的设计师,它使数据绑定非常简单。 - 当你需要使用扩展器提供商,如
HelpProvider
和工具提示
,因为它们与其他控件,这很容易在设计模式对其进行配置。 - 当你需要配置如
数据源
和数据成员
属性,它是非常友好的使用设计师并用巨大的属性网格的功能。 - 将组件添加为一流水平的领域,你可以使用deigner予以公布。
- 当你需要使用
表格
为组件,它利用设计师的完全可用。本地化
功能 - 当你需要简单地添加或移除事件处理程序,你可以使用属性网格做到这一点。
- When you want to configure dependent properties for other controls; for example by adding
BindingSource
to designer, it makes data binding very easy. - When you need to use extender providers like
HelpProvider
andTooltip
, since they are related to other controls, it is very easy to configure them at design mode. - When you need to configure properties like
DataSource
andDataMember
, it is very friendly to use designer and use great property grid features. - Components will add as class level fields, and you can make them public using deigner.
- When you need to use
Localizable
feature ofForm
for your components, it's completely available using designer. - When you need to simply add or remove event handlers you can do it using property grid.
标准代码
如果您在设计器生成的代码,你会看到我们来看一看:
If you take a look at designer generated code you will see:
- 为组件生成的代码支持
ISupportInitialize接口
使用了BeginInit在
和EndInit在
- 对于组件生成的代码,通过
this.components
来构造函数,然后使用它时,处置
- Generated code for components that supports
ISupportInitialize
uses theirBeginInit
andEndInit
- Generated code for components, pass
this.components
to constructor and then use it whenDispose
如果你不需要的设计时支持你写的组件标准的代码,所以这是完全可以使用代码中的主题。
If you don't need design-time support and you write standard code for components, so it's completely OK to use theme in code.
这篇关于我为什么要插入来自设计师的非UI组件Windows.Forms的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!