PIbd-31_Danilov_V_V_COP-30/Components/Visual/CustomTextBox.cs
Владимир Данилов 4d673aa1dc В процессе
2024-10-16 14:03:22 +04:00

65 lines
1.3 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
{
checkBoxNull.Checked = !value.HasValue;
textBoxInteger.Text = value.HasValue ? value.ToString() : 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);
}
}
}