// Peter Bartok <pbartok@novell.com>
//
-// COMPLETE
-
using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
+using System.Globalization;
using System.Runtime.InteropServices;
using System.Text;
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;
decimal_places = 0;
hexadecimal = false;
increment = 1M;
- maximum = 100.0M;
+ maximum = 100M;
minimum = 0.0M;
thousands_separator = false;
+
+ Text = "0";
}
#endregion // Public Constructors
set {
decimal_places = value;
- if (!UserEdit) {
- UpdateEditText();
- }
+ UpdateEditText();
}
}
set {
hexadecimal = value;
- if (!UserEdit) {
- UpdateEditText();
- }
+ UpdateEditText();
}
}
set {
thousands_separator = value;
-
- if (!UserEdit) {
- UpdateEditText();
- }
+ UpdateEditText();
}
}
[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)
}
public override string ToString() {
- return string.Format("{0}: value {1} in range [{2}, {3}]", base.ToString(), dvalue, minimum, maximum);
+ 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
return;
}
- string acceptable = hexadecimal ? "\b-.,0123456789ABCDEF" : "\b-.,0123456789";
+ NumberFormatInfo nfi = CultureInfo.CurrentCulture.NumberFormat;
+ string pressedKey = e.KeyChar.ToString ();
- if (acceptable.IndexOf(e.KeyChar) < 0) {
- // prevent the key from reaching the text box
- e.Handled = true;
+ if ((pressedKey != nfi.NegativeSign) && (pressedKey != nfi.NumberDecimalSeparator) &&
+ (pressedKey != nfi.NumberGroupSeparator)) {
+ string acceptable = hexadecimal ? "\b0123456789abcdefABCDEF" : "\b0123456789";
+ if (acceptable.IndexOf (e.KeyChar) == -1) {
+ // FIXME: produce beep to signal that "invalid" key was pressed
+ // prevent the key from reaching the text box
+ e.Handled = true;
+ }
}
base.OnTextBoxKeyPress(source, e);
}
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() {
+ protected void ParseEditText () {
+ Value = ParseEditText (Text);
UserEdit = false;
+ }
+
+ private decimal ParseEditText (string text) {
+ UserEdit = false;
+ decimal ret = dvalue;
try {
- string user_edit_text = Text.Replace(",", "").Trim();
+ string user_edit_text = text;
if (!hexadecimal) {
- dvalue = decimal.Parse(user_edit_text);
+ ret = decimal.Parse(user_edit_text, CultureInfo.CurrentCulture);
} else {
- dvalue = 0M;
-
- for (int i=0; i < user_edit_text.Length; i++) {
- int hex_digit = Convert.ToInt32(user_edit_text.Substring(i, 1), 16);
-
- dvalue = unchecked(dvalue * 16M + (decimal)hex_digit);
- }
+#if !NET_2_0
+ ret = Convert.ToDecimal (Convert.ToInt32 (user_edit_text, 16));
+#else
+ 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);
+ 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();
+ }
+#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
}
}