Compare commits

..

3 Commits

Author SHA1 Message Date
5d21dc1cde сдано 2024-09-23 12:53:35 +04:00
37673c4961 поправка 2024-09-23 12:27:58 +04:00
d0b096e308 дерево 2024-09-23 12:06:31 +04:00
6 changed files with 98 additions and 149 deletions

View File

@ -38,7 +38,7 @@
textBox.Size = new Size(263, 27); textBox.Size = new Size(263, 27);
textBox.TabIndex = 0; textBox.TabIndex = 0;
textBox.TextChanged += textBox_TextChanged; textBox.TextChanged += textBox_TextChanged;
textBox.Enter += textBox_Enter; //textBox.Enter += textBox_Enter;
// //
// CustomComboBox // CustomComboBox
// //

View File

@ -17,40 +17,12 @@ namespace KOP_Labs
{ {
InitializeComponent(); InitializeComponent();
} }
private string examp = "Введите текст от " + decimal.MinValue + " до" + decimal.MaxValue + " символов";
public int? max = null; public int? MinValue { get; set; }
public int? min = null; public int? MaxValue { get; set; }
public int? Max
{
get
{
return max;
}
set
{
if (value == null || value <= 0) return;
max = value;
}
}
public int? Min
{
get
{
return min;
}
set
{
if (value == null || value < 0) return;
min = value;
}
}
private EventHandler _changeEvent; private EventHandler _changeEvent;
public event EventHandler ChangeEvent public event EventHandler ChangeEvent
{ {
add add
@ -66,41 +38,46 @@ namespace KOP_Labs
private void textBox_TextChanged(object sender, EventArgs e) private void textBox_TextChanged(object sender, EventArgs e)
{ {
_changeEvent?.Invoke(sender, e); _changeEvent?.Invoke(sender, e);
} if (MinValue != null && MaxValue != null)
private void textBox_Enter(object sender, EventArgs e)
{ {
ToolTip tt = new ToolTip(); if (textBox.Text.Length < MinValue || textBox.Text.Length > MaxValue)
tt.Show(examp, textBox, 30, -20, 2000); {
textBox.ForeColor = Color.Red;
}
else
{
textBox.ForeColor = Color.Green;
}
} }
public string? TextBoxValue }
public string TextBoxValue
{ {
get get
{ {
if (Min == null || Max == null) if (MinValue == null && MaxValue == null)
{ {
throw new NoBordersException("Границы не заданы"); throw new NoBordersException("Диапазон не задан.");
} }
if (textBox.Text.Length < Min || textBox.Text.Length > Max) if (textBox.Text.Length >= MinValue && textBox.Text.Length <= MaxValue)
{ {
throw new ArgumentOutOfRangeException("Ваша строка не входит в границы диапозона: " + Min + " - " + Max);
}
return textBox.Text; return textBox.Text;
} }
throw new NoBordersException("Введенное значение не входит в диапазон.");
}
set set
{ {
if (value == null || value.Length < 0 || Max == null) if (MinValue != null && MaxValue != null)
{ {
return; if (value.Length >= MinValue && value.Length <= MaxValue)
}
if (value.Length < Min && value.Length > Max)
{ {
return;
}
textBox.Text = value; textBox.Text = value;
} }
} }
}
}
} }
} }

View File

@ -61,25 +61,14 @@ namespace KOP_Labs
{ {
get get
{ {
if(checkedListBox.Items.Count == 0) return checkedListBox.SelectedItem?.ToString() ?? string.Empty;
{
return "";
}
if(checkedListBox.Items.Count == null)
{
return "";
}
return checkedListBox.SelectedItem.ToString()!;
} }
set set
{ {
int index = checkedListBox.Items.IndexOf(value); if (checkedListBox.Items.Contains(value))
if (index == -1)
{ {
return;
}
checkedListBox.SelectedItem = value; checkedListBox.SelectedItem = value;
checkedListBox.SetItemChecked(index, true); }
} }
} }

View File

