PIbd-32.-Gerimovich-I.M.-COP/Library15Gerimovich/InputRealNumber.cs
platoff aeeee 7d7ce6cb91 try2
2024-09-17 12:41:48 +04:00

92 lines
2.4 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();
}
/// <summary>
/// Установка и получение введенного значения
/// </summary>
public int? IntValue
{
get
{
if (IsNullCheckBox.Checked)
{
return null;
}
if (string.IsNullOrEmpty(MainTextBox.Text))
{
throw new EmptyValueException();
}
int ParsedInt;
if (Int32.TryParse(MainTextBox.Text, out ParsedInt))
{
return ParsedInt;
}
else
{
throw new MalformedRealException();
}
}
set
{
SetNullState(value is null, true);
if (value is not null)
{
MainTextBox.Text = value.ToString();
}
}
}
/// <summary>
/// Событие, вызываемое при смене значения (либо при смене CheckBox)
/// </summary>
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);
}
}
}