PIbd-31_Rodionov.I.A._COP_28/COP/RodionovLibrary/VisualComponents/TextBoxControl.cs

59 lines
1.6 KiB
C#
Raw Normal View History

2024-09-05 23:36:10 +04:00
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
{
2024-09-21 11:26:26 +04:00
if (string.IsNullOrEmpty(Pattern))
throw new NullPatternException("Не задан шаблон");
2024-09-05 23:36:10 +04:00
if (!new Regex(Pattern).IsMatch(textBox.Text))
2024-09-21 11:26:26 +04:00
throw new NotMatchPatternException("Введенное значение не соответствует шаблону");
2024-09-05 23:36:10 +04:00
return textBox.Text;
}
set
{
2024-09-21 11:26:26 +04:00
if (!string.IsNullOrEmpty(Pattern) && new Regex(Pattern).IsMatch(value))
textBox.Text = value;
2024-09-05 23:36:10 +04:00
}
}
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)
{
2024-09-21 11:26:26 +04:00
toolTip.Show(tooltipText ?? "", textBox, 45, 20, 3000);
2024-09-05 23:36:10 +04:00
}
}
}