Хоть как-то но работает...

This commit is contained in:
Никита Волков 2024-09-04 20:00:33 +04:00
parent c44986349c
commit 5f19fb8329
9 changed files with 203 additions and 127 deletions

View File

@ -38,6 +38,9 @@
dateTimePicker.Size = new Size(142, 23); dateTimePicker.Size = new Size(142, 23);
dateTimePicker.TabIndex = 0; dateTimePicker.TabIndex = 0;
dateTimePicker.ValueChanged += dateTimePicker_ValueChanged; 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 // CustomInputRangeDate
// //

View File

@ -27,6 +27,7 @@ namespace WinFormsLibraryVolkov
InitializeComponent(); InitializeComponent();
} }
//Ошибка
public string Error { get; protected set; } = string.Empty; public string Error { get; protected set; } = string.Empty;
//Публичное свойство для установки и получения введенного значения(set, get). //Публичное свойство для установки и получения введенного значения(set, get).

View File

@ -1,6 +1,6 @@
namespace WinFormsLibraryVolkov namespace WinFormsLibraryVolkov
{ {
partial class SelectionListBox partial class CustomSelectedCheckedListBox
{ {
/// <summary> /// <summary>
/// Обязательная переменная конструктора. /// Обязательная переменная конструктора.
@ -39,7 +39,7 @@
checkedListBox.Size = new Size(144, 148); checkedListBox.Size = new Size(144, 148);
checkedListBox.TabIndex = 0; checkedListBox.TabIndex = 0;
checkedListBox.Dock = DockStyle.Fill; 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); checkedListBox.SelectedIndexChanged += new System.EventHandler(this.checkedListBox_SelectedIndexChanged);
// //
// SelectionListBox // SelectionListBox

View File

@ -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<string> itemsList = new List<string>();
public event EventHandler _changeEvent;
public CustomSelectedCheckedListBox()
{
InitializeComponent();
}
// Метод для заполнения списка строками
public void PopulateList(List<string> 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);
}
}
}

View File

@ -34,6 +34,44 @@ namespace WinFormsLibraryVolkov
} }
} }
// Обновленный метод для добавления одного элемента в дерево
public void AddCell<T>(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<T>() where T : class, new() public T GetSelectedObject<T>() where T : class, new()
{ {
if (treeView.SelectedNode == null || Levels == null || treeView.SelectedNode.Nodes.Count > 0) if (treeView.SelectedNode == null || Levels == null || treeView.SelectedNode.Nodes.Count > 0)

View File

@ -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<string> 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);
}
}
}

View File

