110 lines
3.2 KiB
C#
110 lines
3.2 KiB
C#
using System.Windows;
|
|
using System.Windows.Controls;
|
|
using System.Windows.Input;
|
|
|
|
namespace TabletTester2025.Views
|
|
{
|
|
public partial class NumericKeypadWindow : Window
|
|
{
|
|
private readonly bool _allowDecimal;
|
|
private readonly bool _allowNegative;
|
|
|
|
public NumericKeypadWindow(string initialValue, bool allowDecimal, bool allowNegative)
|
|
{
|
|
InitializeComponent();
|
|
_allowDecimal = allowDecimal;
|
|
_allowNegative = allowNegative;
|
|
DisplayBox.Text = initialValue?.Trim() ?? "";
|
|
DecimalButton.IsEnabled = allowDecimal;
|
|
SignButton.IsEnabled = allowNegative;
|
|
}
|
|
|
|
public string ResultText => DisplayBox.Text;
|
|
|
|
private void DigitButton_Click(object sender, RoutedEventArgs e)
|
|
{
|
|
if (sender is Button button && button.Content is string value)
|
|
DisplayBox.Text += value;
|
|
}
|
|
|
|
private void DecimalButton_Click(object sender, RoutedEventArgs e)
|
|
{
|
|
if (!_allowDecimal || DisplayBox.Text.Contains('.'))
|
|
return;
|
|
|
|
DisplayBox.Text = string.IsNullOrWhiteSpace(DisplayBox.Text)
|
|
? "0."
|
|
: DisplayBox.Text + ".";
|
|
}
|
|
|
|
private void SignButton_Click(object sender, RoutedEventArgs e)
|
|
{
|
|
if (!_allowNegative)
|
|
return;
|
|
|
|
DisplayBox.Text = DisplayBox.Text.StartsWith("-")
|
|
? DisplayBox.Text[1..]
|
|
: "-" + DisplayBox.Text;
|
|
}
|
|
|
|
private void BackspaceButton_Click(object sender, RoutedEventArgs e)
|
|
{
|
|
if (DisplayBox.Text.Length > 0)
|
|
DisplayBox.Text = DisplayBox.Text[..^1];
|
|
}
|
|
|
|
private void ClearButton_Click(object sender, RoutedEventArgs e)
|
|
{
|
|
DisplayBox.Clear();
|
|
}
|
|
|
|
private void OkButton_Click(object sender, RoutedEventArgs e)
|
|
{
|
|
DialogResult = true;
|
|
}
|
|
|
|
private void CancelButton_Click(object sender, RoutedEventArgs e)
|
|
{
|
|
DialogResult = false;
|
|
}
|
|
|
|
private void Window_KeyDown(object sender, KeyEventArgs e)
|
|
{
|
|
if (e.Key >= Key.D0 && e.Key <= Key.D9)
|
|
{
|
|
DisplayBox.Text += (e.Key - Key.D0).ToString();
|
|
e.Handled = true;
|
|
return;
|
|
}
|
|
|
|
if (e.Key >= Key.NumPad0 && e.Key <= Key.NumPad9)
|
|
{
|
|
DisplayBox.Text += (e.Key - Key.NumPad0).ToString();
|
|
e.Handled = true;
|
|
return;
|
|
}
|
|
|
|
if (e.Key == Key.Back)
|
|
{
|
|
BackspaceButton_Click(sender, e);
|
|
e.Handled = true;
|
|
}
|
|
else if (e.Key == Key.Enter)
|
|
{
|
|
OkButton_Click(sender, e);
|
|
e.Handled = true;
|
|
}
|
|
else if (e.Key == Key.Escape)
|
|
{
|
|
CancelButton_Click(sender, e);
|
|
e.Handled = true;
|
|
}
|
|
else if (_allowDecimal && (e.Key == Key.Decimal || e.Key == Key.OemPeriod))
|
|
{
|
|
DecimalButton_Click(sender, e);
|
|
e.Handled = true;
|
|
}
|
|
}
|
|
}
|
|
}
|