Завершил разработку treeview

This commit is contained in:
Никита Потапов 2024-09-23 14:37:08 +04:00
parent bce34ad0ab
commit 472cd6d16f
12 changed files with 577 additions and 2 deletions

View File

@ -0,0 +1,7 @@
namespace Components.Exceptions
{
public class NodeIsNotLeafException : UserControlTreeViewException
{
public NodeIsNotLeafException() : base("Selected node is not a leaf") { }
}
}

View File

@ -0,0 +1,7 @@
namespace Components.Exceptions
{
public class NotSelectedNodeException : UserControlTreeViewException
{
public NotSelectedNodeException() : base("Node is not selected") { }
}
}

View File

@ -0,0 +1,7 @@
namespace Components.Exceptions
{
public class PropertyNotDeclaratedException : UserControlTreeViewException
{
public PropertyNotDeclaratedException(string propName) : base($"Property \"{propName}\" not declared") { }
}
}

View File

@ -0,0 +1,7 @@
namespace Components.Exceptions
{
public class PropertyNullException : UserControlTreeViewException
{
public PropertyNullException(string propName) : base($"Property \"{propName}\" is null") { }
}
}

View File

@ -0,0 +1,7 @@
namespace Components.Exceptions
{
public class TreeHierarchyNotSetException : UserControlTreeViewException
{
public TreeHierarchyNotSetException() : base("Tree hierarchy not set") { }
}
}

View File

@ -0,0 +1,8 @@
namespace Components.Exceptions
{
public class UserControlTreeViewException : Exception
{
public UserControlTreeViewException() { }
public UserControlTreeViewException(string message) : base(message) { }
}
}

View File

@ -0,0 +1,56 @@
namespace Components
{
partial class UserControlTreeView
{
/// <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()
{
treeView = new TreeView();
SuspendLayout();
//
// treeView
//
treeView.Location = new Point(22, 17);
treeView.Name = "treeView";
treeView.Size = new Size(188, 285);
treeView.TabIndex = 0;
//
// UserControlTreeView
//
AutoScaleDimensions = new SizeF(7F, 15F);
AutoScaleMode = AutoScaleMode.Font;
BackColor = SystemColors.ActiveCaption;
Controls.Add(treeView);
Name = "UserControlTreeView";
Size = new Size(232, 335);
ResumeLayout(false);
}
#endregion
private TreeView treeView;
}
}

View File

@ -0,0 +1,142 @@
using Components.Exceptions;
using System.Reflection;
namespace Components
{
public partial class UserControlTreeView : UserControl
{
public int SelectedIndex
{
get
{
return treeView.SelectedNode?.Index ?? -1;
}
set
{
if (value > -1 && value < treeView.Nodes.Count)
{
treeView.SelectedNode = treeView.Nodes[value];
}
}
}
private List<(string PropertyName, bool AlwaysCreateBranch)> Hierarchy;
public UserControlTreeView()
{
InitializeComponent();
Hierarchy = [];
}
public T GetObjectSelectedNode<T>()
where T : class, new()
{
var node = treeView.SelectedNode;
if (node == null)
{
throw new NotSelectedNodeException();
}
if ((Hierarchy?.Count ?? 0) > 0)
{
throw new TreeHierarchyNotSetException();
}
if (node.Nodes.Count > 0)
{
throw new NodeIsNotLeafException();
}
T obj = new T();
int propIndex = GetNodeDepth(node);
while (node != null)
{
string propValue = node.Text;
string propName = Hierarchy[propIndex].PropertyName;
var prop = obj.GetType().GetProperty(propName);
if (prop == null)
{
throw new PropertyNotDeclaratedException(propName);
}
prop.SetValue(obj, propValue);
node = node.Parent;
propIndex--;
}
return obj;
}
private int GetNodeDepth(TreeNode node)
{
int depth = 0;
while (node.Parent != null)
{
depth++;
node = node.Parent;
}
return depth;
}
public void SetTreeObjects<T>(List<T> objects)
{
if (objects.Count == 0)
{
return;
}
if ((Hierarchy?.Count ?? 0) > 0)
{
throw new TreeHierarchyNotSetException();
}
PropertyInfo[]? properties = objects[0]!.GetType().GetProperties();
ClearTreeView();
foreach (T obj in objects)
{
var nodes = treeView.Nodes;
foreach (var hierarchyProperty in Hierarchy)
{
PropertyInfo? objectPropertyInfo = properties?.Single(prop => prop.Name == hierarchyProperty.PropertyName);
if (objectPropertyInfo == null)
{
throw new PropertyNotDeclaratedException(hierarchyProperty.PropertyName);
}
string? objectPropertyValue = objectPropertyInfo.GetValue(obj)?.ToString() ?? null;
if (objectPropertyValue == null)
{
throw new PropertyNullException(hierarchyProperty.PropertyName);
}
TreeNode? node = null;
if (!hierarchyProperty.AlwaysCreateBranch)
{
foreach (TreeNode childNode in nodes)
{
if (childNode.Text == objectPropertyValue)
{
node = childNode;
break;
}
}
}
if (node == null)
{
node = nodes.Add(objectPropertyValue);
}
nodes = node.Nodes;
}
}
}
public void SetHierarchy(List<(string PropertyName, bool AlwaysCreateBranch)> hierarchy)
{
Hierarchy = hierarchy;
}
public void ClearTreeView()
{
treeView.Nodes.Clear();
treeView.Update();
}
}
}

