98 lines
2.3 KiB
C#

using CustomComponents.Exceptions;
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;
namespace CustomComponents
{
public partial class frameTextBox : UserControl
{
private string? _text;
public event EventHandler taskHandler;
public frameTextBox()
{
InitializeComponent();
}
public string? Value
{
get
{
return _text;
}
set
{
_text = value;
}
}
private void CheckBoxStateChanged(object sender, EventArgs e)
{
try
{
if (ruleCheckBox.Checked)
{
textBox.Enabled = false;
if (!string.IsNullOrEmpty(_text))
{
throw new ContentException(true);
}
}
else
{
textBox.Enabled = true;
if (string.IsNullOrEmpty(_text))
{
throw new ContentException(false);
}
}
}
catch(ContentException ex)
{
HandlerCreation(ex.Message);
}
taskHandler?.Invoke(this, e);
}
private void CheckTextBoxValueType(object sender, EventArgs e)
{
Value = textBox.Text;
try
{
if(!Value.All(char.IsDigit))
{
throw new NotIntegerException(Value);
}
}
catch (NotIntegerException ex)
{
HandlerCreation(ex.Message);
}
taskHandler?.Invoke(this, e);
}
private void HandlerCreation(string message)
{
EventHandler handler = null;
handler = (sender, e) =>
{
MessageBox.Show(message);
taskHandler -= handler;
};
taskHandler += handler;
}
}
}