From 5b17be92112065c83b83b3c5c1095b25636129ca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D1=80=D1=82=D1=91=D0=BC=20=D0=90=D0=BB=D0=B5=D0=B9?= =?UTF-8?q?=D0=BA=D0=B8=D0=BD?= Date: Wed, 4 Oct 2023 22:43:22 +0400 Subject: [PATCH] =?UTF-8?q?=D0=92=D1=80=D0=BE=D0=B4=D0=B5=20=D1=81=D0=B4?= =?UTF-8?q?=D0=B5=D0=BB=D0=B0=D0=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../WinFormsLibrary/NumberTextBox.cs | 83 +++++------ .../WinFormsLibrary/TreeClass.Designer.cs | 56 ++++++++ WinFormsProject/WinFormsLibrary/TreeClass.cs | 133 ++++++++++++++++++ .../WinFormsLibrary/TreeClass.resx | 60 ++++++++ 4 files changed, 288 insertions(+), 44 deletions(-) create mode 100644 WinFormsProject/WinFormsLibrary/TreeClass.Designer.cs create mode 100644 WinFormsProject/WinFormsLibrary/TreeClass.cs create mode 100644 WinFormsProject/WinFormsLibrary/TreeClass.resx diff --git a/WinFormsProject/WinFormsLibrary/NumberTextBox.cs b/WinFormsProject/WinFormsLibrary/NumberTextBox.cs index 58eb6a8..8dbb2b1 100644 --- a/WinFormsProject/WinFormsLibrary/NumberTextBox.cs +++ b/WinFormsProject/WinFormsLibrary/NumberTextBox.cs @@ -12,8 +12,8 @@ namespace WinFormsLibrary { public partial class NumberTextBox : UserControl { - public int maxValue; - public int minValue; + public int? maxValue = null; + public int? minValue = null; public bool error = false; public NumberTextBox() @@ -22,58 +22,53 @@ namespace WinFormsLibrary } - - //публичное свойство - public string Selected + public int? MinValue { - get - { - if (minValue == null) - { - error = true; - } - - if (maxValue == null) - { - error = true; - } - - if (Check_Value(Convert.ToInt32(numericUpDown.Value))) - { - return ""; - } - return numericUpDown.Value.ToString(); - } + get { return minValue; } set { - if (!Check_Value(Convert.ToInt32(numericUpDown.Value))) + if (value == null) return; + minValue = value; + numericUpDown.Minimum = (int)value; + } + } + + public int? MaxValue + { + get { return maxValue; } + set + { + if (value == null) return; + maxValue = value; + numericUpDown.Maximum = (int)value; + } + } + + public decimal Value + { + get { return numericUpDown.Value; } + set + { + if (MinValue == null || MaxValue == null) { error = true; + return; } - numericUpDown.Value = 0; + + if (value < MinValue || value > MaxValue) + { + error = true; + return; + } + + numericUpDown.Value = value; } } - public void SetMaxValue(int number) // переделать через свойство + public event EventHandler DateChanged { - numericUpDown.Maximum = number; - maxValue = number; - } - - public void SetMinValue(int number) // переделать через свойство - { - numericUpDown.Minimum = number; - minValue = number; - } - - public bool Check_Value(int number) - { - if (number < numericUpDown.Minimum || number > numericUpDown.Maximum) - { - return false; - } - - return true; + add { numericUpDown.ValueChanged += value; } + remove { numericUpDown.ValueChanged -= value; } } } } diff --git a/WinFormsProject/WinFormsLibrary/TreeClass.Designer.cs b/WinFormsProject/WinFormsLibrary/TreeClass.Designer.cs new file mode 100644 index 0000000..698668f --- /dev/null +++ b/WinFormsProject/WinFormsLibrary/TreeClass.Designer.cs @@ -0,0 +1,56 @@ +namespace WinFormsLibrary +{ + partial class TreeClass + { + /// + /// Обязательная переменная конструктора. + /// + 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(292, 224); + this.treeView.TabIndex = 0; + // + // TreeClass + // + this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.Controls.Add(this.treeView); + this.Name = "TreeClass"; + this.Size = new System.Drawing.Size(292, 224); + this.ResumeLayout(false); + + } + + #endregion + + private TreeView treeView; + } +} diff --git a/WinFormsProject/WinFormsLibrary/TreeClass.cs b/WinFormsProject/WinFormsLibrary/TreeClass.cs new file mode 100644 index 0000000..04d2c23 --- /dev/null +++ b/WinFormsProject/WinFormsLibrary/TreeClass.cs @@ -0,0 +1,133 @@ +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 static System.Windows.Forms.VisualStyles.VisualStyleElement; + +namespace WinFormsLibrary +{ + public partial class TreeClass : UserControl + { + private List hierarchy; + public bool hasError = false; + + public TreeClass() + { + InitializeComponent(); + } + + public void setHierarchy(List h) + { + hierarchy = h; + } + + private bool hasValue(TreeNodeCollection nodes, string value) + { + foreach (TreeNode node in nodes) + { + if (node.Text == value) return true; + } + + return false; + } + + private bool addData(T t, string propertyName) + { + TreeNodeCollection current = treeView.Nodes; + + foreach (string h in hierarchy) + { + if (h == propertyName) + { + var field = t.GetType().GetField(h); + + if (field == null) + { + return false; + } + + string value = field.GetValue(t).ToString(); + if (!hasValue(current, value)) current.Add(value); + + TreeNode elem = null; + + foreach (TreeNode child in current) + { + if (child.Text == value) + { + elem = child; + break; + } + } + + if (elem != null) current = elem.Nodes; + } + else + { + if (!hasValue(current, h)) current.Add(h); + + TreeNode elem = null; + + foreach (TreeNode child in current) + { + if (child.Text == h) + { + elem = child; + break; + } + } + + if (elem != null) current = elem.Nodes; + } + } + + return true; + } + + public bool setData(T data, string propertyName) + { + bool status = addData(data, propertyName); + if (!status) return false; + + return true; + } + + public T GetSelectedClass() where T : new() + { + T res = new T(); + TreeNode node = treeView.SelectedNode; + + if (node.Nodes.Count != 0) + { + hasError = true; + return res; + } + + for (int i = hierarchy.Count - 1; i >= 0; i--) + { + if (node == null) + { + hasError = true; + return res; + } + + var curr = res.GetType().GetField(hierarchy[i]); + if (curr == null) + { + hasError = true; + return res; + } + + curr.SetValue(res, node.Text); + node = node.Parent; + } + + return res; + } + } +} diff --git a/WinFormsProject/WinFormsLibrary/TreeClass.resx b/WinFormsProject/WinFormsLibrary/TreeClass.resx new file mode 100644 index 0000000..f298a7b --- /dev/null +++ b/WinFormsProject/WinFormsLibrary/TreeClass.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