View File

@ -0,0 +1,120 @@
<?xml version="1.0" encoding="utf-8"?>
<root>
<!--
Microsoft ResX Schema
Version 2.0
The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes
associated with the data types.
Example:
... ado.net/XML headers & schema ...
<resheader name="resmimetype">text/microsoft-resx</resheader>
<resheader name="version">2.0</resheader>
<resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
<resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
<data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data>
<data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>
<data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
<value>[base64 mime encoded serialized .NET Framework object]</value>
</data>
<data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
<comment>This is a comment</comment>
</data>
There are any number of "resheader" rows that contain simple
name/value pairs.
Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the
mimetype set.
The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not
extensible. For a given mimetype the value must be set accordingly:
Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can
read any of the formats listed below.
mimetype: application/x-microsoft.net.object.binary.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.soap.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.bytearray.base64
value : The object must be serialized into a byte array
: using a System.ComponentModel.TypeConverter
: and then encoded with base64 encoding.
-->
<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 @@
namespace WinFormsApp
{
public class Employee
{
public string Departament { get; set; }
public string Position { get; set; }
public string Name { get; set; }
public string Surname { get; set; }
public Employee() { }
public Employee(string departament, string position, string surname, string name)
{
Departament = departament;
Position = position;
Name = name;
Surname = surname;
}
public override string ToString()
{
return Departament + ", " + Position + ", " + Surname + " " + Name;
}
}
}

View File

