PIbd-32.-Gerimovich-I.M.-COP/Library15Gerimovich/InputRealNumber.cs
platoff aeeee a9749528c7 do
2024-09-30 12:11:02 +04:00

86 lines
2.1 KiB
C#

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Library15Gerimovich.Exceptions;
namespace Library15Gerimovich
{
public partial class InputRealNumber : UserControl
{
private event EventHandler? _valueChanged;
public InputRealNumber()
{
InitializeComponent();
}
public double? DoubleValue
{
get
{
if (IsNullCheckBox.Checked)
{
return null;
}
if (string.IsNullOrEmpty(MainTextBox.Text))
{
throw new EmptyValueException();
}
double ParsedDouble;
if (double.TryParse(MainTextBox.Text, out ParsedDouble))
{
return ParsedDouble;
}
else
{
throw new MalformedRealException();
}
}
set
{
SetNullState(value is null, true);
if (value is not null)
{
MainTextBox.Text = value.ToString();
}
}
}
public event EventHandler? ValueChanged
{
add { _valueChanged += value; }
remove { _valueChanged -= value; }
}
private void SetNullState(bool IsNull, bool ClearText = false)
{
IsNullCheckBox.Checked = IsNull;
MainTextBox.Enabled = !IsNull;
if (ClearText)
{
MainTextBox.Text = string.Empty;
}
}
private void IsNullCheckBox_CheckedChanged(object sender, EventArgs e)
{
SetNullState(IsNullCheckBox.Checked);
_valueChanged?.Invoke(this, e);
}
private void MainTextBox_TextChanged(object sender, EventArgs e)
{
_valueChanged?.Invoke(this, e);
}
}
}