Compare commits
No commits in common. "5d21dc1cdee65f73537ec8faa904c8f24c817559" and "1f8701e03c4f02c7e0134c97fef4704bd4fea483" have entirely different histories.
5d21dc1cde
...
1f8701e03c
2
KOP_Labs/KOP_Labs/CustomComboBox.Designer.cs
generated
2
KOP_Labs/KOP_Labs/CustomComboBox.Designer.cs
generated
@ -38,7 +38,7 @@
|
||||
textBox.Size = new Size(263, 27);
|
||||
textBox.TabIndex = 0;
|
||||
textBox.TextChanged += textBox_TextChanged;
|
||||
//textBox.Enter += textBox_Enter;
|
||||
textBox.Enter += textBox_Enter;
|
||||
//
|
||||
// CustomComboBox
|
||||
//
|
||||
|
@ -17,12 +17,40 @@ namespace KOP_Labs
|
||||
{
|
||||
InitializeComponent();
|
||||
}
|
||||
private string examp = "Введите текст от " + decimal.MinValue + " до" + decimal.MaxValue + " символов";
|
||||
|
||||
public int? max = null;
|
||||
public int? min = null;
|
||||
public int? Max
|
||||
{
|
||||
get
|
||||
{
|
||||
return max;
|
||||
}
|
||||
set
|
||||
{
|
||||
if (value == null || value <= 0) return;
|
||||
max = value;
|
||||
|
||||
}
|
||||
|
||||
public int? MinValue { get; set; }
|
||||
public int? MaxValue { get; set; }
|
||||
}
|
||||
public int? Min
|
||||
{
|
||||
get
|
||||
{
|
||||
return min;
|
||||
}
|
||||
set
|
||||
{
|
||||
if (value == null || value < 0) return;
|
||||
min = value;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private EventHandler _changeEvent;
|
||||
|
||||
public event EventHandler ChangeEvent
|
||||
{
|
||||
add
|
||||
@ -38,44 +66,39 @@ namespace KOP_Labs
|
||||
private void textBox_TextChanged(object sender, EventArgs e)
|
||||
{
|
||||
_changeEvent?.Invoke(sender, e);
|
||||
if (MinValue != null && MaxValue != null)
|
||||
{
|
||||
if (textBox.Text.Length < MinValue || textBox.Text.Length > MaxValue)
|
||||
{
|
||||
textBox.ForeColor = Color.Red;
|
||||
}
|
||||
else
|
||||
{
|
||||
textBox.ForeColor = Color.Green;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public string TextBoxValue
|
||||
private void textBox_Enter(object sender, EventArgs e)
|
||||
{
|
||||
ToolTip tt = new ToolTip();
|
||||
tt.Show(examp, textBox, 30, -20, 2000);
|
||||
}
|
||||
|
||||
public string? TextBoxValue
|
||||
{
|
||||
get
|
||||
{
|
||||
if (MinValue == null && MaxValue == null)
|
||||
if (Min == null || Max == null)
|
||||
{
|
||||
throw new NoBordersException("Диапазон не задан.");
|
||||
throw new NoBordersException("Границы не заданы");
|
||||
}
|
||||
if (textBox.Text.Length >= MinValue && textBox.Text.Length <= MaxValue)
|
||||
if (textBox.Text.Length < Min || textBox.Text.Length > Max)
|
||||
{
|
||||
return textBox.Text;
|
||||
throw new ArgumentOutOfRangeException("Ваша строка не входит в границы диапозона: " + Min + " - " + Max);
|
||||
}
|
||||
throw new NoBordersException("Введенное значение не входит в диапазон.");
|
||||
|
||||
return textBox.Text;
|
||||
}
|
||||
set
|
||||
{
|
||||
if (MinValue != null && MaxValue != null)
|
||||
if (value == null || value.Length < 0 || Max == null)
|
||||
{
|
||||
if (value.Length >= MinValue && value.Length <= MaxValue)
|
||||
{
|
||||
textBox.Text = value;
|
||||
}
|
||||
return;
|
||||
}
|
||||
if (value.Length < Min && value.Length > Max)
|
||||
{
|
||||
return;
|
||||
}
|
||||
textBox.Text = value;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -61,15 +61,26 @@ namespace KOP_Labs
|
||||
{
|
||||
get
|
||||
{
|
||||
return checkedListBox.SelectedItem?.ToString() ?? string.Empty;
|
||||
if(checkedListBox.Items.Count == 0)
|
||||
{
|
||||
return "";
|
||||
}
|
||||
if(checkedListBox.Items.Count == null)
|
||||
{
|
||||
return "";
|
||||
}
|
||||
return checkedListBox.SelectedItem.ToString()!;
|
||||
}
|
||||
set
|
||||
{
|
||||
if (checkedListBox.Items.Contains(value))
|
||||
int index = checkedListBox.Items.IndexOf(value);
|
||||
if (index == -1)
|
||||
{
|
||||
checkedListBox.SelectedItem = value;
|
||||
return;
|
||||
}
|
||||
|
||||
checkedListBox.SelectedItem = value;
|
||||
checkedListBox.SetItemChecked(index, true);
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -15,88 +15,106 @@ namespace KOP_Labs
|
||||
{
|
||||
public partial class CustomTree : UserControl
|
||||
{
|
||||
private List<string> hierarchy;
|
||||
|
||||
public CustomTree()
|
||||
{
|
||||
InitializeComponent();
|
||||
hierarchy = new List<string>();
|
||||
}
|
||||
|
||||
public List<string>? Hierarcy { get; set; }
|
||||
public void Clear()
|
||||
{
|
||||
treeView.Nodes.Clear();
|
||||
}
|
||||
|
||||
public void SetHierarcy(List<string> hierarchy)
|
||||
{
|
||||
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 AddObjectToTree<T>(T obj)
|
||||
{
|
||||
if (hierarchy == null)
|
||||
public void AddNode<T>(T obj) {
|
||||
if (Hierarcy == null)
|
||||
{
|
||||
throw new HierarcyNullException("Иерархия не задана");
|
||||
}
|
||||
TreeNode currentNode = treeView.Nodes.Count > 0 ? treeView.Nodes[0] : treeView.Nodes.Add("");
|
||||
foreach (var property in hierarchy)
|
||||
if (obj == null)
|
||||
{
|
||||
var value = obj.GetType().GetProperty(property).GetValue(obj, null).ToString();
|
||||
|
||||
var childNode = currentNode.Nodes.Cast<TreeNode>().FirstOrDefault(n => n.Text == value);
|
||||
if (childNode == null)
|
||||
{
|
||||
childNode = currentNode.Nodes.Add(value);
|
||||
}
|
||||
|
||||
currentNode = childNode;
|
||||
|
||||
throw new ArgumentNullException("Объект не найден");
|
||||
}
|
||||
}
|
||||
|
||||
public T GetSelectedObject<T>() where T : new()
|
||||
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)
|
||||
{
|
||||
if (hierarchy == null)
|
||||
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)
|
||||
{
|
||||
elements.Remove(firstElem.Key);
|
||||
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!);
|
||||
|
||||
elements.Remove(firstElem.Key);
|
||||
parent.Nodes.Add(AddElementsToParent(elements, newChild));
|
||||
return parent;
|
||||
}
|
||||
}
|
||||
public T GetSelectedNode<T>()
|
||||
where T : new()
|
||||
{
|
||||
if (Hierarcy == null)
|
||||
{
|
||||
throw new HierarcyNullException("Иерархия не задана");
|
||||
}
|
||||
if (treeView.SelectedNode == null)
|
||||
{
|
||||
throw new InvalidOperationException("Узел не выбран");
|
||||
return new T();
|
||||
}
|
||||
|
||||
var node = treeView.SelectedNode;
|
||||
if (node.Nodes.Count != 0)
|
||||
return GetNode(new T(), treeView.SelectedNode);
|
||||
}
|
||||
private T GetNode<T>(T obj, TreeNode node)
|
||||
{
|
||||
if (node != null && node.Tag != null)
|
||||
{
|
||||
throw new InvalidOperationException("Узел не конечный, сформировать объект нельзя");
|
||||
}
|
||||
var tag = node.Tag as Tuple<string, object>;
|
||||
|
||||
var obj = new T();
|
||||
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));
|
||||
var property = obj?.GetType().GetProperty(tag!.Item1);
|
||||
property?.SetValue(obj, Convert.ChangeType(tag!.Item2, property.PropertyType));
|
||||
|
||||
node = node.Parent;
|
||||
if (node == null)
|
||||
{
|
||||
break;
|
||||
}
|
||||
GetNode(obj, node.Parent);
|
||||
}
|
||||
return obj;
|
||||
}
|
||||
|
6
KOP_Labs/WinFormForTest/Form1.Designer.cs
generated
6
KOP_Labs/WinFormForTest/Form1.Designer.cs
generated
@ -42,8 +42,8 @@
|
||||
// customComboBox1
|
||||
//
|
||||
customComboBox1.Location = new Point(264, 50);
|
||||
customComboBox1.MaxValue = 20;
|
||||
customComboBox1.MinValue = 5;
|
||||
customComboBox1.Max = 20;
|
||||
customComboBox1.Min = 5;
|
||||
customComboBox1.Name = "customComboBox1";
|
||||
customComboBox1.Size = new Size(301, 125);
|
||||
customComboBox1.TabIndex = 0;
|
||||
@ -59,7 +59,7 @@
|
||||
//
|
||||
// customTree1
|
||||
//
|
||||
//customTree1.Hierarcy = null;
|
||||
customTree1.Hierarcy = null;
|
||||
customTree1.Location = new Point(615, 50);
|
||||
customTree1.Name = "customTree1";
|
||||
customTree1.Size = new Size(309, 384);
|
||||
|
@ -28,11 +28,10 @@ namespace WinFormForTest
|
||||
|
||||
public void LoadTree()
|
||||
{
|
||||
var hierarcy= new List<string> { "Type", "Name", "Color" };
|
||||
customTree1.SetHierarcy(hierarcy);
|
||||
customTree1.Hierarcy = new List<string> { "Type", "Name", "Color" };
|
||||
foreach (Plant plant in plants)
|
||||
{
|
||||
customTree1.AddObjectToTree(plant);
|
||||
customTree1.AddNode(plant);
|
||||
}
|
||||
}
|
||||
private void buttonClear_Click(object sender, EventArgs e)
|
||||
|
Loading…
Reference in New Issue
Block a user