From 0d948ba72ef5ac6ef9f8dcf76073edfb7bed8c81 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=B0=D0=BA=D1=81=D0=B8=D0=BC=20=D0=AF=D0=BA=D0=BE?= =?UTF-8?q?=D0=B2=D0=BB=D0=B5=D0=B2?= Date: Fri, 6 Sep 2024 17:40:13 +0400 Subject: [PATCH] =?UTF-8?q?=D0=BF=D1=80=D0=B0=D0=B2=D0=BA=D0=B8=20=D0=BD?= =?UTF-8?q?=D0=B0=20=D0=B7=D0=B0=D0=BD=D1=8F=D1=82=D0=B8=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ControlComboBox.Designer.cs | 11 ++-- .../ComponentProgramming/ControlComboBox.cs | 10 +++- .../ComponentProgramming/ControlDataGrid.cs | 38 +++++++++--- .../ControlTextBox.Designer.cs | 26 +++----- .../ComponentProgramming/ControlTextBox.cs | 60 +++++++------------ .../ComponentProgramming/Person.cs | 24 ++++++++ ComponentProgramming/Forms/Form.Designer.cs | 60 ++++++++++++++----- ComponentProgramming/Forms/Form.cs | 34 ++++++++--- ComponentProgramming/Forms/Person.cs | 6 +- 9 files changed, 173 insertions(+), 96 deletions(-) create mode 100644 ComponentProgramming/ComponentProgramming/Person.cs diff --git a/ComponentProgramming/ComponentProgramming/ControlComboBox.Designer.cs b/ComponentProgramming/ComponentProgramming/ControlComboBox.Designer.cs index 00dceff..3a80a3e 100644 --- a/ComponentProgramming/ComponentProgramming/ControlComboBox.Designer.cs +++ b/ComponentProgramming/ComponentProgramming/ControlComboBox.Designer.cs @@ -34,20 +34,23 @@ // comboBoxElements // comboBoxElements.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; + comboBoxElements.DropDownStyle = ComboBoxStyle.DropDownList; comboBoxElements.FormattingEnabled = true; - comboBoxElements.Location = new Point(4, 3); + comboBoxElements.Location = new Point(5, 4); + comboBoxElements.Margin = new Padding(3, 4, 3, 4); comboBoxElements.Name = "comboBoxElements"; - comboBoxElements.Size = new Size(170, 23); + comboBoxElements.Size = new Size(194, 28); comboBoxElements.TabIndex = 0; comboBoxElements.SelectedIndexChanged += comboBoxElements_SelectedIndexChanged; // // ControlComboBox // - AutoScaleDimensions = new SizeF(7F, 15F); + AutoScaleDimensions = new SizeF(8F, 20F); AutoScaleMode = AutoScaleMode.Font; Controls.Add(comboBoxElements); + Margin = new Padding(3, 4, 3, 4); Name = "ControlComboBox"; - Size = new Size(177, 29); + Size = new Size(202, 39); ResumeLayout(false); } diff --git a/ComponentProgramming/ComponentProgramming/ControlComboBox.cs b/ComponentProgramming/ComponentProgramming/ControlComboBox.cs index 48b8905..ec32674 100644 --- a/ComponentProgramming/ComponentProgramming/ControlComboBox.cs +++ b/ComponentProgramming/ComponentProgramming/ControlComboBox.cs @@ -8,7 +8,6 @@ using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using static System.Runtime.InteropServices.JavaScript.JSType; -using static System.Windows.Forms.VisualStyles.VisualStyleElement; namespace ComponentProgramming { @@ -16,7 +15,7 @@ namespace ComponentProgramming { private event EventHandler? _comboBoxChanged; - public string elements + public string SelectedItem { get { @@ -28,7 +27,7 @@ namespace ComponentProgramming } set { - comboBoxElements.Items.AddRange(value.Split(", ")); + comboBoxElements.SelectedItem = value; } } @@ -43,6 +42,11 @@ namespace ComponentProgramming InitializeComponent(); } + public ComboBox.ObjectCollection ComboBoxItems + { + get { return comboBoxElements.Items; } + } + public void ClearComboBox() { comboBoxElements.Items.Clear(); diff --git a/ComponentProgramming/ComponentProgramming/ControlDataGrid.cs b/ComponentProgramming/ComponentProgramming/ControlDataGrid.cs index 6df932b..1028742 100644 --- a/ComponentProgramming/ComponentProgramming/ControlDataGrid.cs +++ b/ComponentProgramming/ComponentProgramming/ControlDataGrid.cs @@ -1,4 +1,5 @@ -using System; +using Forms; +using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; @@ -12,6 +13,8 @@ namespace ComponentProgramming { public partial class ControlDataGrid : UserControl { + private List _props; + private int _numCols; public ControlDataGrid() { InitializeComponent(); @@ -27,9 +30,10 @@ namespace ComponentProgramming dataGridView.Columns[i].Visible = isVisible[i]; dataGridView.Columns[i].Name = props[i]; } - dataGridView.Rows.Add("1", "sadasd"); - dataGridView.Rows.Add("2", "sadsadfasdfasdfasd"); - + _props = props; + _numCols = numCol; + dataGridView.Rows.Add(new object[] { 1, "Александр", "Петров" }); + dataGridView.Rows.Add(new object[] { 2, "Владимир", "Кузьмин" }); } public void ClearData() @@ -37,7 +41,7 @@ namespace ComponentProgramming dataGridView.Rows.Clear(); } - public int SelectedRow + public int? SelectedRow { get { @@ -47,12 +51,28 @@ namespace ComponentProgramming } else { - throw new Exception($"Вы не выбрали строку"); + throw new Exception($"Вы не выбрали строку"); ; } } set { - dataGridView.Columns[0].DisplayIndex = value; + dataGridView.Columns[0].DisplayIndex = (int)value!; + } + } + + public void SetData(T obj, int col, int row) where T : class, new() + { + if(row > dataGridView.Rows.Count-1) + { + dataGridView.Rows.Add(new object[] { null, "", "" }); + row = dataGridView.Rows.Count -1; + } + foreach (var prop in obj.GetType().GetProperties()) + { + if (prop.ToString()!.Contains(_props[col])) + { + dataGridView.Rows[row].Cells[col].Value = prop.GetValue(obj); + } } } @@ -63,7 +83,7 @@ namespace ComponentProgramming return null; } - DataGridViewRow selectedString = dataGridView.SelectedRows[0]; + DataGridViewRow selectedRow = dataGridView.SelectedRows[0]; T obj = new T(); foreach (var prop in obj.GetType().GetProperties()) @@ -72,7 +92,7 @@ namespace ComponentProgramming { continue; } - prop.SetValue(obj, selectedString.Cells[$"{prop.Name}"].Value); + prop.SetValue(obj, selectedRow.Cells[$"{prop.Name}"].Value); } return obj; } diff --git a/ComponentProgramming/ComponentProgramming/ControlTextBox.Designer.cs b/ComponentProgramming/ComponentProgramming/ControlTextBox.Designer.cs index 0c7b663..6a44b50 100644 --- a/ComponentProgramming/ComponentProgramming/ControlTextBox.Designer.cs +++ b/ComponentProgramming/ComponentProgramming/ControlTextBox.Designer.cs @@ -29,35 +29,26 @@ private void InitializeComponent() { textBox = new TextBox(); - checkBox = new CheckBox(); SuspendLayout(); // // textBox // - textBox.Location = new Point(30, 3); + textBox.Location = new Point(3, 4); + textBox.Margin = new Padding(3, 4, 3, 4); textBox.Name = "textBox"; - textBox.Size = new Size(117, 23); + textBox.PlaceholderText = "+79063211213"; + textBox.Size = new Size(133, 27); textBox.TabIndex = 0; - textBox.KeyPress += textBox_KeyPress; - // - // checkBox - // - checkBox.AutoSize = true; - checkBox.Location = new Point(9, 7); - checkBox.Name = "checkBox"; - checkBox.Size = new Size(15, 14); - checkBox.TabIndex = 1; - checkBox.UseVisualStyleBackColor = true; - checkBox.CheckedChanged += checkBox_CheckedChanged; + textBox.TextChanged += textBox_TextChanged; // // ControlTextBox // - AutoScaleDimensions = new SizeF(7F, 15F); + AutoScaleDimensions = new SizeF(8F, 20F); AutoScaleMode = AutoScaleMode.Font; - Controls.Add(checkBox); Controls.Add(textBox); + Margin = new Padding(3, 4, 3, 4); Name = "ControlTextBox"; - Size = new Size(150, 29); + Size = new Size(138, 34); ResumeLayout(false); PerformLayout(); } @@ -65,6 +56,5 @@ #endregion private TextBox textBox; - private CheckBox checkBox; } } diff --git a/ComponentProgramming/ComponentProgramming/ControlTextBox.cs b/ComponentProgramming/ComponentProgramming/ControlTextBox.cs index 5283f63..3be207e 100644 --- a/ComponentProgramming/ComponentProgramming/ControlTextBox.cs +++ b/ComponentProgramming/ComponentProgramming/ControlTextBox.cs @@ -14,18 +14,28 @@ namespace ComponentProgramming { public partial class ControlTextBox : UserControl { + public ControlTextBox() + { + InitializeComponent(); + } + private string? _numPuttern; - private Regex regex = new(@"^\+7\d{10}$"); + public string? NumPattern + { + get { return _numPuttern; } + set { _numPuttern = value!; } + } public string? text { get { - if (checkBox.Checked) + if (_numPuttern == null) { - return textBox.Text = null; + throw new NumberException("Стандарт не задан!"); } + Regex regex = new(_numPuttern); if (regex.IsMatch(textBox.Text)) { return textBox.Text; @@ -37,53 +47,29 @@ namespace ComponentProgramming } set { - if (checkBox.Checked) + if (_numPuttern == null) { - textBox.Text = null; + return; } + Regex regex = new(_numPuttern!); if (regex.IsMatch(value!)) { textBox.Text = value; } - else - { - throw new NumberException(textBox.Text + " не соответствует стандарту!"); - } } } - public ControlTextBox() + private event EventHandler? _textBoxChanged; + + public event EventHandler TextBoxChanged { - InitializeComponent(); + add { _textBoxChanged += value; } + remove { _textBoxChanged -= value; } } - private event EventHandler? _checkBoxChanged; - - public event EventHandler CheckBoxChanged + private void textBox_TextChanged(object sender, EventArgs e) { - add { _checkBoxChanged += value; } - remove { _checkBoxChanged -= value; } - } - - private void checkBox_CheckedChanged(object sender, EventArgs e) - { - if (checkBox.Checked) - { - textBox.Enabled = false; - _checkBoxChanged?.Invoke(this, e); - } - else - { - textBox.Enabled = true; - } - } - - private void textBox_KeyPress(object sender, KeyPressEventArgs e) - { - if(e.KeyChar == '\r') - { - text = textBox.Text; - } + _textBoxChanged?.Invoke(this, e); } } } diff --git a/ComponentProgramming/ComponentProgramming/Person.cs b/ComponentProgramming/ComponentProgramming/Person.cs new file mode 100644 index 0000000..e1e44af --- /dev/null +++ b/ComponentProgramming/ComponentProgramming/Person.cs @@ -0,0 +1,24 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Forms +{ + public class Person + { + public int Id { get; set; } + public string Name { get; set; } + public string Surname { get; set; } + + public Person() { } + + public Person(int id, string name, string surname) + { + Name = name; + Id = id; + Surname = surname; + } + } +} diff --git a/ComponentProgramming/Forms/Form.Designer.cs b/ComponentProgramming/Forms/Form.Designer.cs index b290722..7d8ff2a 100644 --- a/ComponentProgramming/Forms/Form.Designer.cs +++ b/ComponentProgramming/Forms/Form.Designer.cs @@ -33,37 +33,43 @@ controlDataGrid = new ComponentProgramming.ControlDataGrid(); buttonGetObj = new Button(); buttonClear = new Button(); + buttonEnter = new Button(); + buttonAdd = new Button(); SuspendLayout(); // // controlComboBox // - controlComboBox.elements = ""; - controlComboBox.Location = new Point(12, 3); + controlComboBox.SelectedItem = ""; + controlComboBox.Location = new Point(14, 4); + controlComboBox.Margin = new Padding(3, 5, 3, 5); controlComboBox.Name = "controlComboBox"; - controlComboBox.Size = new Size(177, 31); + controlComboBox.Size = new Size(202, 41); controlComboBox.TabIndex = 0; controlComboBox.ComboBoxChanged += controlComboBox_ComboBoxChanged; // // controlTextBox // - controlTextBox.Location = new Point(226, 3); + controlTextBox.Location = new Point(258, 4); + controlTextBox.Margin = new Padding(3, 5, 3, 5); controlTextBox.Name = "controlTextBox"; - controlTextBox.Size = new Size(150, 29); + controlTextBox.Size = new Size(171, 39); controlTextBox.TabIndex = 1; - controlTextBox.CheckBoxChanged += controlTextBox_CheckBoxChanged; // // controlDataGrid // - controlDataGrid.Location = new Point(12, 111); + controlDataGrid.Anchor = AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right; + controlDataGrid.Location = new Point(9, 148); + controlDataGrid.Margin = new Padding(3, 5, 3, 5); controlDataGrid.Name = "controlDataGrid"; - controlDataGrid.Size = new Size(776, 296); + controlDataGrid.Size = new Size(899, 395); controlDataGrid.TabIndex = 2; // // buttonGetObj // - buttonGetObj.Location = new Point(23, 415); + buttonGetObj.Location = new Point(26, 553); + buttonGetObj.Margin = new Padding(3, 4, 3, 4); buttonGetObj.Name = "buttonGetObj"; - buttonGetObj.Size = new Size(126, 23); + buttonGetObj.Size = new Size(144, 31); buttonGetObj.TabIndex = 3; buttonGetObj.Text = "Получить объект"; buttonGetObj.UseVisualStyleBackColor = true; @@ -71,24 +77,48 @@ // // buttonClear // - buttonClear.Location = new Point(654, 415); + buttonClear.Location = new Point(747, 553); + buttonClear.Margin = new Padding(3, 4, 3, 4); buttonClear.Name = "buttonClear"; - buttonClear.Size = new Size(134, 23); + buttonClear.Size = new Size(153, 34); buttonClear.TabIndex = 4; buttonClear.Text = "Очистить объекты"; buttonClear.UseVisualStyleBackColor = true; buttonClear.Click += buttonClear_Click; // + // buttonEnter + // + buttonEnter.Location = new Point(311, 48); + buttonEnter.Name = "buttonEnter"; + buttonEnter.Size = new Size(84, 32); + buttonEnter.TabIndex = 5; + buttonEnter.Text = "Ввод"; + buttonEnter.UseVisualStyleBackColor = true; + buttonEnter.Click += buttonEnter_Click; + // + // buttonAdd + // + buttonAdd.Location = new Point(378, 553); + buttonAdd.Name = "buttonAdd"; + buttonAdd.Size = new Size(168, 31); + buttonAdd.TabIndex = 6; + buttonAdd.Text = "Добавить значение"; + buttonAdd.UseVisualStyleBackColor = true; + buttonAdd.Click += buttonAdd_Click; + // // Form // - AutoScaleDimensions = new SizeF(7F, 15F); + AutoScaleDimensions = new SizeF(8F, 20F); AutoScaleMode = AutoScaleMode.Font; - ClientSize = new Size(800, 450); + ClientSize = new Size(914, 600); + Controls.Add(buttonAdd); + Controls.Add(buttonEnter); Controls.Add(buttonClear); Controls.Add(buttonGetObj); Controls.Add(controlDataGrid); Controls.Add(controlTextBox); Controls.Add(controlComboBox); + Margin = new Padding(3, 4, 3, 4); Name = "Form"; Text = "Form"; ResumeLayout(false); @@ -102,5 +132,7 @@ private ComponentProgramming.ControlDataGrid controlDataGrid; private Button buttonGetObj; private Button buttonClear; + private Button buttonEnter; + private Button buttonAdd; } } diff --git a/ComponentProgramming/Forms/Form.cs b/ComponentProgramming/Forms/Form.cs index 0cbbd1a..bfe85aa 100644 --- a/ComponentProgramming/Forms/Form.cs +++ b/ComponentProgramming/Forms/Form.cs @@ -12,24 +12,29 @@ namespace Forms private void FillBox() { - controlComboBox.elements = " 1, 2, 3, 4"; + controlComboBox.ComboBoxItems.Add(" 1"); + controlComboBox.ComboBoxItems.Add(" 2"); + controlComboBox.ComboBoxItems.Add(" 3"); + controlComboBox.ComboBoxItems.Add(" 4"); + controlComboBox.SelectedItem = "dafafadsf"; } private void FillTextBox() { + controlTextBox.NumPattern = @"^\+7\d{10}$"; controlTextBox.text = "+79063908075"; } private void FillGrid() { - List headers = new List() { "Id", "" }; - List width = new List() { 200, 200 }; - List isVisible = new List() { true, true }; - List props = new List() { "Id", "Name" }; - controlDataGrid.CreateColumns(2, headers, width, isVisible, props); + List headers = new List() { "Id", "", "" }; + List width = new List() { 100, 200, 200 }; + List isVisible = new List() { true, true, true }; + List props = new List() { "Id", "Name", "Surname" }; + controlDataGrid.CreateColumns(3, headers, width, isVisible, props); } private void controlComboBox_ComboBoxChanged(object sender, EventArgs e) { - var elem = controlComboBox.elements; + var elem = controlComboBox.SelectedItem; MessageBox.Show($": {elem}"); } @@ -47,13 +52,24 @@ namespace Forms private void buttonGetObj_Click(object sender, EventArgs e) { - var da = controlDataGrid.GetObject(); - MessageBox.Show($"{da?.Id}:{da?.Name} "); + var val = controlDataGrid.GetObject(); + MessageBox.Show($"{val?.Name} {val?.Surname}"); } private void buttonClear_Click(object sender, EventArgs e) { controlDataGrid.ClearData(); } + + private void buttonEnter_Click(object sender, EventArgs e) + { + var val = controlTextBox.text; + MessageBox.Show($" {val}"); + } + + private void buttonAdd_Click(object sender, EventArgs e) + { + controlDataGrid.SetData(new Person(1, "asdasdas", "asdasd1asd"), 2, 3); + } } } diff --git a/ComponentProgramming/Forms/Person.cs b/ComponentProgramming/Forms/Person.cs index 33d312a..6eaaa59 100644 --- a/ComponentProgramming/Forms/Person.cs +++ b/ComponentProgramming/Forms/Person.cs @@ -8,15 +8,17 @@ namespace Forms { public class Person { - public string Id { get; set; } + public int Id { get; set; } public string Name { get; set; } + public string Surname { get; set; } public Person() { } - public Person(string name, string id) + public Person(int id, string name, string surname) { Name = name; Id = id; + Surname = surname; } } }