101 lines
2.3 KiB
C#
Raw Normal View History

2023-09-13 11:57:01 +04:00
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
2023-09-26 21:38:38 +04:00
using System.Text.RegularExpressions;
2023-09-13 11:57:01 +04:00
using System.Threading.Tasks;
using System.Windows.Forms;
using VisualComponentsLib.CustomException;
2023-09-13 11:57:01 +04:00
namespace VisualComponentsLib
{
2023-09-13 13:48:16 +04:00
public partial class MyTextBox : UserControl
2023-09-13 11:57:01 +04:00
{
2023-09-27 12:59:45 +04:00
public double? TextBoxValue
2023-09-13 12:53:53 +04:00
{
get
{
2023-09-26 21:38:38 +04:00
if (checkBox.Checked)
{
2023-09-26 21:38:38 +04:00
return null;
}
2023-09-26 21:38:38 +04:00
2023-09-27 12:59:45 +04:00
if (!CheckValue())
2023-09-26 21:38:38 +04:00
{
2023-09-27 12:59:45 +04:00
return null;
}
2023-09-26 21:38:38 +04:00
2023-09-27 12:59:45 +04:00
return Convert.ToDouble(textBox.Text);
}
set
{
2023-09-27 13:52:25 +04:00
checkBox.Checked = !value.HasValue;
2023-09-26 21:38:38 +04:00
2023-09-27 13:52:25 +04:00
textBox.Text = value?.ToString();
}
2023-09-13 12:53:53 +04:00
}
2023-09-27 12:59:45 +04:00
private string? ErrorMessage
{
get;
set;
}
2023-09-26 21:38:38 +04:00
public MyTextBox()
{
InitializeComponent();
}
private EventHandler _textChanged;
2023-09-13 12:53:53 +04:00
public new event EventHandler? TextChanged
{
add => _textChanged += value;
remove => _textChanged -= value;
2023-09-13 12:53:53 +04:00
}
2023-09-26 21:38:38 +04:00
private void CheckBox_CheckedChanged_1(object sender, EventArgs e)
2023-09-13 12:53:53 +04:00
{
if (textBox.ReadOnly == true)
{
textBox.ReadOnly = false;
}
else
2023-09-13 12:53:53 +04:00
{
textBox.ReadOnly = true;
}
}
2023-09-26 09:42:55 +04:00
2023-09-26 21:38:38 +04:00
private void TextBox_TextChanged(object sender, EventArgs e)
{
2023-09-26 21:38:38 +04:00
_textChanged?.Invoke(sender, e);
2023-09-13 12:53:53 +04:00
}
2023-09-27 12:59:45 +04:00
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;
}
2023-09-13 11:57:01 +04:00
}
}