零.引言
PropertyGrid显示一个对象的属性和事件时,可以设置其默认属性和事件,也就是当你选中对象时,propertyGrid中焦点在哪一个属性或事件上。为对象的属性提供默认值,使PropertyGrid显示属性时,更加友好。
一.默认属性和默认事件
PropertyGrid能识别默认属性和事件,例如在设计时,双击Form框,就会跳到Form的Load事件中,这是因为Form的默认事件是Load。当你选中属性框中的某一项后,该项会着色(蓝色)选中,在属性和事件选项卡之间切换,就会发现,选中的始终是默认的属性和事件(例如,Form,选中的就是Text属性和Load事件)。
下面我们就来设计默认属性和事件,还是以MyControl为例。
1 //控件 2 [DefaultProperty("Angle")] 3 [DefaultEvent("Test")] 4 public class MyControl : System.Windows.Forms.UserControl 5 { 6 private double _angle = 90D; 7 private Color _penColor = Color.Red; 8 9 public delegate void TestDefaultEvent(); 10 public event TestDefaultEvent Test; 11 12 [BrowsableAttribute(true)] 13 public double Angle 14 { 15 get 16 { return _angle; } 17 set 18 { _angle = value; } 19 } 20 21 [Browsable(true)] 22 public Color PenColor 23 { 24 get 25 { 26 return _penColor; 27 } 28 set 29 { 30 _penColor = value; 31 Invalidate(); 32 } 33 } 34 public MyControl() 35 { 36 37 } 38 39 protected override void OnPaint(System.Windows.Forms.PaintEventArgs e) 40 { 41 e.Graphics.DrawString("The Angle is " + _angle, this.Font, new SolidBrush(this._penColor),0,0); 42 } 43 }