PIbd-32_Artamonova_T.V._COP_2/COP/UserDateTimePicker.cs

101 lines
2.7 KiB
C#
Raw Normal View History

2023-10-12 18:40:10 +04:00
namespace COP
{
public partial class UserDateTimePicker : UserControl
{
private DateTime? minValue;
private DateTime? maxValue;
public event EventHandler? _valueChanged;
private event Action? _errorOccured;
public string Error { get; private set; }
public UserDateTimePicker()
{
InitializeComponent();
Error = string.Empty;
}
public DateTime? MinValue
{
get { return minValue; }
set { minValue = value; }
}
public DateTime? MaxValue
{
get { return maxValue; }
set { maxValue = value; }
}
public DateTime? SelectedValue
{
get
{
if (!minValue.HasValue || !maxValue.HasValue)
{
Error = "Range is not set.";
_errorOccured?.Invoke();
return null;
}
else if (dateTimePicker.Value < minValue ||
dateTimePicker.Value > maxValue)
{
Error = "Selected value is out of range.";
_errorOccured?.Invoke();
return null;
}
return dateTimePicker.Value;
}
set
{
if (!value.HasValue)
{
Error = "Value is null.";
_errorOccured?.Invoke();
return;
}
if (!minValue.HasValue || !maxValue.HasValue)
{
Error = "Range is not set.";
_errorOccured?.Invoke();
return;
}
if (value < minValue || value > maxValue)
{
Error = "Selected value is out of range.";
_errorOccured?.Invoke();
return;
}
dateTimePicker.Value = value.Value;
}
}
public event EventHandler ValueChanged
{
add { _valueChanged += value; }
remove { _valueChanged -= value; }
}
public event Action AnErrorOccurred
{
add { _errorOccured += value; }
remove { _errorOccured -= value; }
}
private void DateTimePicker_ValueChanged(object sender, EventArgs e)
{
try
{
_valueChanged?.Invoke(this, e);
}
catch (Exception ex)
{
Error = ex.Message;
_errorOccured?.Invoke();
}
}
}
}