PIbd-31_Razubaev_S_M_COP-10/WinFormsLibrary/EmailTextBox.cs

79 lines
1.4 KiB
C#
Raw Normal View History

using System.Text.RegularExpressions;
namespace WinFormsLibrary
{
public partial class EmailTextBox : UserControl
{
public EmailTextBox()
{
InitializeComponent();
}
private string _pattern;
private string example = "hello@mail.ru";
private EventHandler _onValueChanged;
public string Pattern
{
get { return _pattern; }
set { _pattern = value; }
}
public string Error
{
get; private set;
}
public void setExample(string str)
{
Regex rg = new Regex(Pattern);
bool address = rg.IsMatch(str);
if (address)
{
example = str;
}
}
public string TextBoxValue
{
get
{
Regex regex = new Regex(Pattern);
if (regex.IsMatch(textBoxEmail.Text))
{
return textBoxEmail.Text;
}
else
{
Error = "Некорректный ввод";
return null;
}
}
set
{
Regex regex = new Regex(Pattern);
if (regex.IsMatch(value))
{
textBoxEmail.Text = value;
}
else
{
Error = "Некорректный ввод";
}
}
}
private void textBoxEmail_MouseEnter(object sender, EventArgs e)
{
toolTipEmail.Show(example, textBoxEmail, 30, -20);
}
public event EventHandler ValueChanged
{
add { _onValueChanged += value; }
remove { _onValueChanged -= value; }
}
private void textBoxEmail_TextChanged(object sender, EventArgs e)
{
_onValueChanged?.Invoke(sender, e);
}
}
}