PIbd-31_Danilov_V_V_COP-30/Components/CustomTextBox.cs
2024-10-03 21:07:20 +04:00

73 lines
1.4 KiB
C#

using Components.Exceptions;
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;
namespace Components
{
public partial class CustomTextBox : UserControl
{
public event EventHandler ValueChanged;
public CustomTextBox()
{
InitializeComponent();
checkBoxNull.CheckedChanged += CheckBoxNull_CheckedChanged;
textBoxInteger.TextChanged += TextBoxInteger_TextChanged;
}
public int? Value
{
get
{
if (checkBoxNull.Checked)
{
return null;
}
else
{
if (int.TryParse(textBoxInteger.Text, out int result))
{
return result;
}
else
{
throw new NotIntegerException("Ожидается целое число.");
}
}
}
set
{
if (value.HasValue)
{
checkBoxNull.Checked = false;
textBoxInteger.Text = value.ToString();
}
else
{
checkBoxNull.Checked = true;
textBoxInteger.Text = string.Empty;
}
}
}
private void TextBoxInteger_TextChanged(object sender, EventArgs e)
{
ValueChanged?.Invoke(this, e);
}
private void CheckBoxNull_CheckedChanged(object sender, EventArgs e)
{
textBoxInteger.Enabled = !checkBoxNull.Checked;
ValueChanged?.Invoke(this, e);
}
}
}