// Peter Bartok <pbartok@novell.com>
//
-// COMPLETE
-
using System;
using System.Collections;
using System.ComponentModel;
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;
maximum = 100M;
minimum = 0.0M;
thousands_separator = false;
+
+ Text = "0";
}
#endregion // Public Constructors
[Bindable(true)]
public decimal Value {
get {
+ if (UserEdit)
+ dvalue = ParseEditText (Text);
return dvalue;
}
if (value != dvalue) {
dvalue = value;
OnValueChanged(EventArgs.Empty);
- UpdateEditText();
+ Text = UpdateEditText (dvalue);
}
}
}
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)
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
}
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
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);
}
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('-');
}
}
- 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
}
}