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

96 lines
2.4 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using DesktopTools.Interfaces;
using System;
using System.Drawing;
using System.Windows.Forms;
namespace DesktopTools.BaseControls
{
/// <summary>
/// Контрол, предоставляющий работу с перечислениями
/// </summary>
public partial class BaseControlEnum : AbstractBaseControl, IBaseControl
{
/// <summary>
/// Конструктор
/// </summary>
/// <param name="propertyName"></param>
/// <param name="mustFilling"></param>
/// <param name="readOnly"></param>
/// <param name="enumType"></param>
public BaseControlEnum(string propertyName, bool mustFilling, bool readOnly, Type enumType) : base(propertyName, mustFilling, readOnly)
{
InitializeComponent();
_baseControl = this;
if (!_mustFilling)
{
checkBoxNullable.CheckedChanged += (object sender, EventArgs e) =>
{
comboBox.Enabled = !(sender as CheckBox).Checked;
CallOnValueChangeEvent();
};
panelControl.Controls.Add(checkBoxNullable);
}
if (enumType.Name.StartsWith("Nullable"))
{
enumType = Nullable.GetUnderlyingType(enumType);
}
comboBox.Items.Clear();
foreach (var val in Enum.GetValues(enumType))
{
comboBox.Items.Add(val);
}
comboBox.SelectedIndexChanged += (object sender, EventArgs e) => { CallOnValueChangeEvent(); };
panelControl.Controls.Add(comboBox);
}
public void SetDefaultValue() => _originalValue = null;
public void SetValueToControl(object value)
{
if (value != null)
{
comboBox.SelectedIndex = comboBox.Items.IndexOf(value);
}
else if (!_mustFilling)
{
checkBoxNullable.Checked = true;
}
}
public void DropValueForControl()
{
if (_originalValue != null)
{
comboBox.SelectedIndex = comboBox.Items.IndexOf(_originalValue);
}
else if (!_mustFilling)
{
checkBoxNullable.Checked = true;
}
}
public bool CheckValueForControl()
{
if (_mustFilling && comboBox.SelectedIndex == -1)
{
BackColor = Color.OrangeRed;
return false;
}
return true;
}
public object GetValueFromControl()
{
if (_mustFilling && comboBox.SelectedIndex == -1)
{
throw new ArgumentNullException($"Поле свойства '{labelTitle.Text}' должно быть заполнено");
}
return checkBoxNullable.Checked ? null : comboBox.SelectedItem;
}
public string GetPropertyName() => _propertyName;
}
}