PIbd-32.-Gerimovich-I.M.-COP/Library15Gerimovich/InputRealNumber.cs
platoff aeeee 50583beb33 lab done
2024-10-01 10:21:43 +04:00

88 lines
2.3 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; }
}
// проверяем нажата ли кнопка is null
private void SetNullState(bool IsNull, bool ClearText = false)
{
IsNullCheckBox.Checked = IsNull;
MainTextBox.Enabled = !IsNull;
if (ClearText)
{
MainTextBox.Text = string.Empty;
}
}
// проверяем нажата ли кнопка is null
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);
}
}
}