COP_Petrushin_PIbd-32/Library14Petrushin/TextBoxRange.cs
2024-09-17 12:40:52 +04:00

68 lines
2.0 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 == null && MaxLength == null)
{
throw new RangeException("Диапазон не задан.");
}
if (textBox1.Text.Length < MinLength || textBox1.Text.Length > MaxLength)
{
throw new RangeException("Введенное значение не входит в диапазон.");
}
return textBox1.Text;
}
set
{
if (MinLength != null && MaxLength != null)
{
if (value.Length >= MinLength && value.Length <= MaxLength)
{
textBox1.Text = value;
ValueChanged?.Invoke(this, EventArgs.Empty);
}
}
}
}
public TextBoxRange()
{
InitializeComponent();
textBox1.TextChanged += TextBox1_TextChanged;
}
private void TextBox1_TextChanged(object sender, EventArgs e)
{
ValueChanged?.Invoke(this, e);
if (MinLength != null && MaxLength != null)
{
if (textBox1.Text.Length < MinLength || textBox1.Text.Length > MaxLength)
{
textBox1.ForeColor = Color.Red;
}
else
{
textBox1.ForeColor = Color.Black;
}
}
}
}
}