DepartmentProject/DepartmentPortal/Common/DesktopTools/BaseControls/BaseControlGuid.cs
kotcheshir73 e8836c8806 куча наработко по выводу элемента и списка
правки по сущностям безоапсности
2021-04-01 21:30:29 +04:00

124 lines
2.9 KiB
C#

using DesktopTools.Enums;
using DesktopTools.Interfaces;
using DesktopTools.Models;
using System;
using System.Drawing;
using System.Windows.Forms;
namespace DesktopTools.BaseControls
{
public partial class BaseControlGuid : AbstractBaseControl, IBaseControl
{
private readonly IControlEntitySelectable _control;
public BaseControlGuid(string propertyName, bool mustFilling, bool readOnly, IControlEntitySelectable controlType, Guid? parentId) : base(propertyName, mustFilling, readOnly)
{
InitializeComponent();
_baseControl = this;
_control = controlType;
panelControl.Controls.Add(textBox);
if (parentId.HasValue)
{
textBox.Tag = parentId.Value;
textBox.Text = _control?.GetTitleFromId(parentId.Value);
panelControl.Enabled = false;
}
if (!_mustFilling)
{
buttonClear.Click += (object sender, EventArgs e) =>
{
textBox.Text = string.Empty;
textBox.Tag = null;
CallOnValueChangeEvent();
};
panelControl.Controls.Add(buttonClear);
}
buttonSelect.Click += (object sender, EventArgs e) =>
{
var form = new Form
{
Height = (controlType as UserControl).Height,
Width = (controlType as UserControl).Width,
Text = $"{controlType.Title}. Выбор",
StartPosition = FormStartPosition.CenterParent,
ControlBox = false
};
var clone = controlType.GetInstance() as IControlEntitySelectable;
clone.Open(new ControlOpenModel
{
OpenMode = ControlOpenMode.Select,
CloseSelect = (bool flag) =>
{
if (flag)
{
textBox.Tag = clone.SelectedId;
textBox.Text = clone.SelectedText;
CallOnValueChangeEvent();
}
form.Close();
}
});
form.Controls.Add(clone as UserControl);
form.ShowDialog();
};
panelControl.Controls.Add(buttonSelect);
}
public void SetDefaultValue() => _originalValue = null;
public void SetValueToControl(object value)
{
if (value != null)
{
var id = new Guid(value.ToString());
textBox.Tag = id;
textBox.Text = _control?.GetTitleFromId(id);
}
else if (!_mustFilling)
{
textBox.Tag = null;
}
}
public void DropValueForControl()
{
if (_originalValue != null)
{
var id = new Guid(_originalValue.ToString());
textBox.Tag = id;
textBox.Text = _control?.GetTitleFromId(id);
}
else if (!_mustFilling)
{
textBox.Tag = null;
textBox.Text = string.Empty;
}
}
public bool CheckValueForControl()
{
if (_mustFilling && textBox.Tag == null)
{
BackColor = Color.OrangeRed;
return false;
}
return true;
}
public object GetValueFromControl()
{
if (_mustFilling && textBox.Tag == null)
{
throw new ArgumentNullException($"Поле свойства '{labelTitle.Text}' должно быть заполнено");
}
return textBox.Tag == null ? null : new Guid(textBox.Tag.ToString());
}
public string GetPropertyName() => _propertyName;
}
}