PIbd-32_Turner_I.A._COP_10/COP/VisualComponentsLib/MyEmailTextBox.cs

86 lines
1.6 KiB
C#
Raw Normal View History

using System;
using ToolTip = System.Windows.Forms.ToolTip;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using System.Drawing;
namespace VisualComponentsLib
{
public partial class MyEmailTextBox : UserControl
{
//Шаблон для textbox
private string pattern;
//Пример ввода
private string example = "ti@gmail.com";
public MyEmailTextBox()
{
InitializeComponent();
}
public string Pattern
{
get { return pattern; }
set { pattern = value; }
}
public string TextBoxValue
{
get
{
Regex rg = new Regex(Pattern);
bool address = rg.IsMatch(emailTextBox.Text);
if (address)
{
return emailTextBox.Text;
}
else
{
throw new Exception("Неправильный адрес");
}
}
set
{
Regex rg = new Regex(Pattern);
bool address = rg.IsMatch(value);
if (address)
{
emailTextBox.Text = value;
}
else
{
throw new Exception("Неправильный адрес");
}
}
}
public void setExample(string str)
{
Regex rg = new Regex(Pattern);
bool address = rg.IsMatch(str);
if (address)
{
example = str;
}
}
private void textBox_Enter(object sender, EventArgs e)
{
int VisibleTime = 2000; //ms
ToolTip tooltip = new ToolTip();
tooltip.Show(example, emailTextBox, 30, -20, VisibleTime);
}
private void textBox_TextChanged(object sender, EventArgs e)
{
Regex rg = new Regex(Pattern);
bool address = rg.IsMatch(emailTextBox.Text);
if (address)
{
emailTextBox.BackColor = Color.Green;
}
else
{
emailTextBox.BackColor = Color.Red;
}
}
}
}