COP/COP_LabWork/CustomComponents/DateTextBox.cs

95 lines
1.7 KiB
C#
Raw Permalink Normal View History

2024-07-29 20:10:28 +04:00
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 CustomComponents
{
public partial class DateTextBox : UserControl
{
private string? pattern;
private string example = "Формат ввода: DD.MM.YYYY";
public DateTextBox()
{
InitializeComponent();
}
public string? Pattern
{
get { return pattern; }
set { pattern = value; }
}
public string TextBoxValue
{
get
{
Regex regex = new Regex(Pattern);
bool isValid = regex.IsMatch(textBox.Text);//2 zadanie
if (isValid)
{
return textBox.Text;
}
else
{
Error = "Неправильный ввод";
return null;
}
}
set
{
Regex regex = new Regex(Pattern);
bool isValid = regex.IsMatch(value);
if (isValid)
{
textBox.Text = value;
}
else
{
Error = "Неправильно";
}
}
}
public string Error
{
get; private set;
}
public void SetExample(string exampleStr)
{
Regex regex = new Regex(Pattern);
bool isValid = regex.IsMatch(exampleStr);
if (isValid)
{
example = exampleStr;
}
}
private void textBox_Enter(object sender, EventArgs e)
{
ToolTip tt = new ToolTip();
tt.Show(example, textBox, 30, -20, 1000);
}
private void textBox_TextChanged(object sender, EventArgs e)
{
_changeEvent?.Invoke(sender, e);
}
private EventHandler _changeEvent;
public event EventHandler ChangeEvent
{
add
{
_changeEvent += value;
}
remove
{
_changeEvent -= value;
}
}
}
}