From d2cbf15038dab5e0bcde5b9f0da22a20fb9a3713 Mon Sep 17 00:00:00 2001 From: GokaPek Date: Sun, 15 Sep 2024 23:58:52 +0400 Subject: [PATCH] 4 --- ExecForm/Form1.Designer.cs | 3 +- Library14Petrushin/TextBoxRange.cs | 59 +++++++++++++++++++++++++++++- 2 files changed, 59 insertions(+), 3 deletions(-) diff --git a/ExecForm/Form1.Designer.cs b/ExecForm/Form1.Designer.cs index 524c16f..78a1321 100644 --- a/ExecForm/Form1.Designer.cs +++ b/ExecForm/Form1.Designer.cs @@ -51,7 +51,7 @@ // // textBoxRange1 // - textBoxRange1.Location = new Point(233, 24); + textBoxRange1.Location = new Point(254, 21); textBoxRange1.Name = "textBoxRange1"; textBoxRange1.Size = new Size(259, 66); textBoxRange1.TabIndex = 3; @@ -73,7 +73,6 @@ private Library14Petrushin.ChooseList chooseList1; private Library14Petrushin.HierarchicalTreeView hierarchicalTreeView1; - private Library14Petrushin.RangeTextBox rangeTextBox1; private Library14Petrushin.TextBoxRange textBoxRange1; } } diff --git a/Library14Petrushin/TextBoxRange.cs b/Library14Petrushin/TextBoxRange.cs index 02d16b9..d1e6e94 100644 --- a/Library14Petrushin/TextBoxRange.cs +++ b/Library14Petrushin/TextBoxRange.cs @@ -1,4 +1,5 @@ -using System; +using Library14Petrushin.Exeptions; +using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; @@ -12,9 +13,65 @@ 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("Диапазон не задан."); + } + } + } }