Второй компонент.

This commit is contained in:
ElEgEv 2023-09-26 21:38:38 +04:00
parent 9ca65527e1
commit df6dc6cc54
4 changed files with 41 additions and 54 deletions

View File

@ -36,9 +36,9 @@
buttonClear = new Button();
groupBox = new GroupBox();
groupBox1 = new GroupBox();
myTextBox = new VisualComponentsLib.MyTextBox();
labelCheckTwo = new Label();
buttonCheckTwo = new Button();
myTextBox = new VisualComponentsLib.MyTextBox();
groupBox.SuspendLayout();
groupBox1.SuspendLayout();
SuspendLayout();
@ -116,9 +116,9 @@
//
// groupBox1
//
groupBox1.Controls.Add(myTextBox);
groupBox1.Controls.Add(labelCheckTwo);
groupBox1.Controls.Add(buttonCheckTwo);
groupBox1.Controls.Add(myTextBox);
groupBox1.Location = new Point(386, 12);
groupBox1.Name = "groupBox1";
groupBox1.Size = new Size(342, 106);
@ -126,6 +126,13 @@
groupBox1.TabStop = false;
groupBox1.Text = "Второй компонент";
//
// myTextBox
//
myTextBox.Location = new Point(6, 20);
myTextBox.Name = "myTextBox";
myTextBox.Size = new Size(210, 80);
myTextBox.TabIndex = 5;
//
// labelCheckTwo
//
labelCheckTwo.AutoSize = true;
@ -146,14 +153,6 @@
buttonCheckTwo.UseVisualStyleBackColor = true;
buttonCheckTwo.Click += ButtonCheckTwo_Click;
//
// myTextBox
//
myTextBox.Value = "";
myTextBox.Location = new Point(6, 19);
myTextBox.Name = "myTextBox";
myTextBox.Size = new Size(210, 84);
myTextBox.TabIndex = 0;
//
// FormMain
//
AutoScaleDimensions = new SizeF(7F, 15F);
@ -162,7 +161,7 @@
Controls.Add(groupBox1);
Controls.Add(groupBox);
Name = "FormMain";
Text = "Form1";
Text = "Тестовое окно";
groupBox.ResumeLayout(false);
groupBox.PerformLayout();
groupBox1.ResumeLayout(false);
@ -179,8 +178,8 @@
private Button buttonClear;
private GroupBox groupBox;
private GroupBox groupBox1;
private VisualComponentsLib.MyTextBox myTextBox;
private Label labelCheckTwo;
private Button buttonCheckTwo;
private VisualComponentsLib.MyTextBox myTextBox;
}
}

View File

@ -28,9 +28,9 @@ namespace VisualComponentsForm
{
try
{
labelCheckTwo.Text = myTextBox.Value;
labelCheckTwo.Text = myTextBox.TextBoxValue;
}
catch(Exception ex)
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}

View File

@ -42,7 +42,7 @@
checkBox.TabIndex = 0;
checkBox.Text = "Включить null";
checkBox.UseVisualStyleBackColor = true;
checkBox.CheckedChanged += CheckBox_CheckedChanged;
checkBox.CheckedChanged += CheckBox_CheckedChanged_1;
//
// textBox
//
@ -50,7 +50,7 @@
textBox.Name = "textBox";
textBox.Size = new Size(182, 23);
textBox.TabIndex = 2;
textBox.KeyPress += TextBox_KeyPress;
textBox.TextChanged += TextBox_TextChanged;
//
// MyTextBox
//

View File

@ -5,6 +5,7 @@ using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;
using VisualComponentsLib.CustomException;
@ -13,38 +14,44 @@ namespace VisualComponentsLib
{
public partial class MyTextBox : UserControl
{
public string? Value
public string? TextBoxValue
{
get
{
if (textBox.Text.Length <= 2 && !checkBox.Checked)
if (checkBox.Checked)
{
return new TextBoxException("Error").Message;
return null;
}
else
if (textBox.Text.Length <= 2)
{
if(double.TryParse(textBox.Text, out double value))
{
return textBox.Text;
}
return new TextBoxException("Error").ToString();
throw new TextBoxException("Число не введено");
}
if (textBox.Text.Contains('.') && textBox.Text.IndexOf('.') != textBox.Text.Length - 1)
{
return textBox.Text;
}
throw new TextBoxException("Некорретный формат числа");
}
set
{
if(value != null)
if (!checkBox.Checked && value == null)
{
textBox.Text = value.ToString();
}
else
{
textBox.Text = null;
throw new TextBoxException("Недопустимое значение null");
}
textBox.Text = value;
}
}
public MyTextBox()
{
InitializeComponent();
}
private EventHandler _textChanged;
public new event EventHandler? TextChanged
@ -53,12 +60,7 @@ namespace VisualComponentsLib
remove => _textChanged -= value;
}
public MyTextBox()
{
InitializeComponent();
}
private void CheckBox_CheckedChanged(object sender, EventArgs e)
private void CheckBox_CheckedChanged_1(object sender, EventArgs e)
{
if (textBox.ReadOnly == true)
{
@ -67,26 +69,12 @@ namespace VisualComponentsLib
else
{
textBox.ReadOnly = true;
Value = null;
textBox.Text = string.Empty;
}
}
private void TextBox_KeyPress(object sender, KeyPressEventArgs e)
private void TextBox_TextChanged(object sender, EventArgs e)
{
// Проверяем, является ли введенный символ цифрой, точкой или клавишей Backspace
if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar) && e.KeyChar != '.')
{
e.Handled = true;
}
// Проверяем, является ли точка уже введенной и не находится ли она на первой позиции
if (e.KeyChar == '.' && (sender as TextBox).Text.IndexOf('.') > -1)
{
e.Handled = true;
}
_textChanged?.Invoke(sender, e);
}
}
}