74 lines
1.9 KiB
C#
Raw Permalink Normal View History

2024-11-27 23:43:53 +04:00
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 WinFormsLibrary1
{
public class Error: Exception
{
string message;
public Error(string text) { message = text; }
}
public partial class TextComponent : UserControl
{
public event EventHandler SelectedElementChange
{
add { checkBox1.CheckedChanged += value; textBox1.TextChanged += value; }
remove { checkBox1.CheckedChanged -= value; textBox1.TextChanged -= value; }
}
public TextComponent()
{
InitializeComponent();
}
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
if (checkBox1.Checked)
{
textBox1.ReadOnly = true;
textBox1.Clear();
}
else textBox1.ReadOnly = false;
}
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
char number = e.KeyChar;
if (!Char.IsDigit(number))
{
e.Handled = true;
}
}
public int? TextValue
{
get
{
if(!checkBox1.Checked)
{
int result = -1;
int.TryParse(textBox1.Text, out result);
if( result == -1)
{
throw new Error("Несоответсвие");
}
return result;
}
return null;
}
set
{
checkBox1.Checked = value == null;
textBox1.Text = value?.ToString();
}
}
}
}