This commit is contained in:
GokaPek 2024-09-17 12:40:52 +04:00
parent c3fcd0c80d
commit a24dbe6e5b
6 changed files with 52 additions and 36 deletions

View File

@ -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

View File

@ -38,6 +38,7 @@
checkedListBox1.Name = "checkedListBox1";
checkedListBox1.Size = new Size(179, 180);
checkedListBox1.TabIndex = 0;
checkedListBox1.ItemCheck += CheckedListBox1_ItemCheck;
checkedListBox1.SelectedIndexChanged += SelectedIndexChanged;
//
// ChooseList

View File

@ -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()
{

View File

@ -29,27 +29,21 @@ namespace Library14Petrushin
this.alwaysNewBranch = alwaysNewBranch;
}
public void AddObjectToTree(object obj, string stopAtProperty)
public void AddObjectToTree<T>(T obj, string stopAtProperty)
{
TreeNode currentNode = treeView1.Nodes.Cast<TreeNode>().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)
{
currentNode = treeView1.Nodes.Add(value);
}
else
{
var childNode = currentNode.Nodes.Cast<TreeNode>().FirstOrDefault(n => n.Text == value);
if (childNode == null || createNewBranch)
{
childNode = currentNode.Nodes.Add(value);
}
currentNode = childNode;
}
if (property == stopAtProperty)
{
@ -73,15 +67,19 @@ namespace Library14Petrushin
}
}
public object GetSelectedObject()
public T GetSelectedObject<T>() 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;

View File

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

View File

@ -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("Диапазон не задан.");
}
}
}