DepartmentProject/DepartmentPortal/Common/DesktopTools/BaseControls/BaseControlGuid.cs

106 lines
2.5 KiB
C#
Raw Normal View History

2021-03-30 22:34:31 +04:00
using DesktopTools.Controls;
using System;
using System.Drawing;
using System.Windows.Forms;
namespace DesktopTools.BaseControls
{
public partial class BaseControlGuid : AbstractBaseControl
{
private AbstractControlViewEntityList _control;
public BaseControlGuid(string propertyName, bool mustFilling, bool readOnly, AbstractControlViewEntityList controlType) : base(propertyName, mustFilling, readOnly)
{
InitializeComponent();
_control = controlType;
panelControl.Controls.Add(textBox);
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.Height,
Width = controlType.Width,
Text = $"{controlType.Title}. Выбор",
StartPosition = FormStartPosition.CenterParent,
ControlBox = false
};
var clone = controlType.Clone();
form.Controls.Add(clone);
clone.Dock = DockStyle.Fill;
clone.FormForSelected = form;
clone.Open();
if (form.ShowDialog() == DialogResult.OK)
{
textBox.Tag = clone.SelectedId;
textBox.Text = clone.SelectedText;
CallOnValueChangeEvent();
}
};
panelControl.Controls.Add(buttonSelect);
}
protected override void SetDefaultValue() => _originalValue = null;
protected override 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 override void DropValue()
{
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 override bool CheckValue()
{
if (_mustFilling && textBox.Tag == null)
{
BackColor = Color.OrangeRed;
return false;
}
return true;
}
protected override object GetValueFromControl()
{
if (_mustFilling && textBox.Tag == null)
{
throw new ArgumentNullException($"Поле свойства '{labelTitle.Text}' должно быть заполнено");
}
return textBox.Tag == null ? null : new Guid(textBox.Tag.ToString());
}
}
}