PIbd-33_Sergunov_M.A._COP_4/Library/CustomComponents/ZhelovanovVisualComponents/TextBoxComponent.cs

130 lines
2.0 KiB
C#
Raw Normal View History

2023-11-17 09:56:34 +04:00
using DocumentFormat.OpenXml.Drawing.Diagrams;
using KOP_Labs.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;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.Button;
namespace KOP_Labs
{
public partial class TextBoxComponent : UserControl
{
public TextBoxComponent()
{
InitializeComponent();
Error = string.Empty;
}
public string Error { get; private set; }
public float? Value
{
get
{
if (checkBox1.Checked)
{
return null;
}
if (CheckValue())
{
return float.Parse(textBox.Text);
}
return null;
}
set
{
if (value == null)
{
checkBox1.Checked = true;
}
textBox.Text = value.ToString();
}
}
private EventHandler checkChanged;
public event EventHandler CheckChanged
{
add
{
checkChanged += value;
}
remove
{
checkChanged -= value;
}
}
private void CheckBox_CheckedChanged(object sender, EventArgs e)
{
Error = string.Empty;
if (checkBox1.Checked)
{
textBox.Enabled = false;
textBox.Text = string.Empty;
}
else
{
textBox.Enabled = true;
}
checkChanged?.Invoke(sender, e);
}
private EventHandler valueChanged;
public event EventHandler ValueChanged
{
add
{
valueChanged += value;
}
remove
{
valueChanged -= value;
}
}
public bool CheckValue()
{
Error = string.Empty;
if (!checkBox1.Checked && (string.IsNullOrEmpty(textBox.Text)))
{
Error = "Пусто!";
return false;
}
if (!checkBox1.Checked && !float.TryParse(textBox.Text, out float floatValue))
{
Error = "Не тот тип!";
return false;
}
return true;
}
private void TextBox_TextChanged(object sender, EventArgs e)
{
valueChanged?.Invoke(sender, e);
}
}
}