use int64 instead of int32 for CRC/HEC/FEC values (they can get quite large eq FEC...
[GitHub/Stricted/SpeedportHybridControl.git] / SpeedportHybridControl.Implementations / NumberTextBox.cs
CommitLineData
b95b5c9b
S
1using System;
2using System.Windows;
3using System.Windows.Input;
4using System.Windows.Controls;
5
6namespace SpeedportHybridControl.Implementations
7{
8 // https://social.msdn.microsoft.com/Forums/vstudio/en-US/fb0745f0-6c26-4a9e-b792-3f7e8484b243/allow-only-number-in-textbox?forum=wpf#8acf76ea-7667-4763-a837-473ea2b03fa5
9 public class NumberTextBox : TextBox
10 {
11 static NumberTextBox()
12 {
13 EventManager.RegisterClassHandler(
14 typeof(NumberTextBox),
15 DataObject.PastingEvent,
16 (DataObjectPastingEventHandler)((sender, e) =>
17 {
18 if (!IsDataValid(e.DataObject))
19 {
20 DataObject data = new DataObject();
21 data.SetText(String.Empty);
22 e.DataObject = data;
23 e.Handled = false;
24 }
25 }));
26 }
27
28 protected override void OnDrop(DragEventArgs e)
29 {
30 e.Handled = !IsDataValid(e.Data);
31 base.OnDrop(e);
32 }
33
34 protected override void OnDragOver(DragEventArgs e)
35 {
36 if (!IsDataValid(e.Data))
37 {
38 e.Handled = true;
39 e.Effects = DragDropEffects.None;
40 }
41
42 base.OnDragEnter(e);
43 }
44
45 private static Boolean IsDataValid(IDataObject data)
46 {
47 Boolean isValid = false;
48 if (data != null)
49 {
50 String text = data.GetData(DataFormats.Text) as String;
51 if (!String.IsNullOrEmpty(text == null ? null : text.Trim()))
52 {
53 Int32 result = -1;
54 if (Int32.TryParse(text, out result))
55 {
56 if (result > 0)
57 {
58 isValid = true;
59 }
60 }
61 }
62 }
63
64 return isValid;
65 }
66
67 protected override void OnKeyDown(KeyEventArgs e)
68 {
69 if (e.Key < Key.D0 || e.Key > Key.D9)
70 {
71 if (e.Key < Key.NumPad0 || e.Key > Key.NumPad9)
72 {
73 if (e.Key != Key.Back)
74 {
75 e.Handled = true;
76 }
77 }
78 }
79 }
80 }
81}