COP_Petrushin_PIbd-32/Library14Petrushin/TextBoxRange.cs

68 lines
2.0 KiB
C#
Raw Normal View History

2024-09-15 23:58:52 +04:00
using Library14Petrushin.Exeptions;
using System;
2024-09-15 23:31:11 +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 Library14Petrushin
{
2024-09-15 23:53:12 +04:00
public partial class TextBoxRange : UserControl
2024-09-15 23:31:11 +04:00
{
2024-09-17 12:40:52 +04:00
public int ?MinLength { get; set; }
public int ?MaxLength { get; set; }
2024-09-15 23:58:52 +04:00
public event EventHandler? ValueChanged;
public string InputValue
{
get
{
2024-09-17 12:40:52 +04:00
if (MinLength == null && MaxLength == null)
2024-09-15 23:58:52 +04:00
{
throw new RangeException("Диапазон не задан.");
}
if (textBox1.Text.Length < MinLength || textBox1.Text.Length > MaxLength)
{
throw new RangeException("Введенное значение не входит в диапазон.");
}
return textBox1.Text;
}
set
{
2024-09-17 12:40:52 +04:00
if (MinLength != null && MaxLength != null)
2024-09-15 23:58:52 +04:00
{
if (value.Length >= MinLength && value.Length <= MaxLength)
{
textBox1.Text = value;
ValueChanged?.Invoke(this, EventArgs.Empty);
}
}
}
}
2024-09-15 23:53:12 +04:00
public TextBoxRange()
2024-09-15 23:31:11 +04:00
{
InitializeComponent();
2024-09-15 23:58:52 +04:00
textBox1.TextChanged += TextBox1_TextChanged;
2024-09-15 23:31:11 +04:00
}
2024-09-15 23:58:52 +04:00
private void TextBox1_TextChanged(object sender, EventArgs e)
{
2024-09-17 12:40:52 +04:00
ValueChanged?.Invoke(this, e);
if (MinLength != null && MaxLength != null)
2024-09-15 23:58:52 +04:00
{
if (textBox1.Text.Length < MinLength || textBox1.Text.Length > MaxLength)
{
2024-09-17 12:40:52 +04:00
textBox1.ForeColor = Color.Red;
2024-09-15 23:58:52 +04:00
}
else
{
2024-09-17 12:40:52 +04:00
textBox1.ForeColor = Color.Black;
2024-09-15 23:58:52 +04:00
}
}
}
2024-09-15 23:31:11 +04:00
}
}