второй компонент работает

This commit is contained in:
Елена Бакальская 2024-09-14 22:23:11 +04:00
parent 89ceade732
commit 0bd7fd69e3
6 changed files with 74 additions and 20 deletions

View File

@ -29,7 +29,7 @@
private void InitializeComponent()
{
selectComponent = new UserComponentsOption19.SelectComponent();
writeComponent1 = new UserComponentsOption19.WriteComponent();
writeComponent = new UserComponentsOption19.WriteComponent();
SuspendLayout();
//
// selectComponent
@ -40,20 +40,20 @@
selectComponent.Size = new Size(189, 36);
selectComponent.TabIndex = 0;
//
// writeComponent1
// writeComponent
//
writeComponent1.Location = new Point(12, 65);
writeComponent1.Name = "writeComponent1";
writeComponent1.Size = new Size(400, 44);
writeComponent1.TabIndex = 1;
//writeComponent1.WriteText = "";
writeComponent.Location = new Point(12, 65);
writeComponent.Name = "writeComponent";
writeComponent.Size = new Size(324, 44);
writeComponent.TabIndex = 1;
writeComponent.Template = null;
//
// FormTest
//
AutoScaleDimensions = new SizeF(8F, 20F);
AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(600, 393);
Controls.Add(writeComponent1);
Controls.Add(writeComponent);
Controls.Add(selectComponent);
Name = "FormTest";
Text = "нукась протестируем...";
@ -63,6 +63,6 @@
#endregion
private UserComponentsOption19.SelectComponent selectComponent;
private UserComponentsOption19.WriteComponent writeComponent1;
private UserComponentsOption19.WriteComponent writeComponent;
}
}

View File

@ -1,3 +1,4 @@
using System.Text.RegularExpressions;
using UserComponentsOption19;
namespace TestAppForCheckComponentsWorking
@ -13,6 +14,7 @@ namespace TestAppForCheckComponentsWorking
{
InitializeComponent();
FillList(19);
SetTemplate();
}
private void SelectComponent_SelectComponentChanged(object? sender, EventArgs e)
@ -28,5 +30,13 @@ namespace TestAppForCheckComponentsWorking
}
selectComponent.FillList(testList);
}
Regex regex = new Regex(@"^[^@\s]+@[^@\s]+\.(com|net|org|gov|ru)$");
public void SetTemplate()
{
writeComponent.SetTextTooltip("email@email.ru");
}
}
}

View File

@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace UserComponentsOption19
{
internal class InvalidInputException : Exception
{
public InvalidInputException(string message) : base (message) { }
}
}

View File

@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace UserComponentsOption19
{
internal class NoTemplateException : Exception
{
public NoTemplateException(string message) : base (message) { }
}
}

View File

@ -37,6 +37,7 @@
textBox.Name = "textBox";
textBox.Size = new Size(310, 27);
textBox.TabIndex = 0;
textBox.TextChanged += textBox_TextChanged;
//
// WriteComponent
//

View File

@ -4,17 +4,20 @@ namespace UserComponentsOption19
{
public partial class WriteComponent : UserControl
{
private Regex regex = new Regex(@"^[^@\s]+@[^@\s]+\.(com|net|org|gov|ru)$^[^@\s]+@[^@\s]+\.(com|net|org|gov)$");
//= new Regex(@"^[^@\s]+@[^@\s]+\.(com|net|org|gov|ru)$^[^@\s]+@[^@\s]+\.(com|net|org|gov)$")
private Regex? regex;
private ToolTip toolTipTemplate = new ToolTip();
public event EventHandler? ChangeText;
public WriteComponent()
{
InitializeComponent();
}
public Regex Template
public Regex? Template
{
get => regex;
@ -24,29 +27,43 @@ namespace UserComponentsOption19
}
}
public void ShowToolTip()
public void SetTextTooltip(string toolTip)
{
toolTipTemplate.SetToolTip(textBox, "Пример: mail@mail.ru");
toolTipTemplate.SetToolTip(textBox, toolTip);
}
private void textBox_TextChanged(object sender, EventArgs e)
{
ChangeText?.Invoke(this, e);
}
public string WriteText
{
get
{
if(Template == null)
if (Template == null)
{
throw new Exception();
throw new NoTemplateException("Не установлен шаблон");
}
return textBox.Text;
}
set
{
MatchCollection match = regex.Matches(textBox.Text);
if(match.Count > 0)
if (Template != null)
{
textBox.Text = match[0].Value;
if (Template.IsMatch(value))
{
textBox.Text = value;
}
else
{
throw new InvalidInputException("Введенное значение не соответствует шаблону");
}
}
else
{
textBox.Text = string.Empty;
}
throw new Exception();
}
}
}