From 2fc9ddf69f7bc7e35540696290e709bbacbc070b Mon Sep 17 00:00:00 2001 From: Yuee Shiness Date: Thu, 21 Sep 2023 17:03:20 +0400 Subject: [PATCH] Events in customTextBox. --- CustomComponents/DropList.Designer.cs | 14 +++-- CustomComponents/DropList.cs | 19 ++---- CustomComponents/DropList.resx | 62 +------------------ .../Exceptions/ContentException.cs | 24 +++++++ .../Exceptions/NotIntegerException.cs | 15 +++++ CustomComponents/TextBox.cs | 36 +++++++---- TestForms/Form1.Designer.cs | 23 ++++--- TestForms/Form1.cs | 6 +- TestForms/Form1.resx | 62 +------------------ 9 files changed, 96 insertions(+), 165 deletions(-) create mode 100644 CustomComponents/Exceptions/ContentException.cs create mode 100644 CustomComponents/Exceptions/NotIntegerException.cs diff --git a/CustomComponents/DropList.Designer.cs b/CustomComponents/DropList.Designer.cs index faa2145..2bb3130 100644 --- a/CustomComponents/DropList.Designer.cs +++ b/CustomComponents/DropList.Designer.cs @@ -34,20 +34,22 @@ // listBox // listBox.FormattingEnabled = true; - listBox.ItemHeight = 20; - listBox.Location = new Point(13, 12); + listBox.ItemHeight = 15; + listBox.Location = new Point(11, 9); + listBox.Margin = new Padding(3, 2, 3, 2); listBox.Name = "listBox"; - listBox.Size = new Size(150, 104); + listBox.Size = new Size(132, 79); listBox.TabIndex = 0; - listBox.SelectedValueChanged += + listBox.SelectedValueChanged += SelectedValue_Changed; // // Frame // - AutoScaleDimensions = new SizeF(8F, 20F); + AutoScaleDimensions = new SizeF(7F, 15F); AutoScaleMode = AutoScaleMode.Font; Controls.Add(listBox); + Margin = new Padding(3, 2, 3, 2); Name = "Frame"; - Size = new Size(189, 136); + Size = new Size(165, 103); ResumeLayout(false); } diff --git a/CustomComponents/DropList.cs b/CustomComponents/DropList.cs index 35ce0eb..d4e0849 100644 --- a/CustomComponents/DropList.cs +++ b/CustomComponents/DropList.cs @@ -3,21 +3,10 @@ public partial class Frame : UserControl { - private event EventHandler? _valueChanged; private string? SelectedValue = string.Empty; - public event EventHandler ValueChanged - { - add - { - _valueChanged += value; - } - remove - { - _valueChanged -= value; - } - } + public event EventHandler ValueChanged; public Frame() { @@ -42,6 +31,10 @@ { get { + if (listBox.SelectedItem is null) + { + return ""; + } return SelectedValue; } set @@ -54,7 +47,7 @@ { var element = sender as ListBox; Value = element.Text.ToString(); - _valueChanged?.Invoke(this, e); + ValueChanged?.Invoke(this, e); } } } \ No newline at end of file diff --git a/CustomComponents/DropList.resx b/CustomComponents/DropList.resx index af32865..f298a7b 100644 --- a/CustomComponents/DropList.resx +++ b/CustomComponents/DropList.resx @@ -1,64 +1,4 @@ - - - + diff --git a/CustomComponents/Exceptions/ContentException.cs b/CustomComponents/Exceptions/ContentException.cs new file mode 100644 index 0000000..5a3d83f --- /dev/null +++ b/CustomComponents/Exceptions/ContentException.cs @@ -0,0 +1,24 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace CustomComponents.Exceptions +{ + public class ContentException : Exception + { + public ContentException(bool state) : base(CustomMessage(state)){ } + + private static string CustomMessage(bool state) + { + if(state) + { + return "TextBox is empty however null flag is checked."; + } + + return "TextBox is empty. You better write something!"; + } + + } +} diff --git a/CustomComponents/Exceptions/NotIntegerException.cs b/CustomComponents/Exceptions/NotIntegerException.cs new file mode 100644 index 0000000..6a79de2 --- /dev/null +++ b/CustomComponents/Exceptions/NotIntegerException.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace CustomComponents.Exceptions +{ + public class NotIntegerException : Exception + { + public NotIntegerException() { } + + public NotIntegerException(string message) : base(new string($"{message} is not an integer!")) { } + } +} diff --git a/CustomComponents/TextBox.cs b/CustomComponents/TextBox.cs index 930cc62..29151b7 100644 --- a/CustomComponents/TextBox.cs +++ b/CustomComponents/TextBox.cs @@ -1,4 +1,5 @@ -using System; +using CustomComponents.Exceptions; +using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; @@ -13,6 +14,9 @@ namespace CustomComponents public partial class frameTextBox : UserControl { private string? _text; + + public event EventHandler taskHandler; + public frameTextBox() { InitializeComponent(); @@ -32,22 +36,32 @@ namespace CustomComponents private void CheckBoxStateChanged(object sender, EventArgs e) { - if (ruleCheckBox.Checked) + try { - textBox.Enabled = false; - if (!string.IsNullOrEmpty(_text)) + if (ruleCheckBox.Checked) { - MessageBox.Show("TextBox is not empty!"); + textBox.Enabled = false; + + if (!string.IsNullOrEmpty(_text)) + { + throw new ContentException(true); + } + } + else + { + textBox.Enabled = true; + if (string.IsNullOrEmpty(_text)) + { + throw new ContentException(false); + } } } - else + catch(ContentException ex) { - textBox.Enabled = true; - if (string.IsNullOrEmpty(_text)) - { - MessageBox.Show("TextBox is empty. You better write something!"); - } + taskHandler += (sender, e) => MessageBox.Show(ex.Message); } + taskHandler?.Invoke(this, e); + } private void CheckTextBoxValueType(object sender, EventArgs args) diff --git a/TestForms/Form1.Designer.cs b/TestForms/Form1.Designer.cs index cd09dd4..533ab72 100644 --- a/TestForms/Form1.Designer.cs +++ b/TestForms/Form1.Designer.cs @@ -36,9 +36,10 @@ // // fillButton // - fillButton.Location = new Point(175, 18); + fillButton.Location = new Point(153, 14); + fillButton.Margin = new Padding(3, 2, 3, 2); fillButton.Name = "fillButton"; - fillButton.Size = new Size(94, 29); + fillButton.Size = new Size(82, 22); fillButton.TabIndex = 1; fillButton.Text = "FILL"; fillButton.UseVisualStyleBackColor = true; @@ -46,9 +47,10 @@ // // clearButton // - clearButton.Location = new Point(175, 75); + clearButton.Location = new Point(153, 56); + clearButton.Margin = new Padding(3, 2, 3, 2); clearButton.Name = "clearButton"; - clearButton.Size = new Size(94, 29); + clearButton.Size = new Size(82, 22); clearButton.TabIndex = 2; clearButton.Text = "CLEAR"; clearButton.UseVisualStyleBackColor = true; @@ -57,28 +59,31 @@ // listBox // listBox.Location = new Point(-3, 0); + listBox.Margin = new Padding(3, 2, 3, 2); listBox.Name = "listBox"; - listBox.Size = new Size(172, 137); + listBox.Size = new Size(150, 120); listBox.TabIndex = 3; listBox.Value = ""; // // frameTextBox1 // - frameTextBox1.Location = new Point(312, 18); + frameTextBox1.Location = new Point(273, 14); + frameTextBox1.Margin = new Padding(3, 2, 3, 2); frameTextBox1.Name = "frameTextBox1"; - frameTextBox1.Size = new Size(291, 91); + frameTextBox1.Size = new Size(255, 68); frameTextBox1.TabIndex = 4; frameTextBox1.Value = null; // // TestFrame // - AutoScaleDimensions = new SizeF(8F, 20F); + AutoScaleDimensions = new SizeF(7F, 15F); AutoScaleMode = AutoScaleMode.Font; - ClientSize = new Size(800, 450); + ClientSize = new Size(700, 338); Controls.Add(frameTextBox1); Controls.Add(clearButton); Controls.Add(fillButton); Controls.Add(listBox); + Margin = new Padding(3, 2, 3, 2); Name = "TestFrame"; Text = "Form1"; ResumeLayout(false); diff --git a/TestForms/Form1.cs b/TestForms/Form1.cs index f25e377..35de418 100644 --- a/TestForms/Form1.cs +++ b/TestForms/Form1.cs @@ -2,11 +2,10 @@ namespace TestForms { public partial class TestFrame : Form { - + public TestFrame() { InitializeComponent(); - listBox.ValueChanged += new EventHandler() } private void clearButton_Click(object sender, EventArgs e) @@ -17,10 +16,9 @@ namespace TestForms private void fillButton_Click(object sender, EventArgs e) { listBox.PopulateList(new List { "SILENT", "RIZZ", "BLSSDY", "YUEEJKE" }); - } - + } } \ No newline at end of file diff --git a/TestForms/Form1.resx b/TestForms/Form1.resx index af32865..f298a7b 100644 --- a/TestForms/Form1.resx +++ b/TestForms/Form1.resx @@ -1,64 +1,4 @@ - - - +