-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNumericTextBox.cs
More file actions
48 lines (43 loc) · 1.33 KB
/
NumericTextBox.cs
File metadata and controls
48 lines (43 loc) · 1.33 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows;
namespace RRule_Generator_Form
{
public class NumericTextBox : TextBox
{
protected override void OnPreviewTextInput(TextCompositionEventArgs e)
{
if (!int.TryParse(e.Text, out int result))
{
e.Handled = true;
}
else
{
// Check if the resulting value is within the allowed range
string newText = this.Text.Remove(this.SelectionStart, this.SelectionLength).Insert(this.SelectionStart, e.Text);
if (string.IsNullOrEmpty(newText))
{
newText = "0";
}
if (int.TryParse(newText, out int value) && (value < 0 || value > 999))
{
e.Handled = true;
}
}
base.OnPreviewTextInput(e);
}
protected override void OnLostFocus(RoutedEventArgs e)
{
if (string.IsNullOrEmpty(this.Text))
{
this.Text = "0";
}
base.OnLostFocus(e);
}
}
}