using DesktopTools.BusinessLogics; using DesktopTools.Controls; using System; using System.Collections.Generic; using System.Linq; using System.Windows.Forms; namespace DepartmentPortalDesctop { public partial class FormMain : Form { private readonly Dictionary _baseControls; private readonly Dictionary _controls; public FormMain() { InitializeComponent(); _baseControls = new Dictionary(); _controls = new Dictionary(); var extensions = DesktopLoader.GetWindowDesktopExtensions(); foreach (var extens in extensions) { var list = extens?.GetListControlEntityList()?.OrderBy(x => x.Order).ToList(); if (list != null && list.Count > 0) { var menu = new ToolStripMenuItem { Text = list[0].Title }; for (int i = 0; i < list.Count; i++) { if (list[i].Control is AbstractControlViewEntityList control) { if (_baseControls.ContainsKey(list[i].Id)) { continue; } _baseControls.Add(list[i].Id, control); var submenu = new ToolStripMenuItem { Text = list[i].Title, Tag = list[i].Id }; submenu.Click += (object sender, EventArgs e) => { OpenControl(new Guid((sender as ToolStripMenuItem).Tag.ToString())); }; menu.DropDownItems.Add(submenu); } } menuMain.Items.Add(menu); } } } /// /// Добавление нового контрола /// /// private void OpenControl(Guid id) { if (!_baseControls.ContainsKey(id)) { return; } if (_baseControls[id] == null) { return; } var ctrl = _baseControls[id].Clone(); if (!_controls.ContainsKey(ctrl.ControlId)) { ctrl.CloseEventAdd += CloseControl; ctrl.Dock = DockStyle.Fill; ctrl.Open(); _controls.Add(ctrl.ControlId, ctrl); splitContainerMain.Panel1.Controls.Clear(); splitContainerMain.Panel1.Controls.Add(ctrl); dataGridViewControls.Rows.Add(new object[] { ctrl.ControlId, ctrl.Title }); dataGridViewControls.Rows[^1].Selected = true; } } /// /// Закрытие контрола /// /// private void CloseControl(Guid id) { if (!_controls.ContainsKey(id)) { return; } var ctrl = _controls[id]; if (ctrl == null) { return; } for (int i = 0; i < dataGridViewControls.Rows.Count; ++i) { if (dataGridViewControls.Rows[i].Cells[0].Value.ToString() == ctrl.ControlId.ToString()) { dataGridViewControls.Rows.RemoveAt(i); if (i < dataGridViewControls.Rows.Count - 1) { dataGridViewControls.Rows[i].Selected = true; ShowControl(new Guid(dataGridViewControls.Rows[i].Cells[0].Value.ToString())); } else if (i > 0) { dataGridViewControls.Rows[i - 1].Selected = true; ShowControl(new Guid(dataGridViewControls.Rows[i - 1].Cells[0].Value.ToString())); } break; } } _controls.Remove(ctrl.ControlId); } /// /// Отображение контрола /// /// private void ShowControl(Guid id) { if (!_controls.ContainsKey(id)) { return; } var ctrl = _controls[id]; if (ctrl == null) { return; } splitContainerMain.Panel1.Controls.Clear(); splitContainerMain.Panel1.Controls.Add(ctrl); } private void ButtonShowHideControlList_Click(object sender, EventArgs e) { ButtonShowHideControlList.Text = panelControls.Visible ? ">" : "<"; panelControls.Visible = !panelControls.Visible; } private void DataGridViewControls_CellClick(object sender, DataGridViewCellEventArgs e) => ShowControl(new Guid(dataGridViewControls.Rows[e.RowIndex].Cells[0].Value.ToString())); } }