PIbd-33_FIrsov_Kirill_KOP/CheckedListBoxLibrary/SmartTextBox.cs

81 lines
2.1 KiB
C#
Raw Permalink Normal View History

2024-11-17 13:25:39 +04:00
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 MyUserControls
{
public partial class SmartTextBox : UserControl
{
private int _minLength;
private int _maxLength;
private string _inputText;
public int MinLength
{
get => _minLength;
set => _minLength = value;
}
public int MaxLength
{
get => _maxLength;
set => _maxLength = value;
}
public string InputText
{
get
{
if (MinLength == 0 && MaxLength == 0)
{
throw new ValueOutOfRangeException("Диапазон значений не задан.");
}
if (_inputText.Length < MinLength || _inputText.Length > MaxLength)
{
throw new ValueOutOfRangeException("Введенное значение не входит в диапазон.");
}
return _inputText;
}
set
{
if (MinLength > 0 || MaxLength > 0)
{
if (value.Length < MinLength || value.Length > MaxLength)
{
return;
}
}
_inputText = value;
textBox1.Text = value;
OnInputTextChanged(EventArgs.Empty);
}
}
public event EventHandler? InputTextChanged;
public SmartTextBox()
{
InitializeComponent();
textBox1.TextChanged += TextBox1_TextChanged;
}
private void TextBox1_TextChanged(object sender, EventArgs e)
{
InputText = textBox1.Text;
}
protected virtual void OnInputTextChanged(EventArgs e)
{
InputTextChanged?.Invoke(this, e);
}
}
}