KOP_PIbd-33_Volkov_N.A._Tik.../KopLab1/FormLibrary/IntegerInputControl.cs

92 lines
2.6 KiB
C#
Raw Normal View History

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 System;
using System.Windows.Forms;
using FormLibrary.Exceptions;
namespace FormLibrary
{
public partial class IntegerInputControl : UserControl
{
// Событие, вызываемое при изменении значения в TextBox
public event EventHandler? ValueChanged;
// Событие, вызываемое при изменении состояния CheckBox
public event EventHandler? CheckBoxChanged;
public IntegerInputControl()
{
InitializeComponent();
checkBoxNull.CheckedChanged += CheckBoxNull_CheckedChanged;
textBoxInput.TextChanged += TextBoxInput_TextChanged;
}
public int? Value
{
get
{
if (checkBoxNull.Checked)
{
return null;
}
else
{
if (string.IsNullOrWhiteSpace(textBoxInput.Text))
{
throw new EmptyValueException();
}
if (int.TryParse(textBoxInput.Text, out int result))
{
return result;
}
else
{
throw new InvalidValueTypeException();
}
}
}
set
{
if (value == null)
{
checkBoxNull.Checked = true;
textBoxInput.Enabled = false;
textBoxInput.Text = string.Empty;
}
else
{
checkBoxNull.Checked = false;
textBoxInput.Enabled = true;
textBoxInput.Text = value.ToString();
}
}
}
private void CheckBoxNull_CheckedChanged(object sender, EventArgs e)
{
textBoxInput.Enabled = !checkBoxNull.Checked;
if (checkBoxNull.Checked)
{
textBoxInput.Text = string.Empty;
}
CheckBoxChanged?.Invoke(this, EventArgs.Empty);
}
private void TextBoxInput_TextChanged(object sender, EventArgs e)
{
ValueChanged?.Invoke(this, EventArgs.Empty);
}
}
}