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

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

View File

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

View File

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

View File

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