13 |
[DefaultEvent("ValueChanged")] |
[DefaultEvent("ValueChanged")] |
14 |
public partial class MaskedHexBox : UserControl |
public partial class MaskedHexBox : UserControl |
15 |
{ |
{ |
|
const int ReferenceWidthPixels = 114; |
|
|
const double ReferenceWidthPercent = 0.50; |
|
|
|
|
16 |
public MaskedHexBox() |
public MaskedHexBox() |
17 |
{ |
{ |
18 |
InitializeComponent(); |
InitializeComponent(); |
79 |
public override string Text |
public override string Text |
80 |
{ |
{ |
81 |
get { return txtMaskedHexBox.Text.PadRight(this.TextLength + 2, '0'); } |
get { return txtMaskedHexBox.Text.PadRight(this.TextLength + 2, '0'); } |
82 |
set { txtMaskedHexBox.Text = value; } |
set { txtMaskedHexBox.Text = value; DoResize(); } |
83 |
} |
} |
84 |
public ulong Value |
public ulong Value |
85 |
{ |
{ |
167 |
} |
} |
168 |
} |
} |
169 |
this.Mask = mask.ToString(); |
this.Mask = mask.ToString(); |
170 |
|
DoResize(); |
171 |
} |
} |
172 |
} |
} |
173 |
private string CreateMask(int length) |
private string CreateMask(int length) |
198 |
|
|
199 |
} |
} |
200 |
|
|
201 |
|
private void MaskedHexBox_Load(object sender, EventArgs e) |
202 |
|
{ |
203 |
|
DoResize(); |
204 |
|
} |
205 |
|
|
206 |
|
protected override void OnResize(EventArgs e) |
207 |
|
{ |
208 |
|
base.OnResize(e); |
209 |
|
DoResize(); |
210 |
|
} |
211 |
|
|
212 |
|
private void DoResize() |
213 |
|
{ |
214 |
|
string t = string.Empty; |
215 |
|
switch (this.TextLength) |
216 |
|
{ |
217 |
|
case 2: |
218 |
|
t = string.Format("0x{0:x2}", this.Value); |
219 |
|
break; |
220 |
|
case 4: |
221 |
|
t = string.Format("0x{0:x4}", this.Value); |
222 |
|
break; |
223 |
|
case 8: goto default; |
224 |
|
case 16: |
225 |
|
t = string.Format("0x{0:x16}", this.Value); |
226 |
|
break; |
227 |
|
default: |
228 |
|
t = string.Format("0x{0:x8}", this.Value); |
229 |
|
break; |
230 |
|
} |
231 |
|
|
232 |
|
|
233 |
|
|
234 |
|
using (Graphics g = this.CreateGraphics()) |
235 |
|
{ |
236 |
|
var size = g.MeasureString(t, this.Font).ToSize(); |
237 |
|
int width = size.Width; // the minimum size of the textbox |
238 |
|
double growth = (double)size.Width / 0.5; |
239 |
|
|
240 |
|
switch (this.TextLength) |
241 |
|
{ |
242 |
|
case 2: this.Width = (int)growth + 25; break; |
243 |
|
case 4: this.Width = (int)growth + 15; break; |
244 |
|
case 8: goto default; |
245 |
|
case 16: this.Width = (int)growth - 70; break; |
246 |
|
default: |
247 |
|
this.Width = (int)growth - 15; break; |
248 |
|
} |
249 |
|
|
250 |
|
|
251 |
|
} |
252 |
|
} |
253 |
} |
} |
254 |
|
|
255 |
#region MaskedHexBox Extensions |
#region MaskedHexBox Extensions |