PIbd-33_Lisov_N.A_COP_30/CustomComponents/TextBox.cs

113 lines
2.7 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 int? _text;
public string Error { get; private set; }
public event EventHandler taskHandler;
private event Action? _errorOccured;
public event Action AnErrorOccurred
{
add { _errorOccured += value; }
remove { _errorOccured -= value; }
}
public frameTextBox()
{
InitializeComponent();
Error = string.Empty;
}
public int? Value
{
get
{
return _text;
}
set
{
_text = value;
}
}
private void CheckBoxStateChanged(object sender, EventArgs e)
{
try
{
if (ruleCheckBox.Checked)
{
textBox.Enabled = false;
if (!string.IsNullOrEmpty(textBox.Text))
{
throw new ContentException(true);
}
}
else
{
textBox.Enabled = true;
if (string.IsNullOrEmpty(textBox.Text))
{
throw new ContentException(false);
}
}
}
catch(ContentException ex)
{
Error = ex.Message;
_errorOccured?.Invoke();
HandlerCreation(ex.Message);
}
taskHandler?.Invoke(this, e);
}
private void CheckTextBoxValueType(object sender, EventArgs e)
{
try
{
if(!textBox.Text.All(char.IsDigit))
{
throw new NotIntegerException(textBox.Text);
}
Value = Convert.ToInt32(textBox.Text);
}
catch (NotIntegerException ex)
{
Value = null;
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;
}
}
}