PIbd-31_Shanygin.A.V._COP_27/ComponentOrientedProgramming/CreateVisualComponent/InputComponent.cs
2024-10-13 16:15:32 +04:00

69 lines
2.0 KiB
C#

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Windows.Forms.VisualStyles;
using CreateVisualComponent.Exceptions;
namespace CreateVisualComponent
{
public partial class InputComponent : UserControl
{
public EventHandler? _changeEvent;
public EventHandler? _checkBoxEvent;
public InputComponent()
{
InitializeComponent();
}
public DateTime? Value
{
get
{
if (checkBoxDate.Checked)
{
return null;
}
if (string.IsNullOrEmpty(textBoxDate.Text))
{
throw new NotFilledException("The textbox is empty, fill it in or click on the checkbox!");
}
if (DateTime.TryParseExact(textBoxDate.Text, "dd/MM/yyyy", null, DateTimeStyles.None, out DateTime parsedDate))
{
return parsedDate;
}
throw new IncorrectFillingException(textBoxDate.Text + " incorrect date!");
}
set
{
checkBoxDate.Checked = value is null;
textBoxDate.Text = value?.ToString("dd/MM/yyyy") ?? string.Empty;
}
}
public event EventHandler CheckBoxEvent
{
add { _changeEvent += value; }
remove { _changeEvent -= value; }
}
private void TextBoxDate_TextChanged(object sender, EventArgs e)
{
_changeEvent?.Invoke(sender, e);
}
private void CheckBoxDate_CheckedChanged(object sender, EventArgs e)
{
textBoxDate.Enabled = !checkBoxDate.Checked;
_changeEvent?.Invoke(sender, e);
}
}
}