62 lines
1.8 KiB
C#
62 lines
1.8 KiB
C#
using RodionovLibrary.Exceptions;
|
||
using System.Text.RegularExpressions;
|
||
|
||
|
||
namespace RodionovLibrary.VisualComponents
|
||
{
|
||
public partial class TextBoxControl : UserControl
|
||
{
|
||
private string? tooltipText;
|
||
|
||
private event EventHandler? _valueChanged;
|
||
|
||
public event EventHandler ValueChanged
|
||
{
|
||
add { _valueChanged += value; }
|
||
remove { _valueChanged -= value; }
|
||
}
|
||
|
||
public string? Pattern { get; set; }
|
||
|
||
public string Value {
|
||
get
|
||
{
|
||
if (Pattern == null)
|
||
throw new NullPatternException("Не задан шаблон!");
|
||
if (!new Regex(Pattern).IsMatch(textBox.Text))
|
||
throw new NotMatchPatternException("Введенное значение не соответствует шаблону!");
|
||
return textBox.Text;
|
||
}
|
||
set
|
||
{
|
||
if (Pattern == null)
|
||
throw new NullPatternException("Не задан шаблон!");
|
||
if (!new Regex(Pattern).IsMatch(value))
|
||
throw new NotMatchPatternException("Введенное значение не соответствует шаблону!");
|
||
textBox.Text = value;
|
||
}
|
||
}
|
||
|
||
public TextBoxControl()
|
||
{
|
||
InitializeComponent();
|
||
}
|
||
|
||
public void SetTooltipText(string text)
|
||
{
|
||
tooltipText = text;
|
||
}
|
||
|
||
private void TextBox_TextChanged(object sender, EventArgs e)
|
||
{
|
||
_valueChanged?.Invoke(sender, e);
|
||
Value = textBox.Text;
|
||
}
|
||
|
||
private void TextBox_MouseEnter(object sender, EventArgs e)
|
||
{
|
||
toolTip.Show(tooltipText ?? "", textBox);
|
||
}
|
||
}
|
||
}
|