PIbd-33_Dyakonov_R_R_COP_14/WinFormsApp1/Form1.cs
dyakonovr e57bd46f1f done
2024-09-29 21:11:27 +04:00

111 lines
3.3 KiB
C#

using System.Security.Cryptography.Xml;
using WinFormsLibrary1;
using WinFormsLibrary1.Errors;
using WinFormsLibrary1.Models;
namespace WinFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
FillCustomCheckedListBox();
FillCustomDataTree();
}
private void FillCustomCheckedListBox()
{
List<string> list = new List<string>() { "Çíà÷åíèå 1", "Çíà÷åíèå 2", "Çíà÷åíèå 3" };
for (int i = 0; i < list.Count; i++)
{
customCheckedListBox.AddItem(list[i]);
}
}
private void FillCustomDataTree()
{
departamentComboBox.Items.Add("Îòäåë ïðîäàæ");
departamentComboBox.Items.Add("Îòäåë àíàëèòèêè");
departamentComboBox.Items.Add("IT îòäåë");
DataTreeNodeConfig config = new DataTreeNodeConfig(new List<string> { "Department", "Position", "Name" });
customDataTree.LoadConfig(config);
List<Employee> employees = new List<Employee>
{
new Employee("Èâàíîâ Èâàí", "Ìåíåäæåð", "Îòäåë ïðîäàæ"),
new Employee("Ïåòðîâ Ïåòð", "Àíàëèòèê", "Îòäåë àíàëèòèêè"),
new Employee("Ñèäîðîâ Ñèäîð", "Ìåíåäæåð", "Îòäåë ïðîäàæ"),
new Employee("Ìàðèÿ Ñìèðíîâà", "Ðàçðàáîò÷èê", "IT îòäåë"),
new Employee("Îëüãà Ïåòðîâà", "Ìåíåäæåð", "Îòäåë ïðîäàæ"),
};
foreach (var employee in employees)
{
customDataTree.AddObject(employee);
}
}
private void addInListboxItemButton_Click(object sender, EventArgs e)
{
string value = listboxItemValuetextBox.Text;
if (customCheckedListBox.Items.Contains(value)) customCheckedListBox.SelectedElement = value;
else customCheckedListBox.AddItem(value);
}
private void clearListboxButton_Click(object sender, EventArgs e)
{
customCheckedListBox.Clear();
}
private void getSelectedItemButton_Click(object sender, EventArgs e)
{
string value = customCheckedListBox.SelectedElement;
if (value == string.Empty) selectedItemLabel.Text = "Selected item: ~empty value~";
else selectedItemLabel.Text = "Selected item: " + value;
}
private void checkCustomTextBoxButton_Click(object sender, EventArgs e)
{
try
{
errorCustomTextBoxLabel.Text = customTextBox.Value == string.Empty ? "~empty value~" : customTextBox.Value;
}
catch (RangeNotSetException ex)
{
// Îáðàáàòûâàåì îøèáêó, åñëè äèàïàçîí íå çàäàí.
MessageBox.Show(ex.Message, "Îøèáêà äèàïàçîíà", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
catch (TextLengthOutOfRangeException ex)
{
// Îáðàáàòûâàåì îøèáêó, åñëè äëèíà òåêñòà âíå äèàïàçîíà.
MessageBox.Show(ex.Message, "Îøèáêà äëèíû òåêñòà", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
private void addEmployeeButton_Click(object sender, EventArgs e)
{
if (positionTextBox.Text == null || nameTextBox.Text == null || departamentComboBox.SelectedItem == null)
{
return;
}
customDataTree.AddObject<Employee>(new(nameTextBox.Text, positionTextBox.Text, departamentComboBox.SelectedItem.ToString()));
customDataTree.Update();
}
private void getSelectedTreeItemButton_Click(object sender, EventArgs e)
{
Employee employee = customDataTree.GetSelectedObject<Employee>();
if (employee == null)
{
return;
}
positionTextBox.Text = employee.Position;
nameTextBox.Text = employee.Name;
departamentComboBox.SelectedItem = employee.Department;
}
}
}