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 Novell, Inc.
23 // Miguel de Icaza (miguel@novell.com).
28 using System.ComponentModel;
30 namespace System.Windows.Forms {
31 [DefaultEvent ("ValueChanged")]
32 [DefaultProperty ("Value")]
33 public class NumericUpDown : UpDownBase, ISupportInitialize {
37 Decimal increment = 1m;
38 bool thousand = false;
43 public NumericUpDown () : base ()
49 #region ISupportInitialize methods
52 // These are used for batch updates of properties:
53 // checking of values should be disabled during this
54 // time, assuming that the caller will set us up
57 // See: http://www.vkarlsen.no/articles/02_isupportinitialize/ISupportInitialize.pdf
60 public void BeginInit ()
65 public void EndInit ()
68 if (updown_value < min)
70 if (updown_value > max)
77 public override void DownButton ()
82 if (updown_value-increment >= min){
83 updown_value -= increment;
88 public override void UpButton ()
93 if (updown_value + increment <= max){
94 updown_value += increment;
99 public override void UpdateEditText ()
105 Text = updown_value.ToString (format);
108 public void ParseEditText ()
113 res = decimal.Parse (Text);
125 protected override void ValidateEditText ()
132 public decimal Value {
139 updown_value = value;
143 if (value < min || value > max)
144 throw new ArgumentOutOfRangeException (
145 String.Format ("Value {0} not within boundaries [{1}, {2}]", value, min, max));
147 updown_value = value;
148 Text = updown_value.ToString (format);
152 public decimal Increment {
162 [RefreshProperties(RefreshProperties.All)]
163 public decimal Maximum {
178 [RefreshProperties(RefreshProperties.All)]
179 public decimal Minimum {
195 [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
196 [EditorBrowsable(EditorBrowsableState.Never)]
198 public override string Text {
211 format = "N" + decimal_places.ToString ();
213 format = "F" + decimal_places.ToString ();
216 public bool ThousandsSeparator {
228 public int DecimalPlaces {
230 return decimal_places;
234 decimal_places = value;
240 #region Overrides for Control hooks
242 protected override void OnLostFocus (EventArgs e)
244 base.OnLostFocus (e);
251 protected override void OnMouseDown (MouseEventArgs e)
256 protected override void OnMouseUp (MouseEventArgs e)
261 protected override void OnTextBoxKeyPress (object source, KeyPressEventArgs e)
263 Console.WriteLine ("OnTextBoxKeyPress: " + e);
265 // TODO: Apparently we must validate digit input here
270 public override string ToString ()
272 return String.Format ("{0} Min={0} Max={1}", base.ToString (), min, max);