100 lines
1.6 KiB
C#

using System.Text.RegularExpressions;
using ToolTip = System.Windows.Forms.ToolTip;
namespace CustomComponents
{
public partial class DateTextBox : UserControl
{
private string? pattern;
private string example = "22.09.2023";
public DateTextBox()
{
InitializeComponent();
}
public string? Pattern
{
get { return pattern; }
set { pattern = value; }
}
public string? TextBoxValue
{
get
{
if (pattern == null)
{
Error = "pattern is null";
return null;
}
Regex regex = new Regex(Pattern);
bool isValid = regex.IsMatch(textBox.Text);
if (isValid)
{
return textBox.Text;
}
else
{
Error = "Uncorrect";
return null;
}
}
set
{
Regex regex = new Regex(Pattern);
bool isValid = regex.IsMatch(value);
if (isValid)
{
textBox.Text = value;
}
else
{
Error = "Uncorrect";
}
}
}
public string Error { get; private set; }
public void setExample(string str)
{
Regex regex = new Regex(Pattern);
bool isValid = regex.IsMatch(str);
if (isValid)
{
example = str;
}
}
private void textBox_Enter(object sender, EventArgs e)
{
int VisibleTime = 3000;
ToolTip tt = new ToolTip();
tt.Show(example, textBox, 30, -20, VisibleTime);
}
private EventHandler _explicitEvent;
public event EventHandler ExplicitEvent
{
add
{
_explicitEvent += value;
}
remove
{
_explicitEvent -= value;
}
}
private void textBox_TextChanged(object sender, EventArgs e)
{
_explicitEvent?.Invoke(sender, e);
}
}
}