| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849 |
- using System.Windows.Controls;
- using System.Windows.Input;
- namespace MuchInfo.Chart.FormulaEdit
- {
- public class NumberTextBox : TextBox
- {
- protected override void OnKeyDown(KeyEventArgs e)
- {
- base.OnKeyDown(e);
-
- if (!IsNumber((byte)e.Key))
- {
- e.Handled = true;
- }
- }
- protected override void OnTextChanged(TextChangedEventArgs e)
- {
- if (!string.IsNullOrEmpty(this.Text))
- {
- double number = 0d;
- if (!double.TryParse(this.Text, out number))
- {
- this.Text = string.Empty;
- }
- }
- base.OnTextChanged(e);
- }
- /// <summary>
- /// 输入 是否为数字
- /// </summary>
- /// <param name="key"></param>
- /// <returns></returns>
- private bool IsNumber(byte key)
- {
- // 数字
- if (key >= 34 && key <= 43) return true;
- // 小数字键盘
- if (key >= 74 && key <= 83) return true;
- // Backspace键
- if (key == 2) return true;
- return false;
- }
- }
- }
|