97 lines
2.7 KiB
C#
97 lines
2.7 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;
|
|||
|
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
|
|||
|
|
|||
|
namespace Controls
|
|||
|
{
|
|||
|
public partial class CustomNumberBox : UserControl
|
|||
|
{
|
|||
|
/// <summary>
|
|||
|
/// Конструктор
|
|||
|
/// </summary>
|
|||
|
public CustomNumberBox()
|
|||
|
{
|
|||
|
InitializeComponent();
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Шаблон вводимого значения
|
|||
|
/// </summary>
|
|||
|
private string NumPattern = @"^\+7\d{10}$";
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Введенное значение
|
|||
|
/// </summary>
|
|||
|
public string? TextBoxValue
|
|||
|
{
|
|||
|
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;
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
throw new CustomNumberException(textBoxNumber.Text + " не соответствует шаблону!");
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Событие, вызываемое при смене значения
|
|||
|
/// </summary>
|
|||
|
private EventHandler _onValueChangedEvent;
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// Событие, вызываемое при смене значения
|
|||
|
/// </summary>
|
|||
|
public event EventHandler ValueChanged
|
|||
|
{
|
|||
|
add
|
|||
|
{
|
|||
|
_onValueChangedEvent += value;
|
|||
|
}
|
|||
|
|
|||
|
remove
|
|||
|
{
|
|||
|
_onValueChangedEvent -= value;
|
|||
|
}
|
|||
|
}
|
|||
|
/// <summary>
|
|||
|
/// Смена значения
|
|||
|
/// </summary>
|
|||
|
private void CustomNumberBox_SelectedValueChanged(object sender, EventArgs e)
|
|||
|
{
|
|||
|
_onValueChangedEvent?.Invoke(sender, e);
|
|||
|
}
|
|||
|
}
|
|||
|
}
|