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 Exception? Error; public InputComponent() { InitializeComponent(); } public DateTime? Value { get { if (!checkBoxDate.Checked) { 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; } else { throw new IncorrectFillingException(textBoxDate.Text + " incorrect date!"); } } return null; } set { if (value is null) { checkBoxDate.Checked = true; } else { textBoxDate.Text = value?.ToString("dd/MM/yyyy"); checkBoxDate.Checked = false; } } } public event EventHandler CheckBoxEvent { add { _checkBoxEvent += value; } remove { _checkBoxEvent += 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; _checkBoxEvent?.Invoke(sender, e); } } }