1 // Permission is hereby granted, free of charge, to any person obtaining
2 // a copy of this software and associated documentation files (the
3 // "Software"), to deal in the Software without restriction, including
4 // without limitation the rights to use, copy, modify, merge, publish,
5 // distribute, sublicense, and/or sell copies of the Software, and to
6 // permit persons to whom the Software is furnished to do so, subject to
7 // the following conditions:
9 // The above copyright notice and this permission notice shall be
10 // included in all copies or substantial portions of the Software.
12 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
13 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
14 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
15 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
16 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
17 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
18 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20 // Copyright (c) 2004, 2005 Novell, Inc.
23 // Miguel de Icaza (miguel@novell.com).
24 // Gonzalo Paniagua Javier (gonzalo@novell.com)
29 using System.ComponentModel;
30 using System.Globalization;
32 namespace System.Windows.Forms {
33 [DefaultEvent ("ValueChanged")]
34 [DefaultProperty ("Value")]
35 public class NumericUpDown : UpDownBase, ISupportInitialize {
39 Decimal increment = 1m;
46 public NumericUpDown ()
53 public new event EventHandler TextChanged;
54 public event EventHandler ValueChanged;
57 #region ISupportInitialize methods
60 // These are used for batch updates of properties:
61 // checking of values should be disabled during this
62 // time, assuming that the caller will set us up
65 // See: http://www.vkarlsen.no/articles/02_isupportinitialize/ISupportInitialize.pdf
68 public void BeginInit ()
73 public void EndInit ()
76 if (updown_value < min)
78 if (updown_value > max)
85 public override void DownButton ()
90 if (updown_value-increment >= min){
96 public override void UpButton ()
101 if (updown_value + increment <= max){
112 format = "N" + decimal_places.ToString ();
114 format = "F" + decimal_places.ToString ();
117 public override void UpdateEditText ()
126 if (hexadecimal) { // gotta convert to something, decimal throws with "X"
127 long val = (long) updown_value;
128 Text = val.ToString (format);
130 Text = updown_value.ToString (format);
134 public void ParseEditText ()
139 res = Int64.Parse (Text, NumberStyles.HexNumber);
141 res = decimal.Parse (Text);
156 protected override void ValidateEditText ()
163 protected override AccessibleObject CreateAccessibilityInstance ()
166 return base.CreateAccessibilityInstance ();
170 public decimal Value {
176 if (updown_value == value) {
181 updown_value = value;
185 if (value < min || value > max)
186 throw new ArgumentOutOfRangeException (
187 String.Format ("Value {0} not within boundaries [{1}, {2}]", value, min, max));
189 updown_value = value;
190 OnValueChanged (EventArgs.Empty);
195 public decimal Increment {
205 [RefreshProperties(RefreshProperties.All)]
206 public decimal Maximum {
221 [RefreshProperties(RefreshProperties.All)]
222 public decimal Minimum {
238 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
239 [EditorBrowsable(EditorBrowsableState.Never)]
241 public override string Text {
251 [DefaultValue (false)]
253 public bool ThousandsSeparator {
259 if (value == thousand)
269 public int DecimalPlaces {
271 return decimal_places;
275 if (value == decimal_places)
278 decimal_places = value;
284 [DefaultValue (false)]
285 public bool Hexadecimal {
286 get { return hexadecimal; }
288 if (value == hexadecimal)
297 #region Overrides for Control hooks
298 static bool CompareCharToString (char c, string str)
301 return (c == str [0]);
303 return (new string (c, 1) == str);
306 protected override void OnTextBoxKeyPress (object source, KeyPressEventArgs e)
308 base.OnTextBoxKeyPress (source, e);
311 char key = e.KeyChar;
312 bool is_digit = Char.IsDigit (key);
313 if (is_digit || hexadecimal) {
314 handled = !(is_digit || (key >= 'a' && key <= 'f') || (key >= 'A' && key <= 'F'));
316 NumberFormatInfo ninfo = CultureInfo.CurrentCulture.NumberFormat;
317 if (CompareCharToString (key, ninfo.NegativeSign)) {
319 } else if (CompareCharToString (key, ninfo.NumberDecimalSeparator)) {
321 } else if (CompareCharToString (key, ninfo.NumberGroupSeparator)) {
329 protected virtual void OnValueChanged (EventArgs e)
331 if (ValueChanged != null)
332 ValueChanged (this, e);
335 public override string ToString ()
337 return String.Format ("{0} Min={0} Max={1}", base.ToString (), min, max);