PIbd-31_Afanasev.S.S._COP_5/Component/ComponentProgramming/ControlTextBox.cs

83 lines
2.1 KiB
C#

using ComponentProgramming.Exceptions;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace ComponentProgramming
{
public partial class ControlTextBox : UserControl
{
public ControlTextBox()
{
InitializeComponent();
}
private string? _numPattern;
public string? NumPattern
{
get { return _numPattern; }
set { _numPattern = value; }
}
public int? IntegerValue
{
get
{
if (checkBoxNull.Checked)
{
return null;
}
if (string.IsNullOrWhiteSpace(textBox.Text))
{
return null;
}
if (int.TryParse(textBox.Text, out int value))
{
return value;
}
throw new FormatException("Введенное значение не является целым числом.");
}
set
{
if (value.HasValue)
{
textBox.Text = value.ToString();
checkBoxNull.Checked = false;
}
else
{
textBox.Text = string.Empty;
checkBoxNull.Checked = true;
}
}
}
private event EventHandler? _textBoxChanged;
public event EventHandler TextBoxChanged
{
add { _textBoxChanged += value; }
remove { _textBoxChanged -= value; }
}
private void textBox_TextChanged(object sender, EventArgs e)
{
_textBoxChanged?.Invoke(this, e);
}
private void checkBoxNull_CheckedChanged(object sender, EventArgs e)
{
textBox.Enabled = !checkBoxNull.Checked;
_textBoxChanged?.Invoke(this, e);
}
}
}