@ -31,6 +31,17 @@
userControlCheckedList = new Components.UserControlCheckedList();
buttonFillCheckedList = new Button();
buttonClearList = new Button();
userControlDatePicker = new Components.UserControlDatePicker();
dateTimePickerMinDate = new DateTimePicker();
dateTimePicker1 = new DateTimePicker();
label1 = new Label();
label2 = new Label();
userControlTreeView = new Components.UserControlTreeView();
buttonFillTreeView = new Button();
buttonClearTreeView = new Button();
buttonShowTreeViewNode = new Button();
textBoxTreeViewSelectedNodeIndex = new TextBox();
textBoxTreeViewSelectedObject = new TextBox();
SuspendLayout();
//
// userControlCheckedList
@ -38,7 +49,7 @@
userControlCheckedList.BackColor = Color.Firebrick;
userControlCheckedList.Location = new Point(12, 12);
userControlCheckedList.Name = "userControlCheckedList";
userControlCheckedList.Size = new Size(315, 170);
userControlCheckedList.Size = new Size(168, 170);
userControlCheckedList.TabIndex = 0;
//
// buttonFillCheckedList
@ -61,17 +72,125 @@
buttonClearList.UseVisualStyleBackColor = true;
buttonClearList.Click += buttonClearList_Click;
//
// userControlDatePicker
//
userControlDatePicker.BackColor = Color.Orange;
userControlDatePicker.Location = new Point(273, 12);
userControlDatePicker.Name = "userControlDatePicker";
userControlDatePicker.Size = new Size(121, 50);
userControlDatePicker.TabIndex = 3;
//
// dateTimePickerMinDate
//
dateTimePickerMinDate.Format = DateTimePickerFormat.Short;
dateTimePickerMinDate.Location = new Point(246, 95);
dateTimePickerMinDate.Name = "dateTimePickerMinDate";
dateTimePickerMinDate.Size = new Size(91, 23);
dateTimePickerMinDate.TabIndex = 4;
//
// dateTimePicker1
//
dateTimePicker1.Format = DateTimePickerFormat.Short;
dateTimePicker1.Location = new Point(343, 95);
dateTimePicker1.Name = "dateTimePicker1";
dateTimePicker1.Size = new Size(95, 23);
dateTimePicker1.TabIndex = 5;
//
// label1
//
label1.AutoSize = true;
label1.Location = new Point(264, 77);
label1.Name = "label1";
label1.Size = new Size(52, 15);
label1.TabIndex = 6;
label1.Text = "MinDate";
//
// label2
//
label2.AutoSize = true;
label2.Location = new Point(361, 77);
label2.Name = "label2";
label2.Size = new Size(54, 15);
label2.TabIndex = 7;
label2.Text = "MaxDate";
//
// userControlTreeView
//
userControlTreeView.BackColor = SystemColors.ActiveCaption;
userControlTreeView.Location = new Point(498, 12);
userControlTreeView.Name = "userControlTreeView";
userControlTreeView.SelectedIndex = 0;
userControlTreeView.Size = new Size(232, 320);
userControlTreeView.TabIndex = 8;
//
// buttonFillTreeView
//
buttonFillTreeView.Location = new Point(498, 348);
buttonFillTreeView.Name = "buttonFillTreeView";
buttonFillTreeView.Size = new Size(75, 23);
buttonFillTreeView.TabIndex = 9;
buttonFillTreeView.Text = "Заполнить";
buttonFillTreeView.UseVisualStyleBackColor = true;
buttonFillTreeView.Click += buttonFillTreeView_Click;
//
// buttonClearTreeView
//
buttonClearTreeView.Location = new Point(574, 348);
buttonClearTreeView.Name = "buttonClearTreeView";
buttonClearTreeView.Size = new Size(75, 23);
buttonClearTreeView.TabIndex = 10;
buttonClearTreeView.Text = "Очистить";
buttonClearTreeView.UseVisualStyleBackColor = true;
buttonClearTreeView.Click += buttonClearTreeView_Click;
//
// buttonShowTreeViewNode
//
buttonShowTreeViewNode.Location = new Point(655, 348);
buttonShowTreeViewNode.Name = "buttonShowTreeViewNode";
buttonShowTreeViewNode.Size = new Size(75, 23);
buttonShowTreeViewNode.TabIndex = 11;
buttonShowTreeViewNode.Text = "Показать";
buttonShowTreeViewNode.UseVisualStyleBackColor = true;
buttonShowTreeViewNode.Click += buttonShowTreeViewNode_Click;
//
// textBoxTreeViewSelectedNodeIndex
//
textBoxTreeViewSelectedNodeIndex.Location = new Point(630, 397);
textBoxTreeViewSelectedNodeIndex.Name = "textBoxTreeViewSelectedNodeIndex";
textBoxTreeViewSelectedNodeIndex.Size = new Size(100, 23);
textBoxTreeViewSelectedNodeIndex.TabIndex = 12;
//
// textBoxTreeViewSelectedObject
//
textBoxTreeViewSelectedObject.Location = new Point(31, 397);
textBoxTreeViewSelectedObject.Name = "textBoxTreeViewSelectedObject";
textBoxTreeViewSelectedObject.ReadOnly = true;
textBoxTreeViewSelectedObject.Size = new Size(593, 23);
textBoxTreeViewSelectedObject.TabIndex = 13;
//
// Form
//
AutoScaleDimensions = new SizeF(7F, 15F);
AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(697, 459);
ClientSize = new Size(768, 470);
Controls.Add(textBoxTreeViewSelectedObject);
Controls.Add(textBoxTreeViewSelectedNodeIndex);
Controls.Add(buttonShowTreeViewNode);
Controls.Add(buttonClearTreeView);
Controls.Add(buttonFillTreeView);
Controls.Add(userControlTreeView);
Controls.Add(label2);
Controls.Add(label1);
Controls.Add(dateTimePicker1);
Controls.Add(dateTimePickerMinDate);
Controls.Add(userControlDatePicker);
Controls.Add(buttonClearList);
Controls.Add(buttonFillCheckedList);
Controls.Add(userControlCheckedList);
Name = "Form";
Text = "Form";
ResumeLayout(false);
PerformLayout();
}
#endregion
@ -79,5 +198,16 @@
private Components.UserControlCheckedList userControlCheckedList;
private Button buttonFillCheckedList;
private Button buttonClearList;
private Components.UserControlDatePicker userControlDatePicker;
private DateTimePicker dateTimePickerMinDate;
private DateTimePicker dateTimePicker1;
private Label label1;
private Label label2;
private Components.UserControlTreeView userControlTreeView;
private Button buttonFillTreeView;
private Button buttonClearTreeView;
private Button buttonShowTreeViewNode;
private TextBox textBoxTreeViewSelectedNodeIndex;
private TextBox textBoxTreeViewSelectedObject;
}
}

