1 public partial class HexBoxShow : UserControl 2 { 3 public UInt16 _hexLength; 4 public HexBoxShow() 5 { 6 InitializeComponent(); 7 createControls(_hexLength); 8 } 9 10 public void createControls(int num) 11 { 12 int index = 1; 13 Label lbl = null; 14 int X = 4; 15 int Y = 4; 16 TextBox tb = null; 17 int A = 4; 18 int B = 18; 19 for (int i = 0; i < num; i++) 20 { 21 lbl = new Label(); 22 lbl.Left = X; 23 lbl.Top = Y; 24 lbl.Text= i.ToString(); 25 lbl.Width = 16; 26 lbl.Height = 12; 27 X += lbl.Width + 4; 28 tb = new TextBox(); 29 tb.Text = "0"; 30 tb.Left = A; 31 tb.Top = B; 32 tb.Width = 16; 33 tb.Height = 20; 34 A += tb.Width + 4; 35 if (index % 4 == 0) 36 { 37 X += 8; 38 A += 8; 39 } 40 if (lbl.Width > this.Width - X) 41 { 42 X = 4; 43 A = 4; 44 Y = lbl.Height + tb.Height + 4; 45 B = lbl.Height + tb.Height + 4; 46 } 47 this.Controls.Add(lbl); 48 this.Controls.Add(tb); 49 index ++; 50 } 51 } 52 53 #region 属性 54 [Description("设置或者获取显示的位数,一位表示四个二进制位")] 55 [Category("HexShow")] 56 [DefaultValue(2)] 57 public UInt16 HexLength 58 { 59 get { return _hexLength; } 60 set 61 { 62 if (value == null) 63 { 64 throw new ArgumentNullException(); 65 } 66 else 67 { 68 if (value >= 0 || value <= 64) 69 _hexLength = value; 70 else 71 throw new ArgumentException("HexLength must be between 0 - 64"); 72 } 73 } 74 } 75 #endregion 76 }
我设置的——hexLength属性为什么在初始化的时候,一直为0 啊?
想要默认值的话可以将
public UInt16 _hexLength
改为:
public UInt16 _hexLength = 2;
[DefaultValue(2)]这个特性是不管用的,这个特性的作用是:你把值设为2,这个值会在vs的属性窗体中粗体显示,其他值都是正常显示
在调用的时候设置默认值了嘛。没有设置的话, public UInt16 _hexLength;这样值默认会是0.
DefaultValue不是提供设计时的默认值,是当你输入的值与你设置的值相同时,这个值不变黑【加粗显示】,只是提供设计时的一种显示样式改变。
如果要达到你说的默认值效果,可以 public UInt16 _hexLength=2;