From a24dbe6e5b0a952aa4cb399df0d01b7d39b32ed9 Mon Sep 17 00:00:00 2001 From: GokaPek Date: Tue, 17 Sep 2024 12:40:52 +0400 Subject: [PATCH] Final --- ExecForm/Form1.Designer.cs | 4 ++- Library14Petrushin/ChooseList.Designer.cs | 1 + Library14Petrushin/ChooseList.cs | 13 +++++++++ Library14Petrushin/HierarchicalTreeView.cs | 30 ++++++++++----------- Library14Petrushin/TextBoxRange.Designer.cs | 14 +++++++++- Library14Petrushin/TextBoxRange.cs | 26 ++++++------------ 6 files changed, 52 insertions(+), 36 deletions(-) diff --git a/ExecForm/Form1.Designer.cs b/ExecForm/Form1.Designer.cs index 78a1321..ce66285 100644 --- a/ExecForm/Form1.Designer.cs +++ b/ExecForm/Form1.Designer.cs @@ -52,8 +52,10 @@ // textBoxRange1 // textBoxRange1.Location = new Point(254, 21); + textBoxRange1.MaxLength = null; + textBoxRange1.MinLength = null; textBoxRange1.Name = "textBoxRange1"; - textBoxRange1.Size = new Size(259, 66); + textBoxRange1.Size = new Size(259, 118); textBoxRange1.TabIndex = 3; // // Form1 diff --git a/Library14Petrushin/ChooseList.Designer.cs b/Library14Petrushin/ChooseList.Designer.cs index 3a4c7a8..63ffc86 100644 --- a/Library14Petrushin/ChooseList.Designer.cs +++ b/Library14Petrushin/ChooseList.Designer.cs @@ -38,6 +38,7 @@ checkedListBox1.Name = "checkedListBox1"; checkedListBox1.Size = new Size(179, 180); checkedListBox1.TabIndex = 0; + checkedListBox1.ItemCheck += CheckedListBox1_ItemCheck; checkedListBox1.SelectedIndexChanged += SelectedIndexChanged; // // ChooseList diff --git a/Library14Petrushin/ChooseList.cs b/Library14Petrushin/ChooseList.cs index eb77cff..5ab5ca4 100644 --- a/Library14Petrushin/ChooseList.cs +++ b/Library14Petrushin/ChooseList.cs @@ -49,6 +49,19 @@ namespace Library14Petrushin { SelectedValueChanged?.Invoke(this, e); } + private void CheckedListBox1_ItemCheck(object sender, ItemCheckEventArgs e) + { + if (e.NewValue == CheckState.Checked) + { + for (int i = 0; i < checkedListBox1.Items.Count; i++) + { + if (i != e.Index) + { + checkedListBox1.SetItemChecked(i, false); + } + } + } + } public void ClearComboBox() { diff --git a/Library14Petrushin/HierarchicalTreeView.cs b/Library14Petrushin/HierarchicalTreeView.cs index 51e7592..ccca4b6 100644 --- a/Library14Petrushin/HierarchicalTreeView.cs +++ b/Library14Petrushin/HierarchicalTreeView.cs @@ -29,27 +29,21 @@ namespace Library14Petrushin this.alwaysNewBranch = alwaysNewBranch; } - public void AddObjectToTree(object obj, string stopAtProperty) + public void AddObjectToTree(T obj, string stopAtProperty) { - TreeNode currentNode = treeView1.Nodes.Cast().FirstOrDefault(); + TreeNode currentNode = treeView1.Nodes.Count > 0 ? treeView1.Nodes[0] : treeView1.Nodes.Add(""); + foreach (var property in hierarchy) { var value = obj.GetType().GetProperty(property).GetValue(obj, null).ToString(); bool createNewBranch = alwaysNewBranch.ContainsKey(property) && alwaysNewBranch[property]; - if (currentNode == null) + var childNode = currentNode.Nodes.Cast().FirstOrDefault(n => n.Text == value); + if (childNode == null || createNewBranch) { - currentNode = treeView1.Nodes.Add(value); - } - else - { - var childNode = currentNode.Nodes.Cast().FirstOrDefault(n => n.Text == value); - if (childNode == null || createNewBranch) - { - childNode = currentNode.Nodes.Add(value); - } - currentNode = childNode; + childNode = currentNode.Nodes.Add(value); } + currentNode = childNode; if (property == stopAtProperty) { @@ -73,15 +67,19 @@ namespace Library14Petrushin } } - public object GetSelectedObject() + public T GetSelectedObject() where T : new() { if (treeView1.SelectedNode == null) { - return null; + throw new InvalidOperationException("Узел не выбран"); } var node = treeView1.SelectedNode; - var obj = Activator.CreateInstance(typeof(Employee)); + if (node.Nodes.Count != 0) + { + throw new InvalidOperationException("Узел не конечный, сформировать класс нельзя"); + } + var obj = new T(); foreach (var property in hierarchy) { var value = node.Text; diff --git a/Library14Petrushin/TextBoxRange.Designer.cs b/Library14Petrushin/TextBoxRange.Designer.cs index cd8a8db..e78bd66 100644 --- a/Library14Petrushin/TextBoxRange.Designer.cs +++ b/Library14Petrushin/TextBoxRange.Designer.cs @@ -29,6 +29,7 @@ private void InitializeComponent() { textBox1 = new TextBox(); + label1 = new Label(); SuspendLayout(); // // textBox1 @@ -38,13 +39,23 @@ textBox1.Size = new Size(243, 27); textBox1.TabIndex = 0; // + // label1 + // + label1.AutoSize = true; + label1.Location = new Point(3, 54); + label1.Name = "label1"; + label1.Size = new Size(50, 20); + label1.TabIndex = 1; + label1.Text = "label1"; + // // TextBoxRange // AutoScaleDimensions = new SizeF(8F, 20F); AutoScaleMode = AutoScaleMode.Font; + Controls.Add(label1); Controls.Add(textBox1); Name = "TextBoxRange"; - Size = new Size(252, 53); + Size = new Size(252, 86); ResumeLayout(false); PerformLayout(); } @@ -52,5 +63,6 @@ #endregion private TextBox textBox1; + private Label label1; } } diff --git a/Library14Petrushin/TextBoxRange.cs b/Library14Petrushin/TextBoxRange.cs index d1e6e94..ddd0778 100644 --- a/Library14Petrushin/TextBoxRange.cs +++ b/Library14Petrushin/TextBoxRange.cs @@ -13,14 +13,14 @@ namespace Library14Petrushin { public partial class TextBoxRange : UserControl { - public int MinLength { get; set; } - public int MaxLength { get; set; } + public int ?MinLength { get; set; } + public int ?MaxLength { get; set; } public event EventHandler? ValueChanged; public string InputValue { get { - if (MinLength == 0 && MaxLength == 0) + if (MinLength == null && MaxLength == null) { throw new RangeException("Диапазон не задан."); } @@ -32,7 +32,7 @@ namespace Library14Petrushin } set { - if (MinLength != 0 && MaxLength != 0) + if (MinLength != null && MaxLength != null) { if (value.Length >= MinLength && value.Length <= MaxLength) { @@ -40,10 +40,6 @@ namespace Library14Petrushin ValueChanged?.Invoke(this, EventArgs.Empty); } } - else - { - throw new RangeException("Диапазон не задан."); - } } } public TextBoxRange() @@ -53,24 +49,18 @@ namespace Library14Petrushin } private void TextBox1_TextChanged(object sender, EventArgs e) { - if (MinLength != 0 && MaxLength != 0) + ValueChanged?.Invoke(this, e); + if (MinLength != null && MaxLength != null) { if (textBox1.Text.Length < MinLength || textBox1.Text.Length > MaxLength) { - // Выделение текста красным цветом, если он не входит в диапазон - textBox1.ForeColor = System.Drawing.Color.Red; + textBox1.ForeColor = Color.Red; } else { - // Возвращение к стандартному цвету текста - textBox1.ForeColor = System.Drawing.Color.Black; - ValueChanged?.Invoke(this, e); + textBox1.ForeColor = Color.Black; } } - else - { - throw new RangeException("Диапазон не задан."); - } } }