View File

@ -1,3 +1,5 @@
using Components.Exceptions;
namespace WinFormsApp
{
public partial class Form : System.Windows.Forms.Form
@ -24,5 +26,65 @@ namespace WinFormsApp
{
userControlCheckedList.ClearCheckedListBoxValues();
}
private void buttonFillTreeView_Click(object sender, EventArgs e)
{
List<Employee> employees = new List<Employee>();
employees.Add(new Employee("Îòäåë ïðîäàæ", "Ìåíåäæåð", "Èâàíîâ", "Ñåìåí"));
employees.Add(new Employee("Îòäåë ìàðêåòèíãà", "Ìàðêåòîëîã", "Êàþìîâà", "Ñâåòëàíà"));
employees.Add(new Employee("Îòäåë ôèíàíñîâ", "Áóõãàëòåð", "Ðàõèìîâà", "Ýëüâèðà"));
employees.Add(new Employee("Îòäåë çàêóïîê", "Ìåíåäæåð", "Ñåìåíîâ", "Ïåòð"));
employees.Add(new Employee("Îòäåë ïðîäàæ", "Ìåíåäæåð", "Èâàíîâ", "Ïàâåë"));
employees.Add(new Employee("Îòäåë ìàðêåòèíãà", "Ìàðêåòîëîã", "Êàðàìûøåâà", "Òàòüÿíà"));
employees.Add(new Employee("Îòäåë ôèíàíñîâ", "Ôèíàíñèñò", "Àáàëîâ", "Àëüáåðò"));
employees.Add(new Employee("Îòäåë çàêóïîê", "Ãëàâíûé ìåíåäæåð", "Ìî÷àëîâ", "Àëåêñåé"));
employees.Add(new Employee("Îòäåë ïðîäàæ", "Ãëàâíûé ìåíåäæåð", "Êóçíåöîâà", "Ìàðèÿ"));
employees.Add(new Employee("Îòäåë ìàðêåòèíãà", "PR-ñïåöèàëèñò", "Êóòÿêîâ", "Ìèõàèë"));
employees.Add(new Employee("Îòäåë ôèíàíñîâ", "Àíàëèòèê", "Àñòàôüåâ", "Ïàâåë"));
employees.Add(new Employee("Îòäåë çàêóïîê", "Àíàëèòèê", "Ñìèðíîâà", "Àíàñòàñèÿ"));
List<(string PropertyName, bool AlwaysCreateBranch)> hierarchy = new();
hierarchy.Add(("Departament", false));
hierarchy.Add(("Position", false));
hierarchy.Add(("Surname", false));
hierarchy.Add(("Name", false));
userControlTreeView.SetHierarchy(hierarchy);
userControlTreeView.SetTreeObjects(employees);
}
private void buttonClearTreeView_Click(object sender, EventArgs e)
{
userControlTreeView.ClearTreeView();
}
private void buttonShowTreeViewNode_Click(object sender, EventArgs e)
{
Employee employee;
try
{
employee = userControlTreeView.GetObjectSelectedNode<Employee>();
textBoxTreeViewSelectedObject.Text = employee.ToString();
}
catch (PropertyNotDeclaratedException ex)
{
MessageBox.Show(ex.Message);
textBoxTreeViewSelectedObject.Text = string.Empty;
}
catch (PropertyNullException ex)
{
MessageBox.Show(ex.Message);
textBoxTreeViewSelectedObject.Text = string.Empty;
}
catch (NotSelectedNodeException ex)
{
MessageBox.Show(ex.Message);
textBoxTreeViewSelectedObject.Text = string.Empty;
}
}
}
}