Fixed typo
[mono.git] / mcs / class / Managed.Windows.Forms / System.Windows.Forms / NumericUpDown.cs
index f29c44b5d9a240a96244986dc446f1593c70fe8d..928535f49dcb21d1e110f46879fa2eb21a45dfb0 100644 (file)
@@ -26,8 +26,6 @@
 //     Peter Bartok            <pbartok@novell.com>
 //
 
-// COMPLETE
-
 using System;
 using System.Collections;
 using System.ComponentModel;
@@ -40,6 +38,11 @@ using System.Windows.Forms;
 namespace System.Windows.Forms {
        [DefaultEvent("ValueChanged")]
        [DefaultProperty("Value")]
+#if NET_2_0
+       [DefaultBindingProperty ("Value")]
+       [ClassInterface (ClassInterfaceType.AutoDispatch)]
+       [ComVisible (true)]
+#endif
        public class NumericUpDown : UpDownBase, ISupportInitialize {
                #region Local Variables
                private int     suppress_validation;
@@ -61,6 +64,8 @@ namespace System.Windows.Forms {
                        maximum = 100M;
                        minimum = 0.0M;
                        thousands_separator = false;
+
+                       Text = "0";
                }
                #endregion      // Public Constructors
 
@@ -245,6 +250,8 @@ namespace System.Windows.Forms {
                [Bindable(true)]
                public decimal Value {
                        get {
+                               if (UserEdit)
+                                       dvalue = ParseEditText (Text);
                                return dvalue;
                        }
 
@@ -257,7 +264,7 @@ namespace System.Windows.Forms {
                                if (value != dvalue) {
                                        dvalue = value;
                                        OnValueChanged(EventArgs.Empty);
-                                       UpdateEditText();
+                                       Text = UpdateEditText (dvalue);
                                }
                        }
                }
@@ -268,14 +275,6 @@ namespace System.Windows.Forms {
                        suppress_validation++;
                }
 
-               public override void DownButton() {
-                       if (UserEdit) {
-                               ParseEditText();
-                       }
-
-                       Value = Math.Max(minimum, unchecked(dvalue - increment));
-               }
-
                public void EndInit() {
                        suppress_validation--;
                        if (suppress_validation == 0)
@@ -286,11 +285,21 @@ namespace System.Windows.Forms {
                        return string.Format("{0}, Minimum = {1}, Maximum = {2}", base.ToString(), minimum, maximum);
                }
 
+               public override void DownButton() {
+                       decimal val = dvalue;
+                       if (UserEdit) {
+                               val = ParseEditText (Text);
+                       }
+
+                       Value = Math.Max(minimum, unchecked(val - increment));
+               }
+
                public override void UpButton() {
+                       decimal val = dvalue;
                        if (UserEdit)
-                               ParseEditText();
+                               val = ParseEditText (Text);
 
-                       Value = Math.Min(maximum, unchecked(dvalue + increment));
+                       Value = Math.Min(maximum, unchecked(val + increment));
                }
                #endregion      // Public Instance Methods
 
@@ -326,46 +335,58 @@ namespace System.Windows.Forms {
                }
 
                protected virtual void OnValueChanged(EventArgs e) {
-                       if (ValueChanged != null) {
-                               ValueChanged(this, e);
-                       }
+                       EventHandler eh = (EventHandler)(Events [ValueChangedEvent]);
+                       if (eh != null)
+                               eh (this, e);
+               }
+
+               protected void ParseEditText () {
+                       Value = ParseEditText (Text);
+                       UserEdit = false;
                }
 
-               protected void ParseEditText() {
+               private decimal ParseEditText (string text) {
                        UserEdit = false;
+                       decimal ret = dvalue;
 
                        try {
-                               string user_edit_text = Text;
+                               string user_edit_text = text;
 
                                if (!hexadecimal) {
-                                       dvalue = decimal.Parse(user_edit_text, CultureInfo.CurrentCulture);
+                                       ret = decimal.Parse(user_edit_text, CultureInfo.CurrentCulture);
                                } else {
-#if NET_1_1
-                                       dvalue = Convert.ToDecimal (Convert.ToInt32 (user_edit_text, 16));
+#if !NET_2_0
+                                       ret = Convert.ToDecimal (Convert.ToInt32 (user_edit_text, 16));
 #else
-                                       dvalue = Convert.ToDecimal (Convert.ToInt32 (user_edit_text, 10));
+                                       ret = Convert.ToDecimal (Convert.ToInt32 (user_edit_text, 10));
 #endif
                                }
 
-                               if (dvalue < minimum) {
-                                       dvalue = minimum;
+                               if (ret < minimum) {
+                                       ret = minimum;
                                }
 
-                               if (dvalue > maximum) {
-                                       dvalue = maximum;
+                               if (ret > maximum) {
+                                       ret = maximum;
                                }
-
-                               OnValueChanged(EventArgs.Empty);
                        }
                        catch {}
+                       return ret;
                }
 
                protected override void UpdateEditText() {
+                       Text = UpdateEditText (ParseEditText (Text));
+               }
+
+               private string UpdateEditText (decimal val) {
                        if (suppress_validation > 0)
-                               return;
+                               return Text;
+
+                       decimal ret = val;
+                       string text = Text;
 
                        if (UserEdit)
-                               ParseEditText(); // validate user input
+                               ret = ParseEditText (text); // parse user input
 
                        if (!hexadecimal) {
                                // "N" and "F" differ only in that "N" includes commas
@@ -383,14 +404,14 @@ namespace System.Windows.Forms {
                                format_string += decimal_places;
 
                                ChangingText = true;
-                               Text = dvalue.ToString(format_string, CultureInfo.CurrentCulture);
+                               text = ret.ToString(format_string, CultureInfo.CurrentCulture);
                        }
                        else {
                                // Decimal.ToString doesn't know the "X" formatter, and
                                // converting it to an int is narrowing, so do it
                                // manually...
 
-                               int[] bits = decimal.GetBits(dvalue);
+                               int[] bits = decimal.GetBits(ret);
 
                                bool negative = (bits[3] < 0);
 
@@ -413,12 +434,12 @@ namespace System.Windows.Forms {
                                }
 
                                if (num_chars == 0) {
-                                       ChangingText = true;
-                                       Text = "0";
-                                       return;
+//                                     ChangingText = true;
+                                       text = "0";
+                                       return text;
                                }
 
-                               StringBuilder chars = new StringBuilder();
+                               StringBuilder chars = new StringBuilder ();
 
                                if (negative)
                                        chars.Append('-');
@@ -440,31 +461,39 @@ namespace System.Windows.Forms {
                                        }
                                }
 
-                               ChangingText = true;
-                               Text = chars.ToString();
+//                             ChangingText = true;
+                               text = chars.ToString();
                        }
+                       return text;
                }
 
                protected override void ValidateEditText() {
-                       ParseEditText();
-                       UpdateEditText();
+                       Value = ParseEditText (Text);
                }
 
 #if NET_2_0
-        protected override void OnLostFocus(EventArgs e) {
-            base.OnLostFocus(e);
-            if (this.UserEdit)
-                this.UpdateEditText();
-        }
+               protected override void OnLostFocus(EventArgs e) {
+                       base.OnLostFocus(e);
+                       if (this.UserEdit)
+                               this.UpdateEditText();
+               }
 #endif
                #endregion      // Protected Instance Methods
 
                #region Events
-               public event EventHandler ValueChanged;
+               static object ValueChangedEvent = new object ();
+
+               public event EventHandler ValueChanged {
+                       add { Events.AddHandler (ValueChangedEvent, value); }
+                       remove { Events.RemoveHandler (ValueChangedEvent, value); }
+               }
 
                [Browsable(false)]
                [EditorBrowsable(EditorBrowsableState.Never)]
-               public event EventHandler TextChanged;
+               public new event EventHandler TextChanged {
+                       add { base.TextChanged += value; }
+                       remove { base.TextChanged -= value; }
+               }
                #endregion      // Events
        }
 }