78 lines
2.5 KiB
C#
78 lines
2.5 KiB
C#
using Library14Petrushin.Exeptions;
|
|
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 Library14Petrushin
|
|
{
|
|
public partial class TextBoxRange : UserControl
|
|
{
|
|
public int MinLength { get; set; }
|
|
public int MaxLength { get; set; }
|
|
public event EventHandler? ValueChanged;
|
|
public string InputValue
|
|
{
|
|
get
|
|
{
|
|
if (MinLength == 0 && MaxLength == 0)
|
|
{
|
|
throw new RangeException("Диапазон не задан.");
|
|
}
|
|
if (textBox1.Text.Length < MinLength || textBox1.Text.Length > MaxLength)
|
|
{
|
|
throw new RangeException("Введенное значение не входит в диапазон.");
|
|
}
|
|
return textBox1.Text;
|
|
}
|
|
set
|
|
{
|
|
if (MinLength != 0 && MaxLength != 0)
|
|
{
|
|
if (value.Length >= MinLength && value.Length <= MaxLength)
|
|
{
|
|
textBox1.Text = value;
|
|
ValueChanged?.Invoke(this, EventArgs.Empty);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
throw new RangeException("Диапазон не задан.");
|
|
}
|
|
}
|
|
}
|
|
public TextBoxRange()
|
|
{
|
|
InitializeComponent();
|
|
textBox1.TextChanged += TextBox1_TextChanged;
|
|
}
|
|
private void TextBox1_TextChanged(object sender, EventArgs e)
|
|
{
|
|
if (MinLength != 0 && MaxLength != 0)
|
|
{
|
|
if (textBox1.Text.Length < MinLength || textBox1.Text.Length > MaxLength)
|
|
{
|
|
// Выделение текста красным цветом, если он не входит в диапазон
|
|
textBox1.ForeColor = System.Drawing.Color.Red;
|
|
}
|
|
else
|
|
{
|
|
// Возвращение к стандартному цвету текста
|
|
textBox1.ForeColor = System.Drawing.Color.Black;
|
|
ValueChanged?.Invoke(this, e);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
throw new RangeException("Диапазон не задан.");
|
|
}
|
|
}
|
|
|
|
}
|
|
}
|