@ -15,106 +15,88 @@ namespace KOP_Labs
{ {
public partial class CustomTree : UserControl public partial class CustomTree : UserControl
{ {
private List<string> hierarchy;
public CustomTree() public CustomTree()
{ {
InitializeComponent(); InitializeComponent();
hierarchy = new List<string>();
} }
public List<string>? Hierarcy { get; set; } public void SetHierarcy(List<string> hierarchy)
public void Clear()
{ {
treeView.Nodes.Clear(); this.hierarchy = hierarchy;
}
public int SelectedNodeIndex
{
get
{
return treeView.SelectedNode?.Index ?? -1;
}
set
{
if (value >= 0 && value < treeView.Nodes.Count)
{
treeView.SelectedNode = treeView.Nodes[value];
}
}
} }
public void AddNode<T>(T obj) { public void AddObjectToTree<T>(T obj)
if (Hierarcy == null) {
if (hierarchy == null)
{ {
throw new HierarcyNullException("Иерархия не задана"); throw new HierarcyNullException("Иерархия не задана");
} }
if (obj == null) TreeNode currentNode = treeView.Nodes.Count > 0 ? treeView.Nodes[0] : treeView.Nodes.Add("");
foreach (var property in hierarchy)
{ {
throw new ArgumentNullException("Объект не найден"); var value = obj.GetType().GetProperty(property).GetValue(obj, null).ToString();
}
var values = GetValuesVithStructure(obj);
var treeNode = new TreeNode();
AddNodesToTreeNode(treeView.Nodes, treeNode.Nodes);
var nodes = AddElementsToParent(values, treeNode);
treeView.Nodes.Clear();
AddNodesToTreeNode(nodes.Nodes, treeView.Nodes);
}
private void AddNodesToTreeNode(TreeNodeCollection fromNodeCollection, TreeNodeCollection toNodeCollection)
{
for (int i = 0; i < fromNodeCollection.Count; i++)
{
var node = fromNodeCollection[i].Clone() as TreeNode;
toNodeCollection.Add(node);
}
}
private Dictionary<string, (object, bool)> GetValuesVithStructure<T>(T obj)
{
PropertyInfo[]? properties = obj?.GetType().GetProperties();
var dict = new Dictionary<string, (object, bool)>();
foreach (var elem in Hierarcy!) {
PropertyInfo? prop = properties?.Single(prop => prop.Name == elem);
if (prop == null)
{
throw new PropertyNullException(nameof(prop));
}
var atr = prop.GetCustomAttributes()?.SingleOrDefault(atr => atr is AlwaysCreateAttribute);
dict[elem] = (prop.GetValue(obj)!, atr == null ? false : true);
}
return dict;
}
private TreeNode AddElementsToParent(Dictionary<string, (object, bool)> elements, TreeNode parent)
{
if (elements.Count == 0)
{
return parent;
}
var firstElem = elements.First();
var child = parent.Nodes
.Cast<TreeNode>()
.SingleOrDefault(node => node.Text == (string)firstElem.Value.Item1);
if (child != null && !firstElem.Value.Item2) var childNode = currentNode.Nodes.Cast<TreeNode>().FirstOrDefault(n => n.Text == value);
if (childNode == null)
{ {
elements.Remove(firstElem.Key); childNode = currentNode.Nodes.Add(value);
return AddElementsToParent(elements, child).Parent;
} }
else
{
var newChild = new TreeNode(firstElem.Value.Item1?.ToString() ?? string.Empty);
newChild.Tag = new Tuple<string, object>(firstElem.Key, firstElem.Value.Item1!); currentNode = childNode;
elements.Remove(firstElem.Key);
parent.Nodes.Add(AddElementsToParent(elements, newChild));
return parent;
} }
} }
public T GetSelectedNode<T>()
where T : new() public T GetSelectedObject<T>() where T : new()
{ {
if (Hierarcy == null) if (hierarchy == null)
{ {
throw new HierarcyNullException("Иерархия не задана"); throw new HierarcyNullException("Иерархия не задана");
} }
if (treeView.SelectedNode == null) if (treeView.SelectedNode == null)
{ {
return new T(); throw new InvalidOperationException("Узел не выбран");
} }
return GetNode(new T(), treeView.SelectedNode);
var node = treeView.SelectedNode;
if (node.Nodes.Count != 0)
{
throw new InvalidOperationException("Узел не конечный, сформировать объект нельзя");
} }
private T GetNode<T>(T obj, TreeNode node)
{
if (node != null && node.Tag != null)
{
var tag = node.Tag as Tuple<string, object>;
var property = obj?.GetType().GetProperty(tag!.Item1); var obj = new T();
property?.SetValue(obj, Convert.ChangeType(tag!.Item2, property.PropertyType)); foreach (var property in hierarchy)
{
var value = node.Text;
var propInfo = typeof(T).GetProperty(property);
if (propInfo == null)
{
throw new InvalidOperationException($"Свойство {property} не найдено в классе {typeof(T).Name}");
}
propInfo.SetValue(obj, Convert.ChangeType(value, propInfo.PropertyType));
GetNode(obj, node.Parent); node = node.Parent;
if (node == null)
{
break;
}
} }
return obj; return obj;
} }

View File

@ -42,8 +42,8 @@
// customComboBox1 // customComboBox1
// //
customComboBox1.Location = new Point(264, 50); customComboBox1.Location = new Point(264, 50);
customComboBox1.Max = 20; customComboBox1.MaxValue = 20;
customComboBox1.Min = 5; customComboBox1.MinValue = 5;
customComboBox1.Name = "customComboBox1"; customComboBox1.Name = "customComboBox1";
customComboBox1.Size = new Size(301, 125); customComboBox1.Size = new Size(301, 125);
customComboBox1.TabIndex = 0; customComboBox1.TabIndex = 0;
@ -59,7 +59,7 @@
// //
// customTree1 // customTree1
// //
customTree1.Hierarcy = null; //customTree1.Hierarcy = null;
customTree1.Location = new Point(615, 50); customTree1.Location = new Point(615, 50);
customTree1.Name = "customTree1"; customTree1.Name = "customTree1";
customTree1.Size = new Size(309, 384); customTree1.Size = new Size(309, 384);

View File

@ -28,10 +28,11 @@ namespace WinFormForTest
public void LoadTree() public void LoadTree()
{ {
customTree1.Hierarcy = new List<string> { "Type", "Name", "Color" }; var hierarcy= new List<string> { "Type", "Name", "Color" };
customTree1.SetHierarcy(hierarcy);
foreach (Plant plant in plants) foreach (Plant plant in plants)
{ {
customTree1.AddNode(plant); customTree1.AddObjectToTree(plant);
} }
} }
private void buttonClear_Click(object sender, EventArgs e) private void buttonClear_Click(object sender, EventArgs e)