PIbd-31_Malafeev.L.S._COP_25/Cop_25/Controls/CustomTextBoxNumber.cs

112 lines
3.0 KiB
C#

using Controls.Exceptions;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Controls
{
public partial class CustomTextBoxNumber : UserControl
{
/// <summary>
/// Конструктор
/// </summary>
public CustomTextBoxNumber()
{
InitializeComponent();
}
/// <summary>
/// Шаблон вводимого значения
/// </summary>
private string? _numberPattern;
/// <summary>
/// Шаблон вводимого значения
/// </summary>
public string? NumPattern
{
get { return _numberPattern; }
set { _numberPattern = value; }
}
/// <summary>
/// Введенное значение
/// </summary>
public string? TextBoxNumber
{
get
{
if (NumPattern == null)
{
throw new CustomNumberException("Шаблон не заполнен!");
}
Regex regex = new Regex(NumPattern);
if (regex.IsMatch(textBoxNumber.Text))
{
return textBoxNumber.Text;
}
else
{
throw new CustomNumberException(textBoxNumber.Text + " не соответствует шаблону!");
}
}
set
{
Regex regex = new Regex(NumPattern!);
if (regex.IsMatch(value))
{
textBoxNumber.Text = value;
}
}
}
/// <summary>
/// Событие, вызываемое при смене значения
/// </summary>
private EventHandler _onValueChangedEvent;
/// <summary>
/// Событие, вызываемое при смене значения
/// </summary>
public event EventHandler ValueChanged
{
add
{
_onValueChangedEvent += value;
}
remove
{
_onValueChangedEvent -= value;
}
}
/// <summary>
/// Смена значения
/// </summary>
private void CustomNumberBox_NumberChanged(object sender, EventArgs e)
{
_onValueChangedEvent?.Invoke(sender, e);
}
/// <summary>
/// Выведение подсказки на экран
/// </summary>
private void textBox_Enter(object sender, EventArgs e)
{
int visibleTime = 2000;
ToolTip tooltip = new ToolTip();
tooltip.Show("+79991144333", textBoxNumber, 30, -20, visibleTime);
}
}
}