PIbd-31_Danilov_V_V_COP-30/Components/CustomTextBox.cs

65 lines
1.3 KiB
C#
Raw Normal View History

2024-10-03 21:07:20 +04:00
using Components.Exceptions;
using System;
2024-09-20 08:33:51 +04:00
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
{
2024-10-03 21:07:20 +04:00
public event EventHandler ValueChanged;
2024-09-20 08:33:51 +04:00
public CustomTextBox()
{
InitializeComponent();
2024-10-03 21:07:20 +04:00
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
{
2024-10-09 19:42:11 +04:00
checkBoxNull.Checked = !value.HasValue;
textBoxInteger.Text = value.HasValue ? value.ToString() : string.Empty;
2024-10-03 21:07:20 +04:00
}
}
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);
2024-09-20 08:33:51 +04:00
}
}
}