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