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

124 lines
2.9 KiB
C#
Raw Permalink Normal View History

2022-03-18 22:48:14 +04:00
using ToolsDesktop.Enums;
using ToolsDesktop.Interfaces;
using ToolsDesktop.Models;
2021-03-30 22:34:31 +04:00
using System;
using System.Drawing;
using System.Windows.Forms;
2022-03-18 22:48:14 +04:00
namespace ToolsDesktop.BaseControls
2021-03-30 22:34:31 +04:00
{
public partial class BaseControlGuid : AbstractBaseControl, IBaseControl
2021-03-30 22:34:31 +04:00
{
private readonly IControlEntitySelectable _control;
2021-03-30 22:34:31 +04:00
public BaseControlGuid(string propertyName, bool mustFilling, bool readOnly, IControlEntitySelectable controlType, Guid? parentId) : base(propertyName, mustFilling, readOnly)
2021-03-30 22:34:31 +04:00
{
InitializeComponent();
_baseControl = this;
2021-03-30 22:34:31 +04:00
_control = controlType;
panelControl.Controls.Add(textBox);
if (parentId.HasValue)
{
textBox.Tag = parentId.Value;
textBox.Text = _control?.GetTitleFromId(parentId.Value);
panelControl.Enabled = false;
}
2021-03-30 22:34:31 +04:00
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,
2021-03-30 22:34:31 +04:00
Text = $"{controlType.Title}. Выбор",
StartPosition = FormStartPosition.CenterParent,
ControlBox = false
};
var clone = controlType.GetInstance() as IControlEntitySelectable;
clone.Open(new ControlOpenModel
2021-03-30 22:34:31 +04:00
{
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();
2021-03-30 22:34:31 +04:00
};
panelControl.Controls.Add(buttonSelect);
}
public void SetDefaultValue() => _originalValue = null;
2021-03-30 22:34:31 +04:00
public void SetValueToControl(object value)
2021-03-30 22:34:31 +04:00
{
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()
2021-03-30 22:34:31 +04:00
{
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()
2021-03-30 22:34:31 +04:00
{
if (_mustFilling && textBox.Tag == null)
{
BackColor = Color.OrangeRed;
return false;
}
return true;
}
public object GetValueFromControl()
2021-03-30 22:34:31 +04:00
{
if (_mustFilling && textBox.Tag == null)
{
throw new ArgumentNullException($"Поле свойства '{labelTitle.Text}' должно быть заполнено");
}
return textBox.Tag == null ? null : new Guid(textBox.Tag.ToString());
}
public string GetPropertyName() => _propertyName;
2021-03-30 22:34:31 +04:00
}
}