PIbd-32_Chubykina_P.P._COP_11/COP/ChubykinaComponents/Components/UserDatePicker.cs

80 lines
2.1 KiB
C#
Raw Normal View History

2024-10-28 14:54:01 +04:00
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ChubykinaComponents.Exceptions;
namespace ChubykinaComponents.Components
{
public partial class UserDatePicker : UserControl
{
public UserDatePicker()
{
InitializeComponent();
}
public event Action<DateTime> DateChange;
private bool lowBoundrySet = false;
private bool highBoundrySet = false;
public bool BoundriesSet { get { return lowBoundrySet && highBoundrySet; } }
public DateTime? dateFrom
{
get
{
return lowBoundrySet ? dateTimePicker.MinDate : null;
}
set
{
if (value.HasValue)
{
dateTimePicker.MinDate = value.Value;
lowBoundrySet = true;
}
}
}
public DateTime? dateTo
{
get
{
return highBoundrySet ? dateTimePicker.MaxDate : null;
}
set
{
if (value.HasValue)
{
dateTimePicker.MaxDate = value.Value;
highBoundrySet = true;
}
}
}
public DateTime? Value
{
get
{
if (!lowBoundrySet || !highBoundrySet)
{
throw new DateBoundsNotSetException("Date bounds are not set.");
}
return dateTimePicker.Value;
}
set
{
if (lowBoundrySet && highBoundrySet && value.HasValue && value.Value >= dateFrom && value.Value <= dateTo) dateTimePicker.Value = value.Value;
}
}
private void dateTimePicker_ValueChanged(object sender, EventArgs e)
{
DateChange?.Invoke(dateTimePicker.Value);
}
}
}