This commit is contained in:
GokaPek 2024-09-15 23:58:52 +04:00
parent 918cbc514b
commit d2cbf15038
2 changed files with 59 additions and 3 deletions

View File

@ -51,7 +51,7 @@
// //
// textBoxRange1 // textBoxRange1
// //
textBoxRange1.Location = new Point(233, 24); textBoxRange1.Location = new Point(254, 21);
textBoxRange1.Name = "textBoxRange1"; textBoxRange1.Name = "textBoxRange1";
textBoxRange1.Size = new Size(259, 66); textBoxRange1.Size = new Size(259, 66);
textBoxRange1.TabIndex = 3; textBoxRange1.TabIndex = 3;
@ -73,7 +73,6 @@
private Library14Petrushin.ChooseList chooseList1; private Library14Petrushin.ChooseList chooseList1;
private Library14Petrushin.HierarchicalTreeView hierarchicalTreeView1; private Library14Petrushin.HierarchicalTreeView hierarchicalTreeView1;
private Library14Petrushin.RangeTextBox rangeTextBox1;
private Library14Petrushin.TextBoxRange textBoxRange1; private Library14Petrushin.TextBoxRange textBoxRange1;
} }
} }

View File

@ -1,4 +1,5 @@
using System; using Library14Petrushin.Exeptions;
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.ComponentModel; using System.ComponentModel;
using System.Data; using System.Data;
@ -12,9 +13,65 @@ namespace Library14Petrushin
{ {
public partial class TextBoxRange : UserControl 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() public TextBoxRange()
{ {
InitializeComponent(); 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("Диапазон не задан.");
}
}
} }
} }