PIbd-31_Afanasev.S.S._COP_5/Component/ComponentProgramming/UserTextBox.cs
2024-10-04 00:24:05 +04:00

84 lines
2.2 KiB
C#

using ComponentProgramming.Exceptions;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace ComponentProgramming
{
public partial class UserTextBox : UserControl
{
public UserTextBox()
{
InitializeComponent();
}
public int? MinValue { get; set; }
public int? MaxValue { get; set; }
private EventHandler _changeEvent;
public event EventHandler ChangeEvent
{
add
{
_changeEvent += value;
}
remove
{
_changeEvent -= value;
}
}
private void textBox_TextChanged(object sender, EventArgs e)
{
_changeEvent?.Invoke(sender, e);
if (MinValue != null && MaxValue != null)
{
if (textBox.Text.Length < MinValue || textBox.Text.Length > MaxValue)
{
textBox.ForeColor = Color.Red;
}
else
{
textBox.ForeColor = Color.Black;
}
}
}
public string TextBoxValue
{
get
{
if (MinValue == null && MaxValue == null)
{
throw new WrongRangeException("Диапазон не задан.");
}
if (textBox.Text.Length >= MinValue && textBox.Text.Length <= MaxValue)
{
return textBox.Text;
}
throw new WrongRangeException("Введенное значение не входит в диапазон.");
}
set
{
if (MinValue != null && MaxValue != null)
{
if (value.Length >= MinValue && value.Length <= MaxValue)
{
textBox.Text = value;
_changeEvent?.Invoke(this, EventArgs.Empty);
}
}
}
}
}
}