1 |
using System; |
2 |
using System.Collections.Generic; |
3 |
using System.ComponentModel; |
4 |
using System.Drawing; |
5 |
using System.Data; |
6 |
using System.Linq; |
7 |
using System.Text; |
8 |
using System.Windows.Forms; |
9 |
|
10 |
namespace System.Windows.Forms |
11 |
{ |
12 |
public partial class NumericBoxBase : UserControl |
13 |
{ |
14 |
public NumericBoxBase() |
15 |
{ |
16 |
InitializeComponent(); |
17 |
this.Value = 0; |
18 |
this.ReadOnly = false; |
19 |
} |
20 |
|
21 |
private ValueType _Value; |
22 |
[Browsable(true)] |
23 |
public ValueType Value |
24 |
{ |
25 |
get { return _Value; } |
26 |
set { _Value = value; this.Text = value.ToString(); } |
27 |
} |
28 |
|
29 |
private bool _ReadOnly; |
30 |
public bool ReadOnly |
31 |
{ |
32 |
get { return _ReadOnly; } |
33 |
set { _ReadOnly = value; if (_ReadOnly) { txtValueBox.Enabled = false; } else { txtValueBox.Enabled = true; } } |
34 |
} |
35 |
public override string Text |
36 |
{ |
37 |
get { return txtValueBox.Text; } |
38 |
set { txtValueBox.Text = value; } |
39 |
} |
40 |
|
41 |
private void btnCopy_Click(object sender, EventArgs e) { Clipboard.SetData(DataFormats.Text, this.Text); } |
42 |
private void btnPaste_Click(object sender, EventArgs e) { if (this.ReadOnly) return; this.Text = (string)Clipboard.GetData(DataFormats.Text); } |
43 |
} |
44 |
|
45 |
#region NumericBox Extensions |
46 |
public static class NumericBoxExtensions |
47 |
{ |
48 |
public static byte ToByte(this NumericBoxBase val) |
49 |
{ |
50 |
try |
51 |
{ |
52 |
return Convert.ToByte(val.Value); |
53 |
} |
54 |
catch { return 0; } |
55 |
} |
56 |
public static sbyte ToSByte(this NumericBoxBase val) |
57 |
{ |
58 |
try |
59 |
{ |
60 |
return Convert.ToSByte(val.Value); |
61 |
} |
62 |
catch { return 0; } |
63 |
} |
64 |
public static Int16 ToInt16(this NumericBoxBase val) |
65 |
{ |
66 |
try |
67 |
{ |
68 |
return Convert.ToInt16(val.Value); |
69 |
} |
70 |
catch { return 0; } |
71 |
} |
72 |
public static Int32 ToInt32(this NumericBoxBase val) |
73 |
{ |
74 |
try |
75 |
{ |
76 |
return Convert.ToInt32(val.Value); |
77 |
} |
78 |
catch { return 0; } |
79 |
} |
80 |
public static Int64 ToInt64(this NumericBoxBase val) |
81 |
{ |
82 |
try |
83 |
{ |
84 |
return Convert.ToInt64(val.Value); |
85 |
} |
86 |
catch { return 0; } |
87 |
} |
88 |
public static UInt16 ToUInt16(this NumericBoxBase val) |
89 |
{ |
90 |
try |
91 |
{ |
92 |
return Convert.ToUInt16(val.Value); |
93 |
} |
94 |
catch { return 0; } |
95 |
} |
96 |
public static UInt32 ToUInt32(this NumericBoxBase val) |
97 |
{ |
98 |
try |
99 |
{ |
100 |
return Convert.ToUInt32(val.Value); |
101 |
} |
102 |
catch { return 0; } |
103 |
} |
104 |
public static UInt64 ToUInt64(this NumericBoxBase val) |
105 |
{ |
106 |
try |
107 |
{ |
108 |
return Convert.ToUInt64(val.Value); |
109 |
} |
110 |
catch { return 0; } |
111 |
} |
112 |
public static Single ToSingle(this NumericBoxBase val) |
113 |
{ |
114 |
try |
115 |
{ |
116 |
return Convert.ToSingle(val.Value); |
117 |
} |
118 |
catch { return 0; } |
119 |
} |
120 |
public static double ToDouble(this NumericBoxBase val) |
121 |
{ |
122 |
try |
123 |
{ |
124 |
return Convert.ToDouble(val.Value); |
125 |
} |
126 |
catch { return 0; } |
127 |
} |
128 |
public static decimal ToDecimal(this NumericBoxBase val) |
129 |
{ |
130 |
try |
131 |
{ |
132 |
return Convert.ToDecimal(val.Value); |
133 |
} |
134 |
catch { return 0; } |
135 |
} |
136 |
public static string ToString(this NumericBoxBase val) |
137 |
{ |
138 |
try |
139 |
{ |
140 |
return Convert.ToString(val.Value); |
141 |
} |
142 |
catch { return "0"; } |
143 |
} |
144 |
} |
145 |
#endregion |
146 |
} |