2023-09-27 13:52:25 +04:00

101 lines
2.3 KiB
C#

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;
using VisualComponentsLib.CustomException;
namespace VisualComponentsLib
{
public partial class MyTextBox : UserControl
{
public double? TextBoxValue
{
get
{
if (checkBox.Checked)
{
return null;
}
if (!CheckValue())
{
return null;
}
return Convert.ToDouble(textBox.Text);
}
set
{
checkBox.Checked = !value.HasValue;
textBox.Text = value?.ToString();
}
}
private string? ErrorMessage
{
get;
set;
}
public MyTextBox()
{
InitializeComponent();
}
private EventHandler _textChanged;
public new event EventHandler? TextChanged
{
add => _textChanged += value;
remove => _textChanged -= value;
}
private void CheckBox_CheckedChanged_1(object sender, EventArgs e)
{
if (textBox.ReadOnly == true)
{
textBox.ReadOnly = false;
}
else
{
textBox.ReadOnly = true;
}
}
private void TextBox_TextChanged(object sender, EventArgs e)
{
_textChanged?.Invoke(sender, e);
}
private bool CheckValue()
{
if (textBox.Text == null && !checkBox.Checked)
{
ErrorMessage = "Недопустимое значение null";
return false;
}
if (!textBox.Text.Contains('.') ||
textBox.Text.IndexOf('.') == textBox.Text.Length - 1 ||
textBox.Text.Length <= 2)
{
ErrorMessage = "Некорректное double значение";
return false;
}
return true;
}
}
}