diff --git a/UserComponentsOption19/TestAppForCheckComponentsWorking/Form1.Designer.cs b/UserComponentsOption19/TestAppForCheckComponentsWorking/Form1.Designer.cs index 026e363..0c0e452 100644 --- a/UserComponentsOption19/TestAppForCheckComponentsWorking/Form1.Designer.cs +++ b/UserComponentsOption19/TestAppForCheckComponentsWorking/Form1.Designer.cs @@ -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; } } diff --git a/UserComponentsOption19/TestAppForCheckComponentsWorking/Form1.cs b/UserComponentsOption19/TestAppForCheckComponentsWorking/Form1.cs index 5a6a4ad..9c9e80d 100644 --- a/UserComponentsOption19/TestAppForCheckComponentsWorking/Form1.cs +++ b/UserComponentsOption19/TestAppForCheckComponentsWorking/Form1.cs @@ -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"); + } + } } diff --git a/UserComponentsOption19/UserComponentsOption19/InvalidInputException.cs b/UserComponentsOption19/UserComponentsOption19/InvalidInputException.cs new file mode 100644 index 0000000..bfcb189 --- /dev/null +++ b/UserComponentsOption19/UserComponentsOption19/InvalidInputException.cs @@ -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) { } + } +} diff --git a/UserComponentsOption19/UserComponentsOption19/NoTemplateException.cs b/UserComponentsOption19/UserComponentsOption19/NoTemplateException.cs new file mode 100644 index 0000000..520ddf5 --- /dev/null +++ b/UserComponentsOption19/UserComponentsOption19/NoTemplateException.cs @@ -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) { } + } +} diff --git a/UserComponentsOption19/UserComponentsOption19/WriteComponent.Designer.cs b/UserComponentsOption19/UserComponentsOption19/WriteComponent.Designer.cs index 6b5c4ee..fee17ad 100644 --- a/UserComponentsOption19/UserComponentsOption19/WriteComponent.Designer.cs +++ b/UserComponentsOption19/UserComponentsOption19/WriteComponent.Designer.cs @@ -37,6 +37,7 @@ textBox.Name = "textBox"; textBox.Size = new Size(310, 27); textBox.TabIndex = 0; + textBox.TextChanged += textBox_TextChanged; // // WriteComponent // diff --git a/UserComponentsOption19/UserComponentsOption19/WriteComponent.cs b/UserComponentsOption19/UserComponentsOption19/WriteComponent.cs index 2815ca8..f6f499b 100644 --- a/UserComponentsOption19/UserComponentsOption19/WriteComponent.cs +++ b/UserComponentsOption19/UserComponentsOption19/WriteComponent.cs @@ -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(); } } }