70 lines
1.9 KiB
C#
70 lines
1.9 KiB
C#
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 WinFormsControlLibrary1.Exceptions;
|
||
|
||
namespace WinFormsControlLibrary1;
|
||
|
||
public partial class DateByPatternTextBox : UserControl
|
||
{
|
||
private readonly TextBox patternTextBox = new();
|
||
private readonly ToolTip _tip = new();
|
||
private string? _pattern;
|
||
private Regex? _regex;
|
||
public event EventHandler? ValueChanged;
|
||
|
||
|
||
public DateByPatternTextBox()
|
||
{
|
||
InitializeComponent();
|
||
Controls.Add(patternTextBox);
|
||
patternTextBox.TextChanged += (_, __) => ValueChanged?.Invoke(this, EventArgs.Empty);
|
||
}
|
||
|
||
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
|
||
public string? Regex
|
||
{
|
||
get => _pattern;
|
||
set
|
||
{
|
||
_pattern = value;
|
||
_regex = string.IsNullOrWhiteSpace(value) ? new Regex(value, RegexOptions.Compiled);
|
||
}
|
||
}
|
||
|
||
public void SetToolTip(string example)
|
||
{
|
||
_tip.SetToolTip(patternTextBox, example);
|
||
}
|
||
|
||
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
|
||
public string Value
|
||
{
|
||
get
|
||
{
|
||
if(_regex is null)
|
||
{
|
||
throw new ComponentValidationException("Не задан шаблон даты (Pattern).");
|
||
}
|
||
var text = patternTextBox.Text.Trim();
|
||
if (!_regex.IsMatch(text))
|
||
{
|
||
throw new ComponentValidationException("Введённая дата не соответствует заданному формату.");
|
||
}
|
||
return text;
|
||
}
|
||
set
|
||
{
|
||
patternTextBox.Text = value ?? string.Empty;
|
||
}
|
||
}
|
||
|
||
}
|