From 2a817ffd5d5943ed1a4a0ab4039abc06daaec2d7 Mon Sep 17 00:00:00 2001 From: "ityurner02@mail.ru" Date: Fri, 15 Sep 2023 21:10:27 +0400 Subject: [PATCH] =?UTF-8?q?=D0=BF=D0=BE=D1=85=D0=BE=D0=B6=D0=B5=202=20?= =?UTF-8?q?=D0=BA=D0=BE=D0=BC=D0=BF=D0=BE=D0=BD=D0=B5=D0=BD=D1=82=20=D0=B3?= =?UTF-8?q?=D0=BE=D1=82=D0=BE=D0=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../MyEmailTextBox.Designer.cs | 59 +++++++++ COP/VisualComponentsLib/MyEmailTextBox.cs | 85 +++++++++++++ COP/VisualComponentsLib/MyEmailTextBox.resx | 120 ++++++++++++++++++ .../VisualComponentsLib.csproj | 9 ++ COP/WinForms/Form1.Designer.cs | 72 +++++++++++ COP/WinForms/Form1.cs | 25 ++++ 6 files changed, 370 insertions(+) create mode 100644 COP/VisualComponentsLib/MyEmailTextBox.Designer.cs create mode 100644 COP/VisualComponentsLib/MyEmailTextBox.cs create mode 100644 COP/VisualComponentsLib/MyEmailTextBox.resx diff --git a/COP/VisualComponentsLib/MyEmailTextBox.Designer.cs b/COP/VisualComponentsLib/MyEmailTextBox.Designer.cs new file mode 100644 index 0000000..f83b797 --- /dev/null +++ b/COP/VisualComponentsLib/MyEmailTextBox.Designer.cs @@ -0,0 +1,59 @@ +namespace VisualComponentsLib +{ + partial class MyEmailTextBox + { + /// + /// Обязательная переменная конструктора. + /// + private System.ComponentModel.IContainer components = null; + + /// + /// Освободить все используемые ресурсы. + /// + /// истинно, если управляемый ресурс должен быть удален; иначе ложно. + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Код, автоматически созданный конструктором компонентов + + /// + /// Требуемый метод для поддержки конструктора — не изменяйте + /// содержимое этого метода с помощью редактора кода. + /// + private void InitializeComponent() + { + this.emailTextBox = new System.Windows.Forms.TextBox(); + this.SuspendLayout(); + // + // emailTextBox + // + this.emailTextBox.Location = new System.Drawing.Point(3, 3); + this.emailTextBox.Name = "emailTextBox"; + this.emailTextBox.Size = new System.Drawing.Size(170, 22); + this.emailTextBox.TabIndex = 0; + this.emailTextBox.TextChanged += new System.EventHandler(this.textBox_TextChanged); + this.emailTextBox.Enter += new System.EventHandler(this.textBox_Enter); + // + // MyEmailTextBox + // + this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 16F); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.Controls.Add(this.emailTextBox); + this.Name = "MyEmailTextBox"; + this.Size = new System.Drawing.Size(177, 30); + this.ResumeLayout(false); + this.PerformLayout(); + + } + + #endregion + + private System.Windows.Forms.TextBox emailTextBox; + } +} diff --git a/COP/VisualComponentsLib/MyEmailTextBox.cs b/COP/VisualComponentsLib/MyEmailTextBox.cs new file mode 100644 index 0000000..1b730c3 --- /dev/null +++ b/COP/VisualComponentsLib/MyEmailTextBox.cs @@ -0,0 +1,85 @@ +using System; +using ToolTip = System.Windows.Forms.ToolTip; +using System.Text.RegularExpressions; +using System.Windows.Forms; +using System.Drawing; + +namespace VisualComponentsLib +{ + public partial class MyEmailTextBox : UserControl + { + //Шаблон для textbox + private string pattern; + //Пример ввода + private string example = "ti@gmail.com"; + public MyEmailTextBox() + { + InitializeComponent(); + } + public string Pattern + { + get { return pattern; } + set { pattern = value; } + } + public string TextBoxValue + { + get + { + Regex rg = new Regex(Pattern); + bool address = rg.IsMatch(emailTextBox.Text); + if (address) + { + return emailTextBox.Text; + } + else + { + throw new Exception("Неправильный адрес"); + } + } + set + { + Regex rg = new Regex(Pattern); + bool address = rg.IsMatch(value); + if (address) + { + emailTextBox.Text = value; + } + else + { + throw new Exception("Неправильный адрес"); + } + } + } + public void setExample(string str) + { + Regex rg = new Regex(Pattern); + bool address = rg.IsMatch(str); + if (address) + { + example = str; + } + + } + private void textBox_Enter(object sender, EventArgs e) + { + int VisibleTime = 2000; //ms + + ToolTip tooltip = new ToolTip(); + tooltip.Show(example, emailTextBox, 30, -20, VisibleTime); + } + + private void textBox_TextChanged(object sender, EventArgs e) + { + Regex rg = new Regex(Pattern); + bool address = rg.IsMatch(emailTextBox.Text); + if (address) + { + emailTextBox.BackColor = Color.Green; + } + else + { + emailTextBox.BackColor = Color.Red; + } + } + } +} diff --git a/COP/VisualComponentsLib/MyEmailTextBox.resx b/COP/VisualComponentsLib/MyEmailTextBox.resx new file mode 100644 index 0000000..1af7de1 --- /dev/null +++ b/COP/VisualComponentsLib/MyEmailTextBox.resx @@ -0,0 +1,120 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git a/COP/VisualComponentsLib/VisualComponentsLib.csproj b/COP/VisualComponentsLib/VisualComponentsLib.csproj index 9c00681..b20a97a 100644 --- a/COP/VisualComponentsLib/VisualComponentsLib.csproj +++ b/COP/VisualComponentsLib/VisualComponentsLib.csproj @@ -48,6 +48,12 @@ MyDropDownList.cs + + UserControl + + + MyEmailTextBox.cs + @@ -55,6 +61,9 @@ MyDropDownList.cs + + MyEmailTextBox.cs + \ No newline at end of file diff --git a/COP/WinForms/Form1.Designer.cs b/COP/WinForms/Form1.Designer.cs index 2e29a58..71a5cb5 100644 --- a/COP/WinForms/Form1.Designer.cs +++ b/COP/WinForms/Form1.Designer.cs @@ -33,6 +33,12 @@ buttonInfo = new Button(); labelInfo = new Label(); buttonClear = new Button(); + emailTextBox = new VisualComponentsLib.MyEmailTextBox(); + labelShow = new Label(); + buttonShow = new Button(); + buttonSetExample = new Button(); + labelExample = new Label(); + textBoxExample = new TextBox(); SuspendLayout(); // // dropDownList @@ -83,6 +89,60 @@ buttonClear.UseVisualStyleBackColor = true; buttonClear.Click += buttonClear_Click; // + // emailTextBox + // + emailTextBox.Location = new Point(12, 132); + emailTextBox.Margin = new Padding(3, 4, 3, 4); + emailTextBox.Name = "emailTextBox"; + emailTextBox.Pattern = null; + emailTextBox.Size = new Size(179, 34); + emailTextBox.TabIndex = 5; + // + // labelShow + // + labelShow.AutoSize = true; + labelShow.Location = new Point(12, 170); + labelShow.Name = "labelShow"; + labelShow.Size = new Size(76, 20); + labelShow.TabIndex = 10; + labelShow.Text = "проверка"; + // + // buttonShow + // + buttonShow.Location = new Point(12, 196); + buttonShow.Name = "buttonShow"; + buttonShow.Size = new Size(94, 29); + buttonShow.TabIndex = 9; + buttonShow.Text = "Проверка"; + buttonShow.UseVisualStyleBackColor = true; + buttonShow.Click += buttonShow_Click; + // + // buttonSetExample + // + buttonSetExample.Location = new Point(12, 328); + buttonSetExample.Name = "buttonSetExample"; + buttonSetExample.Size = new Size(188, 29); + buttonSetExample.TabIndex = 8; + buttonSetExample.Text = "Поменять пример"; + buttonSetExample.UseVisualStyleBackColor = true; + buttonSetExample.Click += buttonSetExample_Click; + // + // labelExample + // + labelExample.AutoSize = true; + labelExample.Location = new Point(12, 263); + labelExample.Name = "labelExample"; + labelExample.Size = new Size(147, 20); + labelExample.TabIndex = 7; + labelExample.Text = "Для ввода примера"; + // + // textBoxExample + // + textBoxExample.Location = new Point(12, 295); + textBoxExample.Name = "textBoxExample"; + textBoxExample.Size = new Size(188, 27); + textBoxExample.TabIndex = 6; + // // Form1 // AutoScaleDimensions = new SizeF(8F, 20F); @@ -93,6 +153,12 @@ Controls.Add(buttonInfo); Controls.Add(labelInfo); Controls.Add(buttonClear); + Controls.Add(emailTextBox); + Controls.Add(textBoxExample); + Controls.Add(labelExample); + Controls.Add(buttonSetExample); + Controls.Add(labelShow); + Controls.Add(buttonShow); Name = "Form1"; Text = "Form1"; ResumeLayout(false); @@ -105,5 +171,11 @@ private Button buttonInfo; private Label labelInfo; private Button buttonClear; + private VisualComponentsLib.MyEmailTextBox emailTextBox; + private TextBox textBoxExample; + private Label labelExample; + private Button buttonSetExample; + private Label labelShow; + private Button buttonShow; } } \ No newline at end of file diff --git a/COP/WinForms/Form1.cs b/COP/WinForms/Form1.cs index b4964f5..f9d026f 100644 --- a/COP/WinForms/Form1.cs +++ b/COP/WinForms/Form1.cs @@ -9,6 +9,7 @@ namespace WinForms list.AddRange(new string[] { "", "", "" }); InitializeComponent(); dropDownList.LoadValues(new List() { "", "", "" }); + emailTextBox.Pattern = @"^([\w\.\-]+)@([\w\-]+)((\.(\w){2,3})+)$"; } private void buttonAdd_Click(object sender, EventArgs e) { @@ -24,5 +25,29 @@ namespace WinForms { dropDownList.Clear(); } + + private void buttonSetExample_Click(object sender, EventArgs e) + { + if (textBoxExample.Text == String.Empty) + { + return; + } + emailTextBox.setExample(textBoxExample.Text); + } + + private void buttonShow_Click(object sender, EventArgs e) + { + try + { + if (emailTextBox.TextBoxValue != null) + { + labelShow.Text = ""; + } + } + catch (Exception ex) + { + MessageBox.Show(ex.Message); + } + } } } \ No newline at end of file