PIbd-31_Rodionov.I.A._COP_28/COP/RodionovLibrary/VisualComponents/TextBoxControl.cs
2024-09-21 11:26:26 +04:00

59 lines
1.6 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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 (string.IsNullOrEmpty(Pattern))
throw new NullPatternException("Не задан шаблон");
if (!new Regex(Pattern).IsMatch(textBox.Text))
throw new NotMatchPatternException("Введенное значение не соответствует шаблону");
return textBox.Text;
}
set
{
if (!string.IsNullOrEmpty(Pattern) && new Regex(Pattern).IsMatch(value))
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, 45, 20, 3000);
}
}
}