2021-04-01 21:30:29 +04:00
|
|
|
|
using DesktopTools.Enums;
|
|
|
|
|
using DesktopTools.Interfaces;
|
|
|
|
|
using DesktopTools.Models;
|
2021-03-30 22:34:31 +04:00
|
|
|
|
using System;
|
|
|
|
|
using System.Drawing;
|
|
|
|
|
using System.Windows.Forms;
|
|
|
|
|
|
|
|
|
|
namespace DesktopTools.BaseControls
|
|
|
|
|
{
|
2021-04-01 21:30:29 +04:00
|
|
|
|
public partial class BaseControlGuid : AbstractBaseControl, IBaseControl
|
2021-03-30 22:34:31 +04:00
|
|
|
|
{
|
2021-04-01 21:30:29 +04:00
|
|
|
|
private readonly IControlEntitySelectable _control;
|
2021-03-30 22:34:31 +04:00
|
|
|
|
|
2021-04-01 21:30:29 +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();
|
2021-04-01 21:30:29 +04:00
|
|
|
|
_baseControl = this;
|
2021-03-30 22:34:31 +04:00
|
|
|
|
|
|
|
|
|
_control = controlType;
|
|
|
|
|
|
|
|
|
|
panelControl.Controls.Add(textBox);
|
|
|
|
|
|
2021-04-01 21:30:29 +04:00
|
|
|
|
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
|
|
|
|
|
{
|
2021-04-01 21:30:29 +04:00
|
|
|
|
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
|
|
|
|
|
};
|
2021-04-01 21:30:29 +04:00
|
|
|
|
var clone = controlType.GetInstance() as IControlEntitySelectable;
|
|
|
|
|
clone.Open(new ControlOpenModel
|
2021-03-30 22:34:31 +04:00
|
|
|
|
{
|
2021-04-01 21:30:29 +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);
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-01 21:30:29 +04:00
|
|
|
|
public void SetDefaultValue() => _originalValue = null;
|
2021-03-30 22:34:31 +04:00
|
|
|
|
|
2021-04-01 21:30:29 +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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-01 21:30:29 +04:00
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-01 21:30:29 +04:00
|
|
|
|
public bool CheckValueForControl()
|
2021-03-30 22:34:31 +04:00
|
|
|
|
{
|
|
|
|
|
if (_mustFilling && textBox.Tag == null)
|
|
|
|
|
{
|
|
|
|
|
BackColor = Color.OrangeRed;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-01 21:30:29 +04:00
|
|
|
|
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());
|
|
|
|
|
}
|
2021-04-01 21:30:29 +04:00
|
|
|
|
|
|
|
|
|
public string GetPropertyName() => _propertyName;
|
2021-03-30 22:34:31 +04:00
|
|
|
|
}
|
|
|
|
|
}
|