@ -1,4 +1,6 @@
namespace WinFormsTestApp using WinFormsLibraryVolkov;
namespace WinFormsTestApp
{ {
partial class FormMain partial class FormMain
{ {
@ -38,7 +40,7 @@
buttonClear = new Button(); buttonClear = new Button();
buttonAdd = new Button(); buttonAdd = new Button();
textBoxAdd = new TextBox(); textBoxAdd = new TextBox();
SelectionListBox = new WinFormsLibraryVolkov.SelectionListBox(); customSelectedCheckedListBox = new WinFormsLibraryVolkov.CustomSelectedCheckedListBox();
groupBoxData = new GroupBox(); groupBoxData = new GroupBox();
labelTransportType = new Label(); labelTransportType = new Label();
labelModel = new Label(); labelModel = new Label();
@ -62,7 +64,7 @@
// //
customInputRangeDate.AutoValidate = AutoValidate.Disable; customInputRangeDate.AutoValidate = AutoValidate.Disable;
customInputRangeDate.CausesValidation = false; 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.Location = new Point(15, 19);
customInputRangeDate.Margin = new Padding(3, 4, 3, 4); customInputRangeDate.Margin = new Padding(3, 4, 3, 4);
customInputRangeDate.MaxDate = new DateTime(2024, 12, 31, 0, 0, 0, 0); customInputRangeDate.MaxDate = new DateTime(2024, 12, 31, 0, 0, 0, 0);
@ -109,7 +111,7 @@
groupBoxSelected.Controls.Add(buttonClear); groupBoxSelected.Controls.Add(buttonClear);
groupBoxSelected.Controls.Add(buttonAdd); groupBoxSelected.Controls.Add(buttonAdd);
groupBoxSelected.Controls.Add(textBoxAdd); groupBoxSelected.Controls.Add(textBoxAdd);
groupBoxSelected.Controls.Add(SelectionListBox); groupBoxSelected.Controls.Add(customSelectedCheckedListBox);
groupBoxSelected.Location = new Point(324, 6); groupBoxSelected.Location = new Point(324, 6);
groupBoxSelected.Name = "groupBoxSelected"; groupBoxSelected.Name = "groupBoxSelected";
groupBoxSelected.Size = new Size(311, 190); groupBoxSelected.Size = new Size(311, 190);
@ -119,7 +121,7 @@
// //
// buttonGetSelected // buttonGetSelected
// //
buttonGetSelected.Location = new Point(192, 144); buttonGetSelected.Location = new Point(174, 139);
buttonGetSelected.Name = "buttonGetSelected"; buttonGetSelected.Name = "buttonGetSelected";
buttonGetSelected.Size = new Size(100, 23); buttonGetSelected.Size = new Size(100, 23);
buttonGetSelected.TabIndex = 14; buttonGetSelected.TabIndex = 14;
@ -130,7 +132,7 @@
// labelSelectedValue // labelSelectedValue
// //
labelSelectedValue.AutoSize = true; labelSelectedValue.AutoSize = true;
labelSelectedValue.Location = new Point(192, 111); labelSelectedValue.Location = new Point(178, 111);
labelSelectedValue.Name = "labelSelectedValue"; labelSelectedValue.Name = "labelSelectedValue";
labelSelectedValue.Size = new Size(82, 15); labelSelectedValue.Size = new Size(82, 15);
labelSelectedValue.TabIndex = 11; labelSelectedValue.TabIndex = 11;
@ -138,7 +140,7 @@
// //
// buttonClear // buttonClear
// //
buttonClear.Location = new Point(192, 81); buttonClear.Location = new Point(174, 81);
buttonClear.Name = "buttonClear"; buttonClear.Name = "buttonClear";
buttonClear.Size = new Size(100, 23); buttonClear.Size = new Size(100, 23);
buttonClear.TabIndex = 13; buttonClear.TabIndex = 13;
@ -148,7 +150,7 @@
// //
// buttonAdd // buttonAdd
// //
buttonAdd.Location = new Point(192, 52); buttonAdd.Location = new Point(174, 52);
buttonAdd.Name = "buttonAdd"; buttonAdd.Name = "buttonAdd";
buttonAdd.Size = new Size(100, 23); buttonAdd.Size = new Size(100, 23);
buttonAdd.TabIndex = 12; buttonAdd.TabIndex = 12;
@ -158,19 +160,19 @@
// //
// textBoxAdd // textBoxAdd
// //
textBoxAdd.Location = new Point(192, 23); textBoxAdd.Location = new Point(162, 23);
textBoxAdd.Name = "textBoxAdd"; textBoxAdd.Name = "textBoxAdd";
textBoxAdd.Size = new Size(100, 23); textBoxAdd.Size = new Size(140, 23);
textBoxAdd.TabIndex = 11; textBoxAdd.TabIndex = 11;
// //
// SelectionListBox // customSelectedCheckedListBox
// //
SelectionListBox.Location = new Point(36, 19); customSelectedCheckedListBox.Location = new Point(6, 19);
SelectionListBox.Margin = new Padding(3, 4, 3, 4); customSelectedCheckedListBox.Margin = new Padding(3, 4, 3, 4);
SelectionListBox.Name = "SelectionListBox"; customSelectedCheckedListBox.Name = "customSelectedCheckedListBox";
SelectionListBox.SelectedValue = ""; customSelectedCheckedListBox.SelectedElement = "";
SelectionListBox.Size = new Size(150, 157); customSelectedCheckedListBox.Size = new Size(150, 157);
SelectionListBox.TabIndex = 0; customSelectedCheckedListBox.TabIndex = 0;
// //
// groupBoxData // groupBoxData
// //
@ -283,7 +285,7 @@
Visual.Controls.Add(groupBoxSelected); Visual.Controls.Add(groupBoxSelected);
Visual.Location = new Point(4, 24); Visual.Location = new Point(4, 24);
Visual.Name = "Visual"; Visual.Name = "Visual";
Visual.Padding = new Padding(3, 3, 3, 3); Visual.Padding = new Padding(3);
Visual.Size = new Size(645, 438); Visual.Size = new Size(645, 438);
Visual.TabIndex = 0; Visual.TabIndex = 0;
Visual.Text = "Visual"; Visual.Text = "Visual";
@ -320,7 +322,7 @@
private Button buttonClear; private Button buttonClear;
private Button buttonAdd; private Button buttonAdd;
private TextBox textBoxAdd; private TextBox textBoxAdd;
private WinFormsLibraryVolkov.SelectionListBox SelectionListBox; private WinFormsLibraryVolkov.CustomSelectedCheckedListBox customSelectedCheckedListBox;
private GroupBox groupBoxData; private GroupBox groupBoxData;
private WinFormsLibraryVolkov.CustomTreeCell customTreeCell; private WinFormsLibraryVolkov.CustomTreeCell customTreeCell;
private Button buttonGetFromTree; private Button buttonGetFromTree;

View File

@ -25,7 +25,7 @@ namespace WinFormsTestApp
{ {
InitializeComponent(); InitializeComponent();
var list = new List<string>() { "Значение 1", "Значение 2", "Значение 3", "Значение 4", "Значение 5" }; var list = new List<string>() { "Значение 1", "Значение 2", "Значение 3", "Значение 4", "Значение 5" };
SelectionListBox.PopulateList(list); customSelectedCheckedListBox.PopulateList(list);
comboBoxTransportType.Items.Add("Наземный транспорт"); comboBoxTransportType.Items.Add("Наземный транспорт");
comboBoxTransportType.Items.Add("Воздушный транспорт"); comboBoxTransportType.Items.Add("Воздушный транспорт");
@ -54,7 +54,11 @@ namespace WinFormsTestApp
private void buttonCheck_Click(object sender, EventArgs e) 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) private void textBoxMin_KeyPress(object sender, KeyPressEventArgs e)
@ -77,17 +81,33 @@ namespace WinFormsTestApp
private void buttonAdd_Click(object sender, EventArgs e) 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) private void buttonClear_Click(object sender, EventArgs e)
{ {
SelectionListBox.ClearList(); customSelectedCheckedListBox.ClearList();
} }
private void buttonGetSelected_Click(object sender, EventArgs e) private void buttonGetSelected_Click(object sender, EventArgs e)
{ {
labelSelectedValue.Text = SelectionListBox.SelectedValue; labelSelectedValue.Text = customSelectedCheckedListBox.SelectedElement;
if (labelSelectedValue.Text == "") if (labelSelectedValue.Text == "")
{ {
labelSelectedValue.Text = "Значение \nне выбрано"; labelSelectedValue.Text = "Значение \nне выбрано";