PIbd-33_Abazov_A.A._KOP_29/AbazovApp/AbazovViewComponents/Components/AbazovDatePicker.cs
2023-09-21 18:50:11 +04:00

74 lines
2.1 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 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 AbazovViewComponents.Exceptions;
namespace AbazovViewComponents.Components
{
public partial class AbazovDatePicker : UserControl
{
public AbazovDatePicker()
{
InitializeComponent();
}
public event Action<DateTime> DateChange;
private bool lowBoundrySet = false;
private bool highBoundrySet = false;
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 (dateFrom is null && dateTo is null) throw new DateBoundsNotSetException("Не заданы диапазоны компонента!");
if (this.dateTimePicker.Value < dateFrom || this.dateTimePicker.Value > dateTo) throw new DateOutOfBoundsException("Дата вне диапазона!");
return this.dateTimePicker.Value;
}
set
{
if (dateFrom is not null && dateTo is not null && value >= dateFrom && value <= dateTo) this.dateTimePicker.Value = value;
}
}
private void dateTimePicker_ValueChanged(object sender, EventArgs e)
{
DateChange.Invoke(dateTimePicker.Value);
}
}
}