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 |
using System.Collections; |
10 |
|
11 |
namespace System.Windows.Forms |
12 |
{ |
13 |
public partial class NumericBoxBase : UserControl |
14 |
{ |
15 |
public NumericBoxBase() |
16 |
{ |
17 |
InitializeComponent(); |
18 |
this.Value = (0).ToString(); |
19 |
this.ReadOnly = false; |
20 |
} |
21 |
|
22 |
[Browsable(true)] |
23 |
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)] |
24 |
[EditorBrowsable(EditorBrowsableState.Always)] |
25 |
public override bool AutoSize { get { return base.AutoSize; } set { if (base.AutoSize != value) { base.AutoSize = value; OnAutoSizeChanged(new EventArgs()); } } } |
26 |
|
27 |
protected override void OnAutoSizeChanged(EventArgs e) |
28 |
{ |
29 |
base.OnAutoSizeChanged(e); |
30 |
layout_panel.AutoSize = base.AutoSize; |
31 |
} |
32 |
|
33 |
new public Font Font |
34 |
{ |
35 |
get { return base.Font; } |
36 |
set |
37 |
{ |
38 |
base.Font = value; |
39 |
txtValueBox.Font = value; |
40 |
btnCopy.Font = value; |
41 |
btnPaste.Font = value; |
42 |
} |
43 |
} |
44 |
private string _Value; |
45 |
[Browsable(true)] |
46 |
public string Value |
47 |
{ |
48 |
get { return _Value; } |
49 |
set { _Value = value; this.Text = value; DoResize(); } |
50 |
} |
51 |
|
52 |
private bool _ReadOnly; |
53 |
public bool ReadOnly |
54 |
{ |
55 |
get { return _ReadOnly; } |
56 |
set { _ReadOnly = value; if (_ReadOnly) { txtValueBox.Enabled = false; } else { txtValueBox.Enabled = true; } } |
57 |
} |
58 |
public override string Text |
59 |
{ |
60 |
get { return txtValueBox.Text; } |
61 |
set { txtValueBox.Text = value; } |
62 |
} |
63 |
|
64 |
private void btnCopy_Click(object sender, EventArgs e) { Clipboard.SetData(DataFormats.Text, this.Text); } |
65 |
private void btnPaste_Click(object sender, EventArgs e) { if (this.ReadOnly) return; this.Text = (string)Clipboard.GetData(DataFormats.Text); } |
66 |
|
67 |
|
68 |
protected void CreateStandardKeys(List<Keys> ValidKeys) |
69 |
{ |
70 |
// these are mostly editing/navigation keys |
71 |
ValidKeys.Add(Keys.Delete); |
72 |
ValidKeys.Add(Keys.Back); |
73 |
ValidKeys.Add(Keys.Up); |
74 |
ValidKeys.Add(Keys.Down); |
75 |
ValidKeys.Add(Keys.Left); |
76 |
ValidKeys.Add(Keys.Right); |
77 |
ValidKeys.Add(Keys.Home); |
78 |
ValidKeys.Add(Keys.End); |
79 |
} |
80 |
protected virtual void CreateValidKeys(List<Keys> ValidKeys) |
81 |
{ |
82 |
//ValidKeys.AddRange(Enum.GetValues(typeof(Keys)).Cast<Keys>().Where(x => x >= Keys.D0 && x <= Keys.D9)); |
83 |
// ValidKeys.AddRange(Enum.GetValues(typeof(Keys)).Cast<Keys>().Where(x => x >= Keys.NumPad0 && x <= Keys.NumPad9)); |
84 |
} |
85 |
protected List<Keys> GetValidKeys() |
86 |
{ |
87 |
List<Keys> ValidKeys = new List<Keys>(); |
88 |
CreateStandardKeys(ValidKeys); |
89 |
CreateValidKeys(ValidKeys); |
90 |
return ValidKeys; |
91 |
} |
92 |
private void txtValueBox_KeyDown(object sender, KeyEventArgs e) |
93 |
{ |
94 |
List<Keys> ValidKeys = this.GetValidKeys(); |
95 |
if (ValidKeys.Contains(e.KeyCode)) |
96 |
{ |
97 |
if (txtValueBox.Text.Contains(".") && (e.KeyCode == Keys.Decimal || e.KeyCode == Keys.OemPeriod)) |
98 |
{ |
99 |
e.SuppressKeyPress = true; |
100 |
} |
101 |
return; |
102 |
} |
103 |
if(!e.Control && !e.Alt) |
104 |
{ |
105 |
e.SuppressKeyPress = true; |
106 |
} |
107 |
} |
108 |
|
109 |
private void txtValueBox_TextChanged(object sender, EventArgs e) |
110 |
{ |
111 |
this.Value = this.Text; |
112 |
} |
113 |
private void DoResize() |
114 |
{ |
115 |
using (Graphics g = this.CreateGraphics()) |
116 |
{ |
117 |
string t = txtValueBox.Text; |
118 |
var size = g.MeasureString(t, this.Font).ToSize(); |
119 |
int width = size.Width; // the minimum size of the textbox |
120 |
txtValueBox.Width = width + 5; |
121 |
this.Width = width; |
122 |
} |
123 |
} |
124 |
} |
125 |
|
126 |
#region NumericBox Extensions |
127 |
public static class NumericBoxExtensions |
128 |
{ |
129 |
public static byte ToByte(this NumericBoxBase val) |
130 |
{ |
131 |
try |
132 |
{ |
133 |
return Convert.ToByte(val.Value); |
134 |
} |
135 |
catch { return 0; } |
136 |
} |
137 |
public static sbyte ToSByte(this NumericBoxBase val) |
138 |
{ |
139 |
try |
140 |
{ |
141 |
return Convert.ToSByte(val.Value); |
142 |
} |
143 |
catch { return 0; } |
144 |
} |
145 |
public static Int16 ToInt16(this NumericBoxBase val) |
146 |
{ |
147 |
try |
148 |
{ |
149 |
return Convert.ToInt16(val.Value); |
150 |
} |
151 |
catch { return 0; } |
152 |
} |
153 |
public static Int32 ToInt32(this NumericBoxBase val) |
154 |
{ |
155 |
try |
156 |
{ |
157 |
return Convert.ToInt32(val.Value); |
158 |
} |
159 |
catch { return 0; } |
160 |
} |
161 |
public static Int64 ToInt64(this NumericBoxBase val) |
162 |
{ |
163 |
try |
164 |
{ |
165 |
return Convert.ToInt64(val.Value); |
166 |
} |
167 |
catch { return 0; } |
168 |
} |
169 |
public static UInt16 ToUInt16(this NumericBoxBase val) |
170 |
{ |
171 |
try |
172 |
{ |
173 |
return Convert.ToUInt16(val.Value); |
174 |
} |
175 |
catch { return 0; } |
176 |
} |
177 |
public static UInt32 ToUInt32(this NumericBoxBase val) |
178 |
{ |
179 |
try |
180 |
{ |
181 |
return Convert.ToUInt32(val.Value); |
182 |
} |
183 |
catch { return 0; } |
184 |
} |
185 |
public static UInt64 ToUInt64(this NumericBoxBase val) |
186 |
{ |
187 |
try |
188 |
{ |
189 |
return Convert.ToUInt64(val.Value); |
190 |
} |
191 |
catch { return 0; } |
192 |
} |
193 |
public static Single ToSingle(this NumericBoxBase val) |
194 |
{ |
195 |
try |
196 |
{ |
197 |
return Convert.ToSingle(val.Value); |
198 |
} |
199 |
catch { return 0; } |
200 |
} |
201 |
public static double ToDouble(this NumericBoxBase val) |
202 |
{ |
203 |
try |
204 |
{ |
205 |
return Convert.ToDouble(val.Value); |
206 |
} |
207 |
catch { return 0; } |
208 |
} |
209 |
public static decimal ToDecimal(this NumericBoxBase val) |
210 |
{ |
211 |
try |
212 |
{ |
213 |
return Convert.ToDecimal(val.Value); |
214 |
} |
215 |
catch { return 0; } |
216 |
} |
217 |
public static string ToString(this NumericBoxBase val) |
218 |
{ |
219 |
try |
220 |
{ |
221 |
return Convert.ToString(val.Value); |
222 |
} |
223 |
catch { return "0"; } |
224 |
} |
225 |
} |
226 |
#endregion |
227 |
} |