Now fetching some of our Sound Graph DLL function pointers.
2 using System.ComponentModel;
3 using System.Windows.Forms;
4 using System.Globalization;
10 /// Restricts the entry of characters to digits, the negative sign,
11 /// the decimal point, and editing keystrokes (backspace).
12 /// It does not handle the AltGr key so any keys that can be created in any
13 /// combination with AltGr these are not filtered
15 public class NumericTextBox : TextBox
17 private const int WM_PASTE = 0x302;
18 private NumberStyles numberStyle = NumberStyles.AllowDecimalPoint | NumberStyles.AllowLeadingSign;
21 /// Restricts the entry of characters to digits, the negative sign,
22 /// the decimal point, and editing keystrokes (backspace).
23 /// It does not handle the AltGr key
25 /// <param name="e"></param>
26 protected override void OnKeyPress(KeyPressEventArgs e)
30 e.Handled = invalidNumeric(e.KeyChar);
35 /// Main method for verifying allowed keypresses.
36 /// This does not catch cut paste copy ... operations.
38 /// <param name="key"></param>
39 /// <returns></returns>
40 private bool invalidNumeric(char key)
44 NumberFormatInfo numberFormatInfo = CultureInfo.CurrentCulture.NumberFormat;
45 string decimalSeparator = numberFormatInfo.NumberDecimalSeparator;
46 string negativeSign = numberFormatInfo.NegativeSign;
48 string keyString = key.ToString();
50 if (Char.IsDigit(key))
54 else if (AllowDecimalSeparator && keyString.Equals(decimalSeparator))
56 if (Text.IndexOf(decimalSeparator) >= 0)
61 else if (AllowNegativeSign && keyString.Equals(negativeSign))
63 if (Text.IndexOf(negativeSign) >= 0)
70 // Backspace key is OK
72 else if ((ModifierKeys & (Keys.Control)) != 0)
74 // Let the edit control handle control and alt key combinations
78 // Swallow this invalid key and beep
86 /// Method invoked when Windows sends a message.
88 /// <param name="m">Message from Windows.</param>
90 /// This is over-ridden so that the user can not use
91 /// cut or paste operations to bypass the TextChanging event.
92 /// This catches ContextMenu Paste, Shift+Insert, Ctrl+V,
93 /// While it is generally frowned upon to override WndProc, no
94 /// other simple mechanism was apparent to simultaneously and
95 /// transparently intercept so many different operations.
97 protected override void WndProc(ref Message m)
99 // Switch to handle message...
104 // Get clipboard object to paste
105 IDataObject clipboardData = Clipboard.GetDataObject();
107 // Get text from clipboard data
108 string pasteText = (string)clipboardData.GetData(
109 DataFormats.UnicodeText);
111 // Get the number of characters to replace
112 int selectionLength = SelectionLength;
114 // If no replacement or insertion, we are done
115 if (pasteText.Length == 0)
119 else if (selectionLength != 0)
121 base.Text = base.Text.Remove(SelectionStart, selectionLength);
124 bool containsInvalidChars = false;
125 foreach (char c in pasteText)
127 if (containsInvalidChars)
131 else if (invalidNumeric(c))
133 containsInvalidChars = true;
137 if (!containsInvalidChars)
139 base.Text = base.Text.Insert(SelectionStart, pasteText);
155 Int32.TryParse(this.Text, numberStyle, CultureInfo.CurrentCulture.NumberFormat, out intValue);
160 public decimal DecimalValue
164 decimal decimalValue;
165 Decimal.TryParse(this.Text, numberStyle, CultureInfo.CurrentCulture.NumberFormat, out decimalValue);
171 private bool allowNegativeSign;
173 public bool AllowNegativeSign
175 get { return allowNegativeSign; }
176 set { allowNegativeSign = value; }
179 private bool allowDecimalSeparator;
181 public bool AllowDecimalSeparator
183 get { return allowDecimalSeparator; }
184 set { allowDecimalSeparator = value; }