From 918cbc514b7e1e4e71d6e0372ea127fe935171ae Mon Sep 17 00:00:00 2001 From: GokaPek Date: Sun, 15 Sep 2024 23:53:12 +0400 Subject: [PATCH] 3 --- ExecForm/Form1.Designer.cs | 30 ++++-- Library14Petrushin/Classes/Employee.cs | 15 +++ ...er.cs => HierarchicalTreeView.Designer.cs} | 24 ++++- Library14Petrushin/HierarchicalTreeView.cs | 98 +++++++++++++++++++ ...TextBox.resx => HierarchicalTreeView.resx} | 0 Library14Petrushin/RangeTextBox.cs | 57 ----------- ...x.Designer.cs => TextBoxRange.Designer.cs} | 13 ++- .../{TreeViewIerarch.cs => TextBoxRange.cs} | 4 +- ...TreeViewIerarch.resx => TextBoxRange.resx} | 50 +++++----- 9 files changed, 187 insertions(+), 104 deletions(-) create mode 100644 Library14Petrushin/Classes/Employee.cs rename Library14Petrushin/{TreeViewIerarch.Designer.cs => HierarchicalTreeView.Designer.cs} (64%) create mode 100644 Library14Petrushin/HierarchicalTreeView.cs rename Library14Petrushin/{RangeTextBox.resx => HierarchicalTreeView.resx} (100%) delete mode 100644 Library14Petrushin/RangeTextBox.cs rename Library14Petrushin/{RangeTextBox.Designer.cs => TextBoxRange.Designer.cs} (85%) rename Library14Petrushin/{TreeViewIerarch.cs => TextBoxRange.cs} (78%) rename Library14Petrushin/{TreeViewIerarch.resx => TextBoxRange.resx} (93%) diff --git a/ExecForm/Form1.Designer.cs b/ExecForm/Form1.Designer.cs index 3774001..524c16f 100644 --- a/ExecForm/Form1.Designer.cs +++ b/ExecForm/Form1.Designer.cs @@ -29,7 +29,8 @@ private void InitializeComponent() { chooseList1 = new Library14Petrushin.ChooseList(); - rangeTextBox1 = new Library14Petrushin.RangeTextBox(); + hierarchicalTreeView1 = new Library14Petrushin.HierarchicalTreeView(); + textBoxRange1 = new Library14Petrushin.TextBoxRange(); SuspendLayout(); // // chooseList1 @@ -40,21 +41,28 @@ chooseList1.Size = new Size(185, 190); chooseList1.TabIndex = 0; // - // rangeTextBox1 + // hierarchicalTreeView1 // - rangeTextBox1.Location = new Point(259, 14); - rangeTextBox1.MaxLength = 0; - rangeTextBox1.MinLength = 0; - rangeTextBox1.Name = "rangeTextBox1"; - rangeTextBox1.Size = new Size(188, 188); - rangeTextBox1.TabIndex = 1; + hierarchicalTreeView1.Location = new Point(567, 2); + hierarchicalTreeView1.Name = "hierarchicalTreeView1"; + hierarchicalTreeView1.SelectedNodeIndex = -1; + hierarchicalTreeView1.Size = new Size(266, 504); + hierarchicalTreeView1.TabIndex = 2; + // + // textBoxRange1 + // + textBoxRange1.Location = new Point(233, 24); + textBoxRange1.Name = "textBoxRange1"; + textBoxRange1.Size = new Size(259, 66); + textBoxRange1.TabIndex = 3; // // Form1 // AutoScaleDimensions = new SizeF(8F, 20F); AutoScaleMode = AutoScaleMode.Font; - ClientSize = new Size(800, 450); - Controls.Add(rangeTextBox1); + ClientSize = new Size(834, 507); + Controls.Add(textBoxRange1); + Controls.Add(hierarchicalTreeView1); Controls.Add(chooseList1); Name = "Form1"; Text = "Form1"; @@ -64,6 +72,8 @@ #endregion private Library14Petrushin.ChooseList chooseList1; + private Library14Petrushin.HierarchicalTreeView hierarchicalTreeView1; private Library14Petrushin.RangeTextBox rangeTextBox1; + private Library14Petrushin.TextBoxRange textBoxRange1; } } diff --git a/Library14Petrushin/Classes/Employee.cs b/Library14Petrushin/Classes/Employee.cs new file mode 100644 index 0000000..90d007e --- /dev/null +++ b/Library14Petrushin/Classes/Employee.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Library14Petrushin.Classes +{ + public class Employee + { + public string? Department { get; set; } + public string? Position { get; set; } + public string? FullName { get; set; } + } +} diff --git a/Library14Petrushin/TreeViewIerarch.Designer.cs b/Library14Petrushin/HierarchicalTreeView.Designer.cs similarity index 64% rename from Library14Petrushin/TreeViewIerarch.Designer.cs rename to Library14Petrushin/HierarchicalTreeView.Designer.cs index d1b8252..cdfc54d 100644 --- a/Library14Petrushin/TreeViewIerarch.Designer.cs +++ b/Library14Petrushin/HierarchicalTreeView.Designer.cs @@ -1,6 +1,6 @@ namespace Library14Petrushin { - partial class TreeViewIerarch + partial class HierarchicalTreeView { /// /// Обязательная переменная конструктора. @@ -28,10 +28,28 @@ /// private void InitializeComponent() { - components = new System.ComponentModel.Container(); - this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + treeView1 = new TreeView(); + SuspendLayout(); + // + // treeView1 + // + treeView1.Location = new Point(3, 3); + treeView1.Name = "treeView1"; + treeView1.Size = new Size(258, 496); + treeView1.TabIndex = 0; + // + // TreeViewIerarch + // + AutoScaleDimensions = new SizeF(8F, 20F); + AutoScaleMode = AutoScaleMode.Font; + Controls.Add(treeView1); + Name = "TreeViewIerarch"; + Size = new Size(264, 502); + ResumeLayout(false); } #endregion + + private TreeView treeView1; } } diff --git a/Library14Petrushin/HierarchicalTreeView.cs b/Library14Petrushin/HierarchicalTreeView.cs new file mode 100644 index 0000000..51e7592 --- /dev/null +++ b/Library14Petrushin/HierarchicalTreeView.cs @@ -0,0 +1,98 @@ +using Library14Petrushin.Classes; +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Data; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Forms; + +namespace Library14Petrushin +{ + public partial class HierarchicalTreeView : UserControl + { + private List hierarchy; + private Dictionary alwaysNewBranch; + + public HierarchicalTreeView() + { + InitializeComponent(); + hierarchy = new List(); + alwaysNewBranch = new Dictionary(); + } + + public void SetHierarchy(List hierarchy, Dictionary alwaysNewBranch) + { + this.hierarchy = hierarchy; + this.alwaysNewBranch = alwaysNewBranch; + } + + public void AddObjectToTree(object obj, string stopAtProperty) + { + TreeNode currentNode = treeView1.Nodes.Cast().FirstOrDefault(); + foreach (var property in hierarchy) + { + var value = obj.GetType().GetProperty(property).GetValue(obj, null).ToString(); + bool createNewBranch = alwaysNewBranch.ContainsKey(property) && alwaysNewBranch[property]; + + if (currentNode == null) + { + currentNode = treeView1.Nodes.Add(value); + } + else + { + var childNode = currentNode.Nodes.Cast().FirstOrDefault(n => n.Text == value); + if (childNode == null || createNewBranch) + { + childNode = currentNode.Nodes.Add(value); + } + currentNode = childNode; + } + + if (property == stopAtProperty) + { + break; + } + } + } + + public int SelectedNodeIndex + { + get + { + return treeView1.SelectedNode?.Index ?? -1; + } + set + { + if (value >= 0 && value < treeView1.Nodes.Count) + { + treeView1.SelectedNode = treeView1.Nodes[value]; + } + } + } + + public object GetSelectedObject() + { + if (treeView1.SelectedNode == null) + { + return null; + } + + var node = treeView1.SelectedNode; + var obj = Activator.CreateInstance(typeof(Employee)); + foreach (var property in hierarchy) + { + var value = node.Text; + obj.GetType().GetProperty(property).SetValue(obj, value); + node = node.Parent; + if (node == null) + { + break; + } + } + return obj; + } + } +} diff --git a/Library14Petrushin/RangeTextBox.resx b/Library14Petrushin/HierarchicalTreeView.resx similarity index 100% rename from Library14Petrushin/RangeTextBox.resx rename to Library14Petrushin/HierarchicalTreeView.resx diff --git a/Library14Petrushin/RangeTextBox.cs b/Library14Petrushin/RangeTextBox.cs deleted file mode 100644 index 536c133..0000000 --- a/Library14Petrushin/RangeTextBox.cs +++ /dev/null @@ -1,57 +0,0 @@ -using Library14Petrushin.Exeptions; -using System; -using System.Collections.Generic; -using System.ComponentModel; -using System.Data; -using System.Drawing; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using System.Windows.Forms; - -namespace Library14Petrushin -{ - public partial class RangeTextBox : UserControl - { - public int MinLength { get; set; } - public int MaxLength { get; set; } - - public event EventHandler? ValueChanged; - - public string InputValue - { - get - { - if (MinLength == 0 && MaxLength == 0) - { - throw new RangeException("Диапазон не задан."); - } - if (textBox1.Text.Length < MinLength || textBox1.Text.Length > MaxLength) - { - throw new RangeException("Введенное значение не входит в диапазон."); - } - return textBox1.Text; - } - set - { - if (MinLength != 0 && MaxLength != 0) - { - if (value.Length >= MinLength && value.Length <= MaxLength) - { - textBox1.Text = value; - ValueChanged?.Invoke(this, EventArgs.Empty); - } - } - else - { - throw new RangeException("Диапазон не задан."); - } - } - } - - private void TextBox1_TextChanged(object sender, EventArgs e) - { - ValueChanged?.Invoke(this, e); - } - } -} diff --git a/Library14Petrushin/RangeTextBox.Designer.cs b/Library14Petrushin/TextBoxRange.Designer.cs similarity index 85% rename from Library14Petrushin/RangeTextBox.Designer.cs rename to Library14Petrushin/TextBoxRange.Designer.cs index 7d618e3..cd8a8db 100644 --- a/Library14Petrushin/RangeTextBox.Designer.cs +++ b/Library14Petrushin/TextBoxRange.Designer.cs @@ -1,6 +1,6 @@ namespace Library14Petrushin { - partial class RangeTextBox + partial class TextBoxRange { /// /// Обязательная переменная конструктора. @@ -33,19 +33,18 @@ // // textBox1 // - textBox1.Location = new Point(0, 3); + textBox1.Location = new Point(3, 14); textBox1.Name = "textBox1"; - textBox1.Size = new Size(270, 27); + textBox1.Size = new Size(243, 27); textBox1.TabIndex = 0; - textBox1.TextChanged += TextBox1_TextChanged; // - // RangeTextBox + // TextBoxRange // AutoScaleDimensions = new SizeF(8F, 20F); AutoScaleMode = AutoScaleMode.Font; Controls.Add(textBox1); - Name = "RangeTextBox"; - Size = new Size(273, 37); + Name = "TextBoxRange"; + Size = new Size(252, 53); ResumeLayout(false); PerformLayout(); } diff --git a/Library14Petrushin/TreeViewIerarch.cs b/Library14Petrushin/TextBoxRange.cs similarity index 78% rename from Library14Petrushin/TreeViewIerarch.cs rename to Library14Petrushin/TextBoxRange.cs index 18c339f..02d16b9 100644 --- a/Library14Petrushin/TreeViewIerarch.cs +++ b/Library14Petrushin/TextBoxRange.cs @@ -10,9 +10,9 @@ using System.Windows.Forms; namespace Library14Petrushin { - public partial class TreeViewIerarch : UserControl + public partial class TextBoxRange : UserControl { - public TreeViewIerarch() + public TextBoxRange() { InitializeComponent(); } diff --git a/Library14Petrushin/TreeViewIerarch.resx b/Library14Petrushin/TextBoxRange.resx similarity index 93% rename from Library14Petrushin/TreeViewIerarch.resx rename to Library14Petrushin/TextBoxRange.resx index 1af7de1..af32865 100644 --- a/Library14Petrushin/TreeViewIerarch.resx +++ b/Library14Petrushin/TextBoxRange.resx @@ -1,17 +1,17 @@  -