86 lines
1.8 KiB
C#
86 lines
1.8 KiB
C#
using ShabComponentsLibrary.Exceptions;
|
|
|
|
namespace ShabComponentsLibrary
|
|
{
|
|
/// <summary>
|
|
/// Текстовый визуальный компонент для ввода значения определенного типа
|
|
/// </summary>
|
|
public partial class ShabInputComponent : UserControl
|
|
{
|
|
private event EventHandler? _valueChanged;
|
|
|
|
public ShabInputComponent()
|
|
{
|
|
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 MalformedIntegralException();
|
|
}
|
|
}
|
|
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);
|
|
}
|
|
}
|
|
}
|