为 Windows 添加新属性






1.02/5 (17投票s)
2006年6月17日
1分钟阅读

34931

343
为 Windows 添加新属性
请检查此源代码文件中的 Form4 和 Form5
引言
向 Windows 窗体添加新属性
-
在解决方案资源管理器中,右键单击项目名称,然后选择“添加”>“添加 Windows 窗体”FormName
-
右键单击窗体上的任意位置,然后选择“查看代码”。在 视图中,在 Windows 窗体设计器生成的代码区域之后插入以下代码
//define constant values for State public enum State{Idle, Connecting, Processing} //use this field as storage location //for FormState property private State formState; //set attributes for FormState property [Browsable(true), EditorBrowsable(EditorBrowsableState.Never), Description(“Sets the custom Form state”), Category(“Custom”)] //Creating FormState property public State FormState { get { return formState; } set { formState = value; switch (formState) { case State.Idle: this.BackColor = Color.Red; this.Text = “Idle”; break; case State.Connecting: this.BackColor = Color.Orange; this.Text = “Connecting...”; break; case State.Processing: this.BackColor = Color.Green; this.Text = “Processing”; break; } } }
-
控制属性行为的属性
-
Browsable 指示属性是否在“属性”窗口中显示。其默认值为 true。
EditorBrowsable 指示属性是否应出现在
代码视图中对象的 IntelliSense 列表中。其值为
EditorBrowsableState 枚举类型,具有三个
可能的值——高级、始终和永不。其默认
值为始终,表示“始终列出此属性”。如果
您将值更改为永不,则该属性将隐藏在
IntelliSense 功能中。
Description 为属性指定描述字符串。当
指定 Description 属性时,它将显示在
选择属性时“属性”窗口的描述区域中。
Category 指定属性的类别。类别用于在
“属性”窗口中对属性列表进行分类。
在解决方案资源管理器中右键单击
-
项目名称,然后选择“添加”>“添加继承
-
窗体”
-
将新窗体命名为
-
anyname 使其从之前的窗体继承
-
检查属性窗口... 自定义类别和 Formstate 新属性