Zhelovanov_Dmitrii_COP/KOP_Labs/TextBoxComponent.cs

130 lines
2.0 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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);
}
}
}