Вроде

This commit is contained in:
Алексей Тихоненков 2024-09-05 15:05:04 +04:00
parent 5a901fc072
commit 079f196dcf
10 changed files with 496 additions and 103 deletions

View File

@ -20,77 +20,143 @@
base.Dispose(disposing); base.Dispose(disposing);
} }
#region Windows Form Designer generated code #region Windows Form Designer generated code
/// <summary> /// <summary>
/// Required method for Designer support - do not modify /// Required method for Designer support - do not modify
/// the contents of this method with the code editor. /// the contents of this method with the code editor.
/// </summary> /// </summary>
private void InitializeComponent() private void InitializeComponent()
{ {
userCheckedListBox = new WinFormsLibrary1.UserCheckedListBox(); userCheckedListBox = new WinFormsLibrary1.UserCheckedListBox();
buttonClear = new Button(); buttonClear = new Button();
ButtonShowItems = new Button(); ButtonShowItems = new Button();
textBoxItems = new TextBox(); textBoxItems = new TextBox();
SuspendLayout(); userDateTimePicker = new COP.UserDateTimePicker();
// textBoxShowDate = new TextBox();
// userCheckedListBox ButtonShowDate = new Button();
// ButtonRangeSet = new Button();
userCheckedListBox.Location = new Point(0, 0); userTreeView = new COP.UserTreeView();
userCheckedListBox.Name = "userCheckedListBox"; SuspendLayout();
userCheckedListBox.SelectedValue = ""; //
userCheckedListBox.Size = new Size(263, 170); // userCheckedListBox
userCheckedListBox.TabIndex = 0; //
userCheckedListBox.SelectedValueChanged += UserCheckedListBox_SelectedValueChanged; userCheckedListBox.Location = new Point(0, 0);
// userCheckedListBox.Margin = new Padding(3, 2, 3, 2);
// buttonClear userCheckedListBox.Name = "userCheckedListBox";
// userCheckedListBox.SelectedValue = "";
buttonClear.Location = new Point(0, 176); userCheckedListBox.Size = new Size(230, 128);
buttonClear.Name = "buttonClear"; userCheckedListBox.TabIndex = 0;
buttonClear.Size = new Size(263, 29); userCheckedListBox.SelectedValueChanged += UserCheckedListBox_SelectedValueChanged;
buttonClear.TabIndex = 2; //
buttonClear.Text = "Очистить"; // buttonClear
buttonClear.UseVisualStyleBackColor = true; //
buttonClear.Click += buttonClear_Click; buttonClear.Location = new Point(0, 132);
// buttonClear.Margin = new Padding(3, 2, 3, 2);
// ButtonShowItems buttonClear.Name = "buttonClear";
// buttonClear.Size = new Size(230, 22);
ButtonShowItems.Location = new Point(0, 211); buttonClear.TabIndex = 2;
ButtonShowItems.Name = "ButtonShowItems"; buttonClear.Text = "Очистить";
ButtonShowItems.Size = new Size(263, 29); buttonClear.UseVisualStyleBackColor = true;
ButtonShowItems.TabIndex = 3; buttonClear.Click += buttonClear_Click;
ButtonShowItems.Text = "Показать"; //
ButtonShowItems.UseVisualStyleBackColor = true; // ButtonShowItems
ButtonShowItems.Click += ButtonShowItems_Click; //
// ButtonShowItems.Location = new Point(0, 158);
// textBoxItems ButtonShowItems.Margin = new Padding(3, 2, 3, 2);
// ButtonShowItems.Name = "ButtonShowItems";
textBoxItems.Location = new Point(0, 246); ButtonShowItems.Size = new Size(230, 22);
textBoxItems.Multiline = true; ButtonShowItems.TabIndex = 3;
textBoxItems.Name = "textBoxItems"; ButtonShowItems.Text = "Показать";
textBoxItems.Size = new Size(263, 87); ButtonShowItems.UseVisualStyleBackColor = true;
textBoxItems.TabIndex = 4; ButtonShowItems.Click += ButtonShowItems_Click;
// //
// FormTestComponents // textBoxItems
// //
AutoScaleDimensions = new SizeF(8F, 20F); textBoxItems.Location = new Point(0, 184);
AutoScaleMode = AutoScaleMode.Font; textBoxItems.Margin = new Padding(3, 2, 3, 2);
ClientSize = new Size(800, 450); textBoxItems.Multiline = true;
Controls.Add(textBoxItems); textBoxItems.Name = "textBoxItems";
Controls.Add(ButtonShowItems); textBoxItems.Size = new Size(231, 66);
Controls.Add(buttonClear); textBoxItems.TabIndex = 4;
Controls.Add(userCheckedListBox); //
Name = "FormTestComponents"; // userDateTimePicker
Text = "FormTestComponents "; //
ResumeLayout(false); userDateTimePicker.Location = new Point(258, 11);
PerformLayout(); userDateTimePicker.Margin = new Padding(3, 2, 3, 2);
} userDateTimePicker.MaxValue = null;
userDateTimePicker.MinValue = null;
userDateTimePicker.Name = "userDateTimePicker";
userDateTimePicker.Size = new Size(222, 22);
userDateTimePicker.TabIndex = 5;
userDateTimePicker.ValueChanged += UserDateTimePicker_ValueChanged;
//
// textBoxShowDate
//
textBoxShowDate.Location = new Point(258, 58);
textBoxShowDate.Name = "textBoxShowDate";
textBoxShowDate.Size = new Size(222, 23);
textBoxShowDate.TabIndex = 6;
//
// ButtonShowDate
//
ButtonShowDate.Location = new Point(290, 105);
ButtonShowDate.Name = "ButtonShowDate";
ButtonShowDate.Size = new Size(168, 23);
ButtonShowDate.TabIndex = 7;
ButtonShowDate.Text = "Показать";
ButtonShowDate.UseVisualStyleBackColor = true;
ButtonShowDate.Click += ButtonShowDate_Click;
//
// ButtonRangeSet
//
ButtonRangeSet.Location = new Point(486, 47);
ButtonRangeSet.Name = "ButtonRangeSet";
ButtonRangeSet.Size = new Size(131, 43);
ButtonRangeSet.TabIndex = 8;
ButtonRangeSet.Text = "Установить диапозон";
ButtonRangeSet.UseVisualStyleBackColor = true;
ButtonRangeSet.Click += ButtonRangeSet_Click;
//
// userTreeView
//
userTreeView.Location = new Point(454, 158);
userTreeView.Name = "userTreeView";
userTreeView.Size = new Size(150, 95);
userTreeView.TabIndex = 9;
userTreeView.Load += UserTreeView_Load;
//
// FormTestComponents
//
AutoScaleDimensions = new SizeF(7F, 15F);
AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(700, 338);
Controls.Add(userTreeView);
Controls.Add(ButtonRangeSet);
Controls.Add(ButtonShowDate);
Controls.Add(textBoxShowDate);
Controls.Add(userDateTimePicker);
Controls.Add(textBoxItems);
Controls.Add(ButtonShowItems);
Controls.Add(buttonClear);
Controls.Add(userCheckedListBox);
Margin = new Padding(3, 2, 3, 2);
Name = "FormTestComponents";
Text = "FormTestComponents ";
ResumeLayout(false);
PerformLayout();
}
#endregion #endregion
private WinFormsLibrary1.UserCheckedListBox userCheckedListBox; private WinFormsLibrary1.UserCheckedListBox userCheckedListBox;
private Button buttonClear; private Button buttonClear;
private Button ButtonShowItems; private Button ButtonShowItems;
private TextBox textBoxItems; private TextBox textBoxItems;
} private COP.UserDateTimePicker userDateTimePicker;
private TextBox textBoxShowDate;
private Button ButtonShowDate;
private Button ButtonRangeSet;
private COP.UserTreeView userTreeView;
}
} }

View File

@ -1,29 +1,85 @@
using COP;
namespace TestsComponents namespace TestsComponents
{ {
public partial class FormTestComponents : Form public partial class FormTestComponents : Form
{ {
public FormTestComponents() public FormTestComponents()
{ {
InitializeComponent(); InitializeComponent();
Fill(); Fill();
} }
public void Fill() public void Fill()
{ {
userCheckedListBox.append(new() { "items1", "dsdsf", "items3", "fgdg", "iit" }); userCheckedListBox.append(new() { "items1", "dsdsf", "items3", "fgdg", "iit" });
} }
private void UserCheckedListBox_SelectedValueChanged(object sender, EventArgs e) private void UserCheckedListBox_SelectedValueChanged(object sender, EventArgs e)
{ {
MessageBox.Show("Selected value changed");
} MessageBox.Show("Selected value changed");
private void buttonClear_Click(object sender, EventArgs e)
{ }
userCheckedListBox.ClearList(); private void buttonClear_Click(object sender, EventArgs e)
} {
private void ButtonShowItems_Click(object sender, EventArgs e) userCheckedListBox.ClearList();
{ }
private void ButtonShowItems_Click(object sender, EventArgs e)
textBoxItems.Text = userCheckedListBox.SelectedValue; {
}
textBoxItems.Text = userCheckedListBox.SelectedValue;
} }
private void ButtonShowDate_Click(object sender, EventArgs e)
{
try
{
textBoxShowDate.Clear();
textBoxShowDate.Text = userDateTimePicker.SelectedValue.ToString();
}
catch (Exception ex)
{
MessageBox.Show("Îøèáêà");
}
}
private void UserDateTimePicker_ValueChanged(object sender, EventArgs e)
{
MessageBox.Show("Value changed");
DateTime? selectedDate = userDateTimePicker.SelectedValue;
}
private void ButtonRangeSet_Click(object sender, EventArgs e)
{
userDateTimePicker.MaxValue = new DateTime(2024, 9, 20);
userDateTimePicker.MinValue = new DateTime(2024,8,10);
}
private void UserTreeView_Load(object sender, EventArgs e)
{
List<Employee> employees = new List<Employee>
{
new Employee { Department = "Îòäåë1", Position = "Äîëæíîñòü1", Name = "Ñîòðóäíèê1" },
new Employee { Department = "Îòäåë2", Position = "Äîëæíîñòü2", Name = "Ñîòðóäíèê2" },
new Employee { Department = "Îòäåë1", Position = "Äîëæíîñòü1", Name = "Ñîòðóäíèê3" },
new Employee { Department = "Îòäåë2", Position = "Äîëæíîñòü3", Name = "Ñîòðóäíèê4" },
};
var Hierarchy = new List<string> { "Department", "Position", "Name" };
foreach (var prop in employees[0].GetType().GetProperties())
{
userTreeView.Hierarchy.Add(prop.Name);
}
try
{
userTreeView.PopulateTree(employees);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
public class Employee
{
public string? Department { get; set; }
public string? Position { get; set; }
public string? Name { get; set; }
}
}
} }

View File

@ -8,7 +8,7 @@ using System.Threading.Tasks;
namespace COP.Exceptions namespace COP.Exceptions
{ {
[Serializable] [Serializable]
public class NoRangeException : ApplicationException public class NoRangeException : Exception
{ {
public NoRangeException() : base() { } public NoRangeException() : base() { }
public NoRangeException(string message) : base(message) { } public NoRangeException(string message) : base(message) { }

View File

@ -8,7 +8,7 @@ using System.Threading.Tasks;
namespace COP.Exceptions namespace COP.Exceptions
{ {
[Serializable] [Serializable]
public class NotIncludedInRangeException: ApplicationException public class NotIncludedInRangeException: Exception
{ {
public NotIncludedInRangeException() : base() { } public NotIncludedInRangeException() : base() { }

View File

@ -1,6 +1,6 @@
namespace COP namespace COP
{ {
partial class UserDateTimePickercs partial class UserDateTimePicker
{ {
/// <summary> /// <summary>
/// Обязательная переменная конструктора. /// Обязательная переменная конструктора.

View File

@ -11,13 +11,13 @@ using COP.Exceptions;
namespace COP namespace COP
{ {
public partial class UserDateTimePickercs : UserControl public partial class UserDateTimePicker : UserControl
{ {
private DateTime? minValue; private DateTime? minValue;
private DateTime? maxValue; private DateTime? maxValue;
public event EventHandler? _valueChanged; public event EventHandler? _valueChanged;
public UserDateTimePickercs() public UserDateTimePicker()
{ {
InitializeComponent(); InitializeComponent();
@ -44,9 +44,9 @@ namespace COP
else if (dateTimePicker.Value < minValue || else if (dateTimePicker.Value < minValue ||
dateTimePicker.Value > maxValue) dateTimePicker.Value > maxValue)
{ {
throw new NotIncludedInRangeException("Selected value is out of range.");
return null;
} }
return dateTimePicker.Value; return dateTimePicker.Value;
} }
@ -60,8 +60,9 @@ namespace COP
if (value < minValue || value > maxValue) if (value < minValue || value > maxValue)
{ {
throw new NotIncludedInRangeException("Selected value is out of range."); return;
}
}
dateTimePicker.Value = value.Value; dateTimePicker.Value = value.Value;

View File

@ -0,0 +1,55 @@
namespace COP
{
partial class UserTreeView
{
/// <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(0, 0);
treeView.Name = "treeView";
treeView.Size = new Size(150, 97);
treeView.TabIndex = 0;
//
// UserTreeView
//
AutoScaleDimensions = new SizeF(7F, 15F);
AutoScaleMode = AutoScaleMode.Font;
Controls.Add(treeView);
Name = "UserTreeView";
Size = new Size(150, 95);
ResumeLayout(false);
}
#endregion
private TreeView treeView;
}
}

View File

@ -0,0 +1,95 @@
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 COP
{
public partial class UserTreeView : UserControl
{
public List<string> Hierarchy;
public UserTreeView()
{
InitializeComponent();
Hierarchy = new();
}
public int SelectedItemIndex
{
get
{
if (treeView.SelectedNode == null)
{
return -1;
}
return treeView.SelectedNode.Index;
}
}
public T? GetSelectedObject<T>() where T : class, new()
{
// параметризованный
//проверка есть или нет иерархии
TreeNode selectedNode = treeView.SelectedNode;
if (Hierarchy == null)
{
throw new Exception("Hierarchy is not set.");
}
if (selectedNode?.FirstNode != null)
{
throw new Exception("Not a last level node.");
}
T currentBranch = new T();
Hierarchy.Reverse();
foreach (var property in Hierarchy)
{
typeof(T).GetProperty(property).SetValue(currentBranch, Convert.ChangeType(selectedNode.Text, typeof(T).GetProperty(property).PropertyType));
selectedNode = selectedNode.Parent;
}
Hierarchy.Reverse();
return currentBranch;
}
public void PopulateTree<T>(List<T> items)
{
treeView.BeginUpdate();
treeView.Nodes.Clear();
if (items == null)
{
throw new ArgumentNullException(nameof(items));
}
if (Hierarchy == null)
{
throw new Exception("Hierarchy is not set.");
}
foreach (var item in items)
{
TreeNodeCollection treeViewNodes = treeView.Nodes;
foreach (var elem in Hierarchy)
{
bool flag = false;
for (int i = 0; i < treeViewNodes.Count; i++)
{
if (item.GetType().GetProperty(elem).GetValue(item).ToString() == treeViewNodes[i].Text)
{
treeViewNodes = treeViewNodes[i].Nodes;
flag = true;
break;
}
}
if (!flag)
{
var newNode = treeViewNodes.Add(item.GetType().GetProperty(elem).GetValue(item).ToString());
treeViewNodes = newNode.Nodes;
}
}
}
treeView.EndUpdate();
}
}
}

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>