PIbd-31_Shanygin.A.V._COP_27/ComponentOrientedProgramming/CreateVisualComponent/InputComponent.cs

69 lines
2.0 KiB
C#
Raw Permalink Normal View History

2024-09-09 22:02:46 +04:00
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
{
2024-09-14 18:15:23 +04:00
if (checkBoxDate.Checked)
2024-09-09 22:02:46 +04:00
{
2024-09-14 18:15:23 +04:00
return null;
2024-09-09 22:02:46 +04:00
}
2024-09-14 18:15:23 +04:00
if (string.IsNullOrEmpty(textBoxDate.Text))
2024-09-09 22:02:46 +04:00
{
2024-09-14 18:15:23 +04:00
throw new NotFilledException("The textbox is empty, fill it in or click on the checkbox!");
2024-09-09 22:02:46 +04:00
}
2024-09-14 18:15:23 +04:00
if (DateTime.TryParseExact(textBoxDate.Text, "dd/MM/yyyy", null, DateTimeStyles.None, out DateTime parsedDate))
2024-09-09 22:02:46 +04:00
{
2024-09-14 18:15:23 +04:00
return parsedDate;
2024-09-09 22:02:46 +04:00
}
2024-09-14 18:15:23 +04:00
throw new IncorrectFillingException(textBoxDate.Text + " incorrect date!");
}
set
{
checkBoxDate.Checked = value is null;
textBoxDate.Text = value?.ToString("dd/MM/yyyy") ?? string.Empty;
2024-09-09 22:02:46 +04:00
}
}
public event EventHandler CheckBoxEvent
{
2024-10-13 16:15:32 +04:00
add { _changeEvent += value; }
remove { _changeEvent -= value; }
2024-09-09 22:02:46 +04:00
}
private void TextBoxDate_TextChanged(object sender, EventArgs e)
{
_changeEvent?.Invoke(sender, e);
}
private void CheckBoxDate_CheckedChanged(object sender, EventArgs e)
{
textBoxDate.Enabled = !checkBoxDate.Checked;
2024-10-13 16:15:32 +04:00
_changeEvent?.Invoke(sender, e);
2024-09-09 22:02:46 +04:00
}
}
}