diff --git a/VolkovLabs/WinFormsLibraryVolkov/CustomInputRangeDate.Designer.cs b/VolkovLabs/WinFormsLibraryVolkov/CustomInputRangeDate.Designer.cs index bf00c1b..946844d 100644 --- a/VolkovLabs/WinFormsLibraryVolkov/CustomInputRangeDate.Designer.cs +++ b/VolkovLabs/WinFormsLibraryVolkov/CustomInputRangeDate.Designer.cs @@ -38,6 +38,9 @@ dateTimePicker.Size = new Size(142, 23); dateTimePicker.TabIndex = 0; dateTimePicker.ValueChanged += dateTimePicker_ValueChanged; + dateTimePicker.Enter += dateTimePicker_Enter; + dateTimePicker.MaxDate = new DateTime(2024, 12, 31, 0, 0, 0, 0); + dateTimePicker.MinDate = new DateTime(2004, 1, 11, 0, 0, 0, 0); // // CustomInputRangeDate // diff --git a/VolkovLabs/WinFormsLibraryVolkov/CustomInputRangeDate.cs b/VolkovLabs/WinFormsLibraryVolkov/CustomInputRangeDate.cs index e7c4159..f4d811d 100644 --- a/VolkovLabs/WinFormsLibraryVolkov/CustomInputRangeDate.cs +++ b/VolkovLabs/WinFormsLibraryVolkov/CustomInputRangeDate.cs @@ -27,6 +27,7 @@ namespace WinFormsLibraryVolkov InitializeComponent(); } + //Ошибка public string Error { get; protected set; } = string.Empty; //Публичное свойство для установки и получения введенного значения(set, get). diff --git a/VolkovLabs/WinFormsLibraryVolkov/SelectionListBox.Designer.cs b/VolkovLabs/WinFormsLibraryVolkov/CustomSelectedCheckedListBox.Designer.cs similarity index 95% rename from VolkovLabs/WinFormsLibraryVolkov/SelectionListBox.Designer.cs rename to VolkovLabs/WinFormsLibraryVolkov/CustomSelectedCheckedListBox.Designer.cs index 1f6f6d0..f49b90e 100644 --- a/VolkovLabs/WinFormsLibraryVolkov/SelectionListBox.Designer.cs +++ b/VolkovLabs/WinFormsLibraryVolkov/CustomSelectedCheckedListBox.Designer.cs @@ -1,6 +1,6 @@ namespace WinFormsLibraryVolkov { - partial class SelectionListBox + partial class CustomSelectedCheckedListBox { /// /// Обязательная переменная конструктора. @@ -39,7 +39,7 @@ checkedListBox.Size = new Size(144, 148); checkedListBox.TabIndex = 0; checkedListBox.Dock = DockStyle.Fill; - checkedListBox.ItemCheck += new System.Windows.Forms.ItemCheckEventHandler(this.CheckedListBox_ItemCheck); + checkedListBox.ItemCheck += new System.Windows.Forms.ItemCheckEventHandler(this.checkedListBox_ItemCheck); checkedListBox.SelectedIndexChanged += new System.EventHandler(this.checkedListBox_SelectedIndexChanged); // // SelectionListBox diff --git a/VolkovLabs/WinFormsLibraryVolkov/CustomSelectedCheckedListBox.cs b/VolkovLabs/WinFormsLibraryVolkov/CustomSelectedCheckedListBox.cs new file mode 100644 index 0000000..9c3d3e8 --- /dev/null +++ b/VolkovLabs/WinFormsLibraryVolkov/CustomSelectedCheckedListBox.cs @@ -0,0 +1,113 @@ +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 WinFormsLibraryVolkov +{ + public partial class CustomSelectedCheckedListBox : UserControl + { + // Приватный список для хранения элементов + private List itemsList = new List(); + + public event EventHandler _changeEvent; + + public CustomSelectedCheckedListBox() + { + InitializeComponent(); + } + + // Метод для заполнения списка строками + public void PopulateList(List items) + { + if (items == null) + throw new ArgumentNullException(nameof(items)); + + foreach (var item in items) + { + if (!itemsList.Contains(item)) + { + itemsList.Add(item); + checkedListBox.Items.Add(item, false); + } + } + } + + public void AddItem(string newItem) + { + if (!itemsList.Contains(newItem)) + { + itemsList.Add(newItem); + UpdateCheckedListBox(); + } + } + + // Метод для обновления CheckedListBox на основе itemsList + private void UpdateCheckedListBox() + { + checkedListBox.Items.Clear(); + foreach (var item in itemsList) + { + checkedListBox.Items.Add(item, false); + } + } + + //Cобытие, вызываемое при смене значения в CheckedListBox + public event EventHandler ChangeEvent + { + add + { + _changeEvent += value; + } + remove + { + _changeEvent -= value; + } + + } + + // Метод для очистки списка + public void ClearList() + { + checkedListBox.Items.Clear(); + itemsList.Clear(); + } + + // Публичное свойство для получения и установки выбранного значения + public string SelectedElement + { + get + { + return (checkedListBox.SelectedIndex > -1 && checkedListBox.GetItemChecked(checkedListBox.SelectedIndex)) ? checkedListBox.SelectedItem.ToString() : string.Empty; + } + set + { + if (checkedListBox.Items.Contains(value)) + { + checkedListBox.SelectedItem = value; + } + } + } + + // Обработчик изменения состояния элементов + private void checkedListBox_ItemCheck(object sender, ItemCheckEventArgs e) + { + if (e.NewValue == CheckState.Checked && checkedListBox.CheckedItems.Count > 0) + { + checkedListBox.ItemCheck -= checkedListBox_ItemCheck; + checkedListBox.SetItemChecked(checkedListBox.CheckedIndices[0], value: false); + checkedListBox.ItemCheck += checkedListBox_ItemCheck; + } + } + + private void checkedListBox_SelectedIndexChanged(object sender, EventArgs e) + { + _changeEvent?.Invoke(sender, e); + } + } +} diff --git a/VolkovLabs/WinFormsLibraryVolkov/SelectionListBox.resx b/VolkovLabs/WinFormsLibraryVolkov/CustomSelectedCheckedListBox.resx similarity index 100% rename from VolkovLabs/WinFormsLibraryVolkov/SelectionListBox.resx rename to VolkovLabs/WinFormsLibraryVolkov/CustomSelectedCheckedListBox.resx diff --git a/VolkovLabs/WinFormsLibraryVolkov/CustomTreeCell.cs b/VolkovLabs/WinFormsLibraryVolkov/CustomTreeCell.cs index fc1b082..5ab9bfe 100644 --- a/VolkovLabs/WinFormsLibraryVolkov/CustomTreeCell.cs +++ b/VolkovLabs/WinFormsLibraryVolkov/CustomTreeCell.cs @@ -34,6 +34,44 @@ namespace WinFormsLibraryVolkov } } + // Обновленный метод для добавления одного элемента в дерево + public void AddCell(int columnIndex, T element) + { + if (Levels == null || element == null || columnIndex < 0 || columnIndex >= Levels.NodeNames.Count) + { + return; + } + + TreeNodeCollection treeNodeCollection = treeView.Nodes; + int num = 0; + foreach (string nodeName in Levels.NodeNames) + { + // Получение значения свойства или поля + PropertyInfo property = element.GetType().GetProperty(nodeName); + string text = property?.GetValue(element, null)?.ToString() ?? nodeName; + + TreeNode treeNode = null; + foreach (TreeNode item in treeNodeCollection) + { + if (item.Text == text) + { + treeNode = item; + break; + } + } + + // Если узел не найден, добавляем новый + treeNodeCollection = (treeNode == null) ? treeNodeCollection.Add(text).Nodes : treeNode.Nodes; + + if (num >= columnIndex) + { + break; + } + + num++; + } + } + public T GetSelectedObject() where T : class, new() { if (treeView.SelectedNode == null || Levels == null || treeView.SelectedNode.Nodes.Count > 0) diff --git a/VolkovLabs/WinFormsLibraryVolkov/SelectionListBox.cs b/VolkovLabs/WinFormsLibraryVolkov/SelectionListBox.cs deleted file mode 100644 index b9ddbe9..0000000 --- a/VolkovLabs/WinFormsLibraryVolkov/SelectionListBox.cs +++ /dev/null @@ -1,101 +0,0 @@ -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 WinFormsLibraryVolkov -{ - public partial class SelectionListBox : UserControl - { - public event EventHandler _changeEvent; - - public SelectionListBox() - { - InitializeComponent(); - } - - // Метод для заполнения списка строками - public void PopulateList(IEnumerable items) - { - if (items == null) - throw new ArgumentNullException(nameof(items)); - - checkedListBox.Items.Clear(); - foreach (var item in items) - { - checkedListBox.Items.Add(item, false); - } - } - - // Метод для очистки списка - public void ClearList() - { - checkedListBox.Items.Clear(); - } - - // Публичное свойство для получения и установки выбранного значения - public string SelectedValue - { - get - { - foreach (var item in checkedListBox.CheckedItems) - { - return item.ToString(); - } - return string.Empty; - } - set - { - bool found = false; - for (int i = 0; i < checkedListBox.Items.Count; i++) - { - if (checkedListBox.Items[i].ToString() == value) - { - checkedListBox.SetItemChecked(i, true); - found = true; - } - else - { - checkedListBox.SetItemChecked(i, false); - } - } - if (!found) - { - // Если значение не найдено, очистить выбор - foreach (int index in checkedListBox.CheckedIndices) - { - checkedListBox.SetItemChecked(index, false); - } - } - } - } - - // Обработчик изменения состояния элементов - private void CheckedListBox_ItemCheck(object sender, ItemCheckEventArgs e) - { - // Используем BeginInvoke, чтобы дождаться завершения изменения - this.BeginInvoke((MethodInvoker)delegate - { - // Убедимся, что только один элемент выбран - for (int i = 0; i < checkedListBox.Items.Count; i++) - { - if (i != e.Index) - { - checkedListBox.SetItemChecked(i, false); - } - } - // Вызов события - _changeEvent?.Invoke(this, EventArgs.Empty); - }); - } - private void checkedListBox_SelectedIndexChanged(object sender, EventArgs e) - { - _changeEvent?.Invoke(sender, e); - } - } -} diff --git a/VolkovLabs/WinFormsTestApp/FormMain.Designer.cs b/VolkovLabs/WinFormsTestApp/FormMain.Designer.cs index d906e30..9b50949 100644 --- a/VolkovLabs/WinFormsTestApp/FormMain.Designer.cs +++ b/VolkovLabs/WinFormsTestApp/FormMain.Designer.cs @@ -1,4 +1,6 @@ -namespace WinFormsTestApp +using WinFormsLibraryVolkov; + +namespace WinFormsTestApp { partial class FormMain { @@ -38,7 +40,7 @@ buttonClear = new Button(); buttonAdd = new Button(); textBoxAdd = new TextBox(); - SelectionListBox = new WinFormsLibraryVolkov.SelectionListBox(); + customSelectedCheckedListBox = new WinFormsLibraryVolkov.CustomSelectedCheckedListBox(); groupBoxData = new GroupBox(); labelTransportType = new Label(); labelModel = new Label(); @@ -62,7 +64,7 @@ // customInputRangeDate.AutoValidate = AutoValidate.Disable; customInputRangeDate.CausesValidation = false; - customInputRangeDate.Date = new DateTime(2024, 9, 1, 0, 0, 0, 0); + customInputRangeDate.Date = new DateTime(2024, 9, 4, 18, 20, 42, 32); customInputRangeDate.Location = new Point(15, 19); customInputRangeDate.Margin = new Padding(3, 4, 3, 4); customInputRangeDate.MaxDate = new DateTime(2024, 12, 31, 0, 0, 0, 0); @@ -109,7 +111,7 @@ groupBoxSelected.Controls.Add(buttonClear); groupBoxSelected.Controls.Add(buttonAdd); groupBoxSelected.Controls.Add(textBoxAdd); - groupBoxSelected.Controls.Add(SelectionListBox); + groupBoxSelected.Controls.Add(customSelectedCheckedListBox); groupBoxSelected.Location = new Point(324, 6); groupBoxSelected.Name = "groupBoxSelected"; groupBoxSelected.Size = new Size(311, 190); @@ -119,7 +121,7 @@ // // buttonGetSelected // - buttonGetSelected.Location = new Point(192, 144); + buttonGetSelected.Location = new Point(174, 139); buttonGetSelected.Name = "buttonGetSelected"; buttonGetSelected.Size = new Size(100, 23); buttonGetSelected.TabIndex = 14; @@ -130,7 +132,7 @@ // labelSelectedValue // labelSelectedValue.AutoSize = true; - labelSelectedValue.Location = new Point(192, 111); + labelSelectedValue.Location = new Point(178, 111); labelSelectedValue.Name = "labelSelectedValue"; labelSelectedValue.Size = new Size(82, 15); labelSelectedValue.TabIndex = 11; @@ -138,7 +140,7 @@ // // buttonClear // - buttonClear.Location = new Point(192, 81); + buttonClear.Location = new Point(174, 81); buttonClear.Name = "buttonClear"; buttonClear.Size = new Size(100, 23); buttonClear.TabIndex = 13; @@ -148,7 +150,7 @@ // // buttonAdd // - buttonAdd.Location = new Point(192, 52); + buttonAdd.Location = new Point(174, 52); buttonAdd.Name = "buttonAdd"; buttonAdd.Size = new Size(100, 23); buttonAdd.TabIndex = 12; @@ -158,19 +160,19 @@ // // textBoxAdd // - textBoxAdd.Location = new Point(192, 23); + textBoxAdd.Location = new Point(162, 23); textBoxAdd.Name = "textBoxAdd"; - textBoxAdd.Size = new Size(100, 23); + textBoxAdd.Size = new Size(140, 23); textBoxAdd.TabIndex = 11; // - // SelectionListBox + // customSelectedCheckedListBox // - SelectionListBox.Location = new Point(36, 19); - SelectionListBox.Margin = new Padding(3, 4, 3, 4); - SelectionListBox.Name = "SelectionListBox"; - SelectionListBox.SelectedValue = ""; - SelectionListBox.Size = new Size(150, 157); - SelectionListBox.TabIndex = 0; + customSelectedCheckedListBox.Location = new Point(6, 19); + customSelectedCheckedListBox.Margin = new Padding(3, 4, 3, 4); + customSelectedCheckedListBox.Name = "customSelectedCheckedListBox"; + customSelectedCheckedListBox.SelectedElement = ""; + customSelectedCheckedListBox.Size = new Size(150, 157); + customSelectedCheckedListBox.TabIndex = 0; // // groupBoxData // @@ -283,7 +285,7 @@ Visual.Controls.Add(groupBoxSelected); Visual.Location = new Point(4, 24); Visual.Name = "Visual"; - Visual.Padding = new Padding(3, 3, 3, 3); + Visual.Padding = new Padding(3); Visual.Size = new Size(645, 438); Visual.TabIndex = 0; Visual.Text = "Visual"; @@ -320,7 +322,7 @@ private Button buttonClear; private Button buttonAdd; private TextBox textBoxAdd; - private WinFormsLibraryVolkov.SelectionListBox SelectionListBox; + private WinFormsLibraryVolkov.CustomSelectedCheckedListBox customSelectedCheckedListBox; private GroupBox groupBoxData; private WinFormsLibraryVolkov.CustomTreeCell customTreeCell; private Button buttonGetFromTree; diff --git a/VolkovLabs/WinFormsTestApp/FormMain.cs b/VolkovLabs/WinFormsTestApp/FormMain.cs index 96a0dd8..761e575 100644 --- a/VolkovLabs/WinFormsTestApp/FormMain.cs +++ b/VolkovLabs/WinFormsTestApp/FormMain.cs @@ -25,7 +25,7 @@ namespace WinFormsTestApp { InitializeComponent(); var list = new List() { "Значение 1", "Значение 2", "Значение 3", "Значение 4", "Значение 5" }; - SelectionListBox.PopulateList(list); + customSelectedCheckedListBox.PopulateList(list); comboBoxTransportType.Items.Add("Наземный транспорт"); comboBoxTransportType.Items.Add("Воздушный транспорт"); @@ -54,7 +54,11 @@ namespace WinFormsTestApp private void buttonCheck_Click(object sender, EventArgs e) { - // доделать + labelCheckValue.Text = customInputRangeDate.Date.ToString(); + if (labelCheckValue.Text == "") + { + labelCheckValue.Text = customInputRangeDate.Error; + } } private void textBoxMin_KeyPress(object sender, KeyPressEventArgs e) @@ -77,17 +81,33 @@ namespace WinFormsTestApp private void buttonAdd_Click(object sender, EventArgs e) { - + string newItem = textBoxAdd.Text.Trim(); + + if (!string.IsNullOrEmpty(newItem)) + { + if (!customSelectedCheckedListBox.SelectedElement.Equals(newItem)) + { + if (customSelectedCheckedListBox.SelectedElement != string.Empty) + { + customSelectedCheckedListBox.SelectedElement = newItem; + } + else + { + customSelectedCheckedListBox.AddItem(newItem); + customSelectedCheckedListBox.SelectedElement = newItem; + } + } + } } private void buttonClear_Click(object sender, EventArgs e) { - SelectionListBox.ClearList(); + customSelectedCheckedListBox.ClearList(); } private void buttonGetSelected_Click(object sender, EventArgs e) { - labelSelectedValue.Text = SelectionListBox.SelectedValue; + labelSelectedValue.Text = customSelectedCheckedListBox.SelectedElement; if (labelSelectedValue.Text == "") { labelSelectedValue.Text = "Значение \nне выбрано";