PIbd-33_Dyakonov_R_R_COP_14/WinFormsLibrary1/CustomTextBox.cs

67 lines
1.5 KiB
C#
Raw Permalink Normal View History

2024-09-29 21:11:27 +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;
using WinFormsLibrary1.Errors;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
namespace WinFormsLibrary1
{
public partial class CustomTextBox : UserControl
{
public int? MinLength { get; set; } = null;
public int? MaxLength { get; set; } = null;
public event EventHandler ValueChanged;
public string Value
{
get
{
if (MinLength == null || MaxLength == null)
throw new RangeNotSetException();
if (textBox.Text.Length < MinLength || textBox.Text.Length > MaxLength)
throw new TextLengthOutOfRangeException(MinLength ?? -1, MaxLength ?? -1);
return textBox.Text;
}
set
{
if (MinLength == null || MaxLength == null
|| textBox.Text.Length < MinLength || textBox.Text.Length > MaxLength)
return;
textBox.Text = value;
}
}
public string CheckValue(string v)
{
if (MinLength == null || MaxLength == null)
throw new RangeNotSetException();
if (v.Length < MinLength || v.Length > MaxLength)
throw new TextLengthOutOfRangeException(MinLength ?? -1, MaxLength ?? -1);
return v;
}
public CustomTextBox()
{
InitializeComponent();
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
ValueChanged?.Invoke(this, EventArgs.Empty);
}
}
}