Added tree view component

This commit is contained in:
abazov73 2023-09-21 21:05:38 +04:00
parent 71fa6549fb
commit 1d0c82bfb4
6 changed files with 327 additions and 2 deletions

View File

@ -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<System.DateTime>(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;
}
}

View File

@ -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<Worker> workers = new List<Worker>();
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;
}
}
}

View File

@ -0,0 +1,56 @@
namespace AbazovViewComponents.Components
{
partial class AbazovTreeView
{
/// <summary>
/// Обязательная переменная конструктора.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Освободить все используемые ресурсы.
/// </summary>
/// <param name="disposing">истинно, если управляемый ресурс должен быть удален; иначе ложно.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Код, автоматически созданный конструктором компонентов
/// <summary>
/// Требуемый метод для поддержки конструктора — не изменяйте
/// содержимое этого метода с помощью редактора кода.
/// </summary>
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;
}
}

View File

@ -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<T>(List<T> 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;
}
}
}
}
}
}
}

View File

@ -0,0 +1,60 @@
<root>
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
<xsd:element name="root" msdata:IsDataSet="true">
<xsd:complexType>
<xsd:choice maxOccurs="unbounded">
<xsd:element name="metadata">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" />
</xsd:sequence>
<xsd:attribute name="name" use="required" type="xsd:string" />
<xsd:attribute name="type" type="xsd:string" />
<xsd:attribute name="mimetype" type="xsd:string" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="assembly">
<xsd:complexType>
<xsd:attribute name="alias" type="xsd:string" />
<xsd:attribute name="name" type="xsd:string" />
</xsd:complexType>
</xsd:element>
<xsd:element name="data">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
<xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />
<xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
<xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="resheader">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" />
</xsd:complexType>
</xsd:element>
</xsd:choice>
</xsd:complexType>
</xsd:element>
</xsd:schema>
<resheader name="resmimetype">
<value>text/microsoft-resx</value>
</resheader>
<resheader name="version">
<value>2.0</value>
</resheader>
<resheader name="reader">
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
</root>

View File

@ -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;
}
}