-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathNumericTextBox.cs
More file actions
107 lines (95 loc) · 3.5 KB
/
NumericTextBox.cs
File metadata and controls
107 lines (95 loc) · 3.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace SharpOcarina
{
public partial class NumericTextBox : TextBox
{
bool allowHex = false;
int digits = 4;
private bool IsCharValid(char Ch)
{
return ((Char.IsNumber(Ch) == true) ||
(allowHex == true && ((Ch >= 'A' && Ch <= 'F') || (Ch >= 'a' && Ch <= 'f'))) ||
(Ch == '\b'));
}
private bool IsStringValid(string Str)
{
foreach (char Ch in Str)
if (IsCharValid(Ch) == false)
return false;
return true;
}
protected override void OnKeyPress(KeyPressEventArgs e)
{
base.OnKeyPress(e);
///* See if the user is trying keyboard-based cut/copy/paste (Ctrl+X/C/V) and handle it */
//if (this.SelectionLength != 0 && (e.KeyChar == 0x18 || e.KeyChar == 0x3 || e.KeyChar == 0x16))
//{
// switch (e.KeyChar)
// {
// case (char)0x18:
// /* Cut */
// Clipboard.SetText(this.SelectedText);
// this.SelectedText = string.Empty;
// break;
// case (char)0x03:
// /* Copy */
// Clipboard.SetText(this.SelectedText);
// break;
// case (char)0x16:
// /* Paste */
// string PasteText = string.Empty;
// foreach (char Ch in Clipboard.GetText())
// {
// if (IsCharValid(Ch) == true)
// {
// PasteText += Char.ToUpper(Ch);
// }
// }
// this.Text.Insert(this.SelectionStart, PasteText);
// break;
// }
//}
/* Make sure the user doesn't enter more characters than allowed; also take selection length into account */
if (Text.Length >= digits + this.SelectionLength && e.KeyChar != '\b')
e.Handled = true;
/* Make sure the character is a number, if hex is allowed is between A and F, or is backspace */
if (IsCharValid(e.KeyChar) == true)
{
/* ...and now turn it into upper-case to make the look consistent */
e.KeyChar = Char.ToUpper(e.KeyChar);
}
else
{
/* Otherwise, ignore this keypress */
e.Handled = true;
}
}
public int IntValue
{
get
{
if (Text == "") return 0;
else
return Int32.Parse(Text, (allowHex == true ? System.Globalization.NumberStyles.HexNumber : System.Globalization.NumberStyles.Integer));
}
}
public decimal DecimalValue
{
get { return Decimal.Parse(Text); }
}
public bool AllowHex
{
get { return allowHex; }
set { allowHex = value; }
}
public int Digits
{
get { return digits; }
set { digits = Math.Max(1, value); }
}
}
}