From 1d0c82bfb4f0e321b43606661bfa262f70c5ec62 Mon Sep 17 00:00:00 2001 From: abazov73 <92822431+abazov73@users.noreply.github.com> Date: Thu, 21 Sep 2023 21:05:38 +0400 Subject: [PATCH] Added tree view component --- AbazovApp/AbazovAppView/FormTest.Designer.cs | 82 ++++++++++++++++++- AbazovApp/AbazovAppView/FormTest.cs | 36 ++++++++ .../Components/AbazovTreeView.Designer.cs | 56 +++++++++++++ .../Components/AbazovTreeView.cs | 73 +++++++++++++++++ .../Components/AbazovTreeView.resx | 60 ++++++++++++++ AbazovApp/AbazovViewComponents/Worker.cs | 22 +++++ 6 files changed, 327 insertions(+), 2 deletions(-) create mode 100644 AbazovApp/AbazovViewComponents/Components/AbazovTreeView.Designer.cs create mode 100644 AbazovApp/AbazovViewComponents/Components/AbazovTreeView.cs create mode 100644 AbazovApp/AbazovViewComponents/Components/AbazovTreeView.resx create mode 100644 AbazovApp/AbazovViewComponents/Worker.cs diff --git a/AbazovApp/AbazovAppView/FormTest.Designer.cs b/AbazovApp/AbazovAppView/FormTest.Designer.cs index b39a6b9..d8bb050 100644 --- a/AbazovApp/AbazovAppView/FormTest.Designer.cs +++ b/AbazovApp/AbazovAppView/FormTest.Designer.cs @@ -36,6 +36,12 @@ this.buttonBoundries = new System.Windows.Forms.Button(); this.buttonSetDate = new System.Windows.Forms.Button(); this.buttonGetDate = new System.Windows.Forms.Button(); + this.abazovTreeView = new AbazovViewComponents.Components.AbazovTreeView(); + this.buttonSetHierarchy = new System.Windows.Forms.Button(); + this.buttonAddWorkers = new System.Windows.Forms.Button(); + this.buttonGetValue = new System.Windows.Forms.Button(); + this.buttonGetIndex = new System.Windows.Forms.Button(); + this.buttonSetIndex = new System.Windows.Forms.Button(); this.SuspendLayout(); // // abazovCheckedListBox @@ -79,9 +85,11 @@ // // abazovDatePicker // + this.abazovDatePicker.dateFrom = null; + this.abazovDatePicker.dateTo = null; this.abazovDatePicker.Location = new System.Drawing.Point(188, 12); this.abazovDatePicker.Name = "abazovDatePicker"; - this.abazovDatePicker.Size = new System.Drawing.Size(321, 48); + this.abazovDatePicker.Size = new System.Drawing.Size(275, 48); this.abazovDatePicker.TabIndex = 4; this.abazovDatePicker.DateChange += new System.Action(this.abazovDatePicker_DateChange); this.abazovDatePicker.AutoSizeChanged += new System.EventHandler(this.abazovDatePicker_DateChange); @@ -116,11 +124,75 @@ this.buttonGetDate.UseVisualStyleBackColor = true; this.buttonGetDate.Click += new System.EventHandler(this.buttonGetDate_Click); // + // abazovTreeView + // + this.abazovTreeView.Location = new System.Drawing.Point(469, 12); + this.abazovTreeView.Name = "abazovTreeView"; + this.abazovTreeView.SelectedNodeIndex = -1; + this.abazovTreeView.Size = new System.Drawing.Size(369, 249); + this.abazovTreeView.TabIndex = 8; + // + // buttonSetHierarchy + // + this.buttonSetHierarchy.Location = new System.Drawing.Point(469, 257); + this.buttonSetHierarchy.Name = "buttonSetHierarchy"; + this.buttonSetHierarchy.Size = new System.Drawing.Size(296, 29); + this.buttonSetHierarchy.TabIndex = 9; + this.buttonSetHierarchy.Text = "Установить иерархию"; + this.buttonSetHierarchy.UseVisualStyleBackColor = true; + this.buttonSetHierarchy.Click += new System.EventHandler(this.buttonSetHierarchy_Click); + // + // buttonAddWorkers + // + this.buttonAddWorkers.Location = new System.Drawing.Point(469, 292); + this.buttonAddWorkers.Name = "buttonAddWorkers"; + this.buttonAddWorkers.Size = new System.Drawing.Size(296, 29); + this.buttonAddWorkers.TabIndex = 10; + this.buttonAddWorkers.Text = "Добавить рабочих"; + this.buttonAddWorkers.UseVisualStyleBackColor = true; + this.buttonAddWorkers.Click += new System.EventHandler(this.buttonAddWorkers_Click); + // + // buttonGetValue + // + this.buttonGetValue.Location = new System.Drawing.Point(469, 327); + this.buttonGetValue.Name = "buttonGetValue"; + this.buttonGetValue.Size = new System.Drawing.Size(296, 29); + this.buttonGetValue.TabIndex = 11; + this.buttonGetValue.Text = "Получить значение"; + this.buttonGetValue.UseVisualStyleBackColor = true; + this.buttonGetValue.Click += new System.EventHandler(this.buttonGetValue_Click); + // + // buttonGetIndex + // + this.buttonGetIndex.Location = new System.Drawing.Point(469, 362); + this.buttonGetIndex.Name = "buttonGetIndex"; + this.buttonGetIndex.Size = new System.Drawing.Size(296, 29); + this.buttonGetIndex.TabIndex = 12; + this.buttonGetIndex.Text = "Получить индекс"; + this.buttonGetIndex.UseVisualStyleBackColor = true; + this.buttonGetIndex.Click += new System.EventHandler(this.buttonGetIndex_Click); + // + // buttonSetIndex + // + this.buttonSetIndex.Location = new System.Drawing.Point(469, 397); + this.buttonSetIndex.Name = "buttonSetIndex"; + this.buttonSetIndex.Size = new System.Drawing.Size(296, 29); + this.buttonSetIndex.TabIndex = 13; + this.buttonSetIndex.Text = "Установить индекс"; + this.buttonSetIndex.UseVisualStyleBackColor = true; + this.buttonSetIndex.Click += new System.EventHandler(this.buttonSetIndex_Click); + // // FormTest // this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 20F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(800, 450); + this.ClientSize = new System.Drawing.Size(850, 450); + this.Controls.Add(this.buttonSetIndex); + this.Controls.Add(this.buttonGetIndex); + this.Controls.Add(this.buttonGetValue); + this.Controls.Add(this.buttonAddWorkers); + this.Controls.Add(this.buttonSetHierarchy); + this.Controls.Add(this.abazovTreeView); this.Controls.Add(this.buttonGetDate); this.Controls.Add(this.buttonSetDate); this.Controls.Add(this.buttonBoundries); @@ -145,5 +217,11 @@ private Button buttonBoundries; private Button buttonSetDate; private Button buttonGetDate; + private AbazovViewComponents.Components.AbazovTreeView abazovTreeView; + private Button buttonSetHierarchy; + private Button buttonAddWorkers; + private Button buttonGetValue; + private Button buttonGetIndex; + private Button buttonSetIndex; } } \ No newline at end of file diff --git a/AbazovApp/AbazovAppView/FormTest.cs b/AbazovApp/AbazovAppView/FormTest.cs index 9068044..ffb38e2 100644 --- a/AbazovApp/AbazovAppView/FormTest.cs +++ b/AbazovApp/AbazovAppView/FormTest.cs @@ -91,5 +91,41 @@ namespace AbazovAppView MessageBox.Show(ex.Message); } } + + private void buttonSetHierarchy_Click(object sender, EventArgs e) + { + abazovTreeView.setHierarchy(new List<(string, bool)>() { ("department", false), ("groupNumber", false), ("name", true) }); + } + + private void buttonAddWorkers_Click(object sender, EventArgs e) + { + List workers = new List(); + workers.Add(new Worker("Отдел 1", 1, "Иванов")); + workers.Add(new Worker("Отдел 1", 1, "Петров")); + workers.Add(new Worker("Отдел 1", 1, "Иванов")); + workers.Add(new Worker("Отдел 1", 2, "Иванов")); + workers.Add(new Worker("Отдел 2", 1, "Иванов")); + workers.Add(new Worker("Отдел 2", 1, "Иванов")); + workers.Add(new Worker("Отдел 2", 1, "Петров")); + workers.Add(new Worker("Отдел 2", 2, "Иванов")); + abazovTreeView.addItems(workers); + } + + private void buttonGetValue_Click(object sender, EventArgs e) + { + Worker? worker = abazovTreeView.getSelecetedNodeValue(); + if (worker == null) return; + MessageBox.Show(worker.department + ", номер группы: " + worker.groupNumber + ", " + worker.name); + } + + private void buttonGetIndex_Click(object sender, EventArgs e) + { + MessageBox.Show(abazovTreeView.SelectedNodeIndex.ToString()); + } + + private void buttonSetIndex_Click(object sender, EventArgs e) + { + abazovTreeView.SelectedNodeIndex = 0; + } } } diff --git a/AbazovApp/AbazovViewComponents/Components/AbazovTreeView.Designer.cs b/AbazovApp/AbazovViewComponents/Components/AbazovTreeView.Designer.cs new file mode 100644 index 0000000..4412dfb --- /dev/null +++ b/AbazovApp/AbazovViewComponents/Components/AbazovTreeView.Designer.cs @@ -0,0 +1,56 @@ +namespace AbazovViewComponents.Components +{ + partial class AbazovTreeView + { + /// + /// Обязательная переменная конструктора. + /// + 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.treeView = new System.Windows.Forms.TreeView(); + this.SuspendLayout(); + // + // treeView + // + this.treeView.Location = new System.Drawing.Point(0, 0); + this.treeView.Name = "treeView"; + this.treeView.Size = new System.Drawing.Size(295, 212); + this.treeView.TabIndex = 0; + // + // AbazovTreeView + // + this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 20F); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.Controls.Add(this.treeView); + this.Name = "AbazovTreeView"; + this.Size = new System.Drawing.Size(295, 212); + this.ResumeLayout(false); + + } + + #endregion + + private TreeView treeView; + } +} diff --git a/AbazovApp/AbazovViewComponents/Components/AbazovTreeView.cs b/AbazovApp/AbazovViewComponents/Components/AbazovTreeView.cs new file mode 100644 index 0000000..956f8ae --- /dev/null +++ b/AbazovApp/AbazovViewComponents/Components/AbazovTreeView.cs @@ -0,0 +1,73 @@ +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; +using AbazovAppView; + +namespace AbazovViewComponents.Components +{ + public partial class AbazovTreeView : UserControl + { + public AbazovTreeView() + { + InitializeComponent(); + } + private List<(string, bool)> hierarchy = new List<(string, bool)>(); + + public int SelectedNodeIndex + { + get + { + return treeView.SelectedNode?.Index ?? -1; + } + set + { + if (treeView.SelectedNode == null) treeView.SelectedNode = value >= 0 && value < treeView.Nodes.Count ? treeView.Nodes[value] : treeView.SelectedNode; + else treeView.SelectedNode = value >= 0 && value < treeView.SelectedNode.Nodes.Count ? treeView.SelectedNode.Nodes[value] : treeView.SelectedNode; + } + } + + public Worker? getSelecetedNodeValue() + { + if (treeView.SelectedNode == null || treeView.SelectedNode.Nodes.Count > 0) return null; + return new Worker(treeView.SelectedNode.Parent.Parent.Text, Convert.ToInt32(treeView.SelectedNode.Parent.Text), treeView.SelectedNode.Text); + } + + public void setHierarchy(List<(string, bool)> fields) + { + hierarchy = fields; + } + + public void addItems(List items) + { + var type = typeof(T); + var fields = type.GetFields(); + foreach (T item in items) + { + TreeNodeCollection nodes = treeView.Nodes; + for (int i = 0; i < hierarchy.Count; i++) + { + var field = fields.FirstOrDefault(x => x.Name.Equals(hierarchy[i].Item1)); + if (field is not null) + { + var node = nodes.Find(field.Name, false).FirstOrDefault(x => x.Text == field.GetValue(item).ToString()); + if (node is not null && !hierarchy[i].Item2) + { + nodes = node.Nodes; + } + else + { + TreeNode newNode = nodes.Add(field.Name, field.GetValue(item).ToString()); + nodes = newNode.Nodes; + } + } + } + } + } + } +} diff --git a/AbazovApp/AbazovViewComponents/Components/AbazovTreeView.resx b/AbazovApp/AbazovViewComponents/Components/AbazovTreeView.resx new file mode 100644 index 0000000..f298a7b --- /dev/null +++ b/AbazovApp/AbazovViewComponents/Components/AbazovTreeView.resx @@ -0,0 +1,60 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 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/AbazovApp/AbazovViewComponents/Worker.cs b/AbazovApp/AbazovViewComponents/Worker.cs new file mode 100644 index 0000000..e9113bc --- /dev/null +++ b/AbazovApp/AbazovViewComponents/Worker.cs @@ -0,0 +1,22 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AbazovAppView +{ + public class Worker + { + public Worker(string department, int groupNumber, string name) + { + this.department = department; + this.groupNumber = groupNumber; + this.name = name; + } + + public string department; + public int groupNumber; + public string name; + } +}