简体   繁体   English

Control的默认属性

[英]Control's default properties

When I add a control to a form through the designer, not all properties of the control appear in the designer code. 当我通过设计器向表单添加控件时,并非控件的所有属性都出现在设计器代码中。 For example, when I add a ListBox the UseWaitCursor property does not appear in the designer code unless it is set to True . 例如,当我添加ListBox ,除非设置为True否则UseWaitCursor属性不会出现在设计器代码中。 When I change it to False it disappears from the designer code, which makes me think that the properties somehow have defaults and don't appear in the designer code if left at default. 当我将其更改为False它会从设计器代码中消失,这使我认为属性以某种方式具有默认值,并且如果保留默认值,则不会出现在设计器代码中。

Can someone please help me understand how the designer works and where all this is tracked. 有人可以帮助我了解设计师的工作方式以及跟踪所有这些内容。 The reason I ask is I am currently writing a class that extends a third party ActiveX control which I plan to initialize dynamically at run time. 我问的原因是我正在编写一个扩展第三方ActiveX控件的类,我打算在运行时动态初始化它。 I was going through the designer code (when the third party control is added through the designer) and a lot of its properties do not appear there. 我正在浏览设计器代码(通过设计器添加第三方控件时),并且很多属性都没有出现在那里。

This is done with the [DefaultValue] attribute. 这是通过[DefaultValue]属性完成的。 The Control.UseWaitCursor property looks similar to this: Control.UseWaitCursor属性看起来类似于:

[DefaultValue(false)]
public bool UseWaitCursor
{
   // etc..
}

So if you leave the value at False in the Properties window then the designer knows that it should not display the value in Bold and that it is not necessary to put the property assignment in the InitializeComponent() method since the default is already good. 因此,如果在“属性”窗口中将值保留为False,则设计者知道它不应以Bold显示值,并且不必将属性赋值放在InitializeComponent()方法中,因为默认值已经很好。 An ActiveX control will certainly have a lot of properties set at its default value as well. ActiveX控件肯定会将许多属性设置为其默认值。

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

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