KOP_PIbd-33_Volkov_N.A._Tik.../KopLab1/FormLibrary/IntegerInputControl.cs
2024-09-05 15:30:28 +04:00

83 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 System;
using System.Windows.Forms;
using FormLibrary.Exceptions;
namespace FormLibrary
{
public partial class IntegerInputControl : UserControl
{
public event EventHandler? ValueChanged;
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 is not null)
{
textBoxInput.Text = value.ToString();
}
checkBoxNull.Checked = value is null;
}
}
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);
}
}
}