From 29ef211385715371a8acc3adeaa060841a22fd45 Mon Sep 17 00:00:00 2001 From: Kirill Date: Thu, 3 Oct 2024 21:07:20 +0400 Subject: [PATCH] =?UTF-8?q?=D0=A7=D1=82=D0=BE-=D1=82=D0=BE=20=D0=BD=D0=B0?= =?UTF-8?q?=D0=BF=D0=B8=D1=81=D0=B0=D0=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Components/CustomDataGridView.Designer.cs | 25 ++++++- Components/CustomDataGridView.cs | 83 +++++++++++++++++++++ Components/CustomListBox.cs | 15 ++-- Components/CustomTextBox.Designer.cs | 35 ++++++++- Components/CustomTextBox.cs | 54 +++++++++++++- Components/Exception/NotIntegerException.cs | 13 ++++ Components/Person.cs | 4 +- WinFormsTest/Form1.Designer.cs | 22 +++++- WinFormsTest/Form1.cs | 20 +++++ 9 files changed, 257 insertions(+), 14 deletions(-) create mode 100644 Components/Exception/NotIntegerException.cs diff --git a/Components/CustomDataGridView.Designer.cs b/Components/CustomDataGridView.Designer.cs index 540bbf8..f345f91 100644 --- a/Components/CustomDataGridView.Designer.cs +++ b/Components/CustomDataGridView.Designer.cs @@ -28,10 +28,31 @@ /// private void InitializeComponent() { - components = new System.ComponentModel.Container(); - this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + dataGridView = new DataGridView(); + ((System.ComponentModel.ISupportInitialize)dataGridView).BeginInit(); + SuspendLayout(); + // + // dataGridView + // + dataGridView.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize; + dataGridView.Location = new Point(3, 3); + dataGridView.Name = "dataGridView"; + dataGridView.Size = new Size(445, 363); + dataGridView.TabIndex = 0; + // + // ValueTableControl + // + AutoScaleDimensions = new SizeF(7F, 15F); + AutoScaleMode = AutoScaleMode.Font; + Controls.Add(dataGridView); + Name = "ValueTableControl"; + Size = new Size(451, 369); + ((System.ComponentModel.ISupportInitialize)dataGridView).EndInit(); + ResumeLayout(false); } #endregion + + private DataGridView dataGridView; } } diff --git a/Components/CustomDataGridView.cs b/Components/CustomDataGridView.cs index 727cb65..026e9d6 100644 --- a/Components/CustomDataGridView.cs +++ b/Components/CustomDataGridView.cs @@ -15,6 +15,89 @@ namespace Components public CustomDataGridView() { InitializeComponent(); + ConfigureDataGridView(); + } + + private void ConfigureDataGridView() + { + dataGridView.SelectionMode = DataGridViewSelectionMode.FullRowSelect; + dataGridView.MultiSelect = false; + dataGridView.RowHeadersVisible = false; + dataGridView.AllowUserToAddRows = false; + + dataGridView.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill; + } + + public void ConfigureColumns(List<(string HeaderText, string DataPropertyName, float FillWeight)> columns) + { + dataGridView.Columns.Clear(); + + foreach (var column in columns) + { + dataGridView.Columns.Add(new DataGridViewTextBoxColumn + { + HeaderText = column.HeaderText, + DataPropertyName = column.DataPropertyName, + FillWeight = column.FillWeight + }); + } + } + + public void ClearRows() + { + dataGridView.Rows.Clear(); + } + + public int SelectedRowIndex + { + get => dataGridView.SelectedRows[0].Index; + set + { + if (value >= 0 && value < dataGridView.Rows.Count) + { + dataGridView.ClearSelection(); + dataGridView.Rows[value].Selected = true; + } + } + } + + public T GetSelectedObject() where T : new() + { + if (dataGridView.SelectedRows.Count == 0) + throw new InvalidOperationException("Нет выбранной строки."); + + var selectedRow = dataGridView.SelectedRows[0]; + var obj = new T(); + + foreach (DataGridViewColumn column in dataGridView.Columns) + { + var prop = typeof(T).GetProperty(column.DataPropertyName); + if (prop != null) + { + var value = selectedRow.Cells[column.Index].Value; + prop.SetValue(obj, Convert.ChangeType(value, prop.PropertyType)); + } + } + + return obj; + } + + public void FillData(List objects) + { + dataGridView.Rows.Clear(); + + if (objects == null || !objects.Any()) + { + return; + } + + var properties = typeof(T).GetProperties(); + + foreach (var obj in objects) + { + var values = properties.Select(p => p.GetValue(obj, null)).ToArray(); + dataGridView.Rows.Add(values); + } } } } diff --git a/Components/CustomListBox.cs b/Components/CustomListBox.cs index dfa9dd5..b10288e 100644 --- a/Components/CustomListBox.cs +++ b/Components/CustomListBox.cs @@ -32,13 +32,14 @@ namespace Components public string SelectedItem { get { return (string?)listBox.SelectedItem ?? string.Empty; } - set { listBox.SelectedItem = value; } - } - - public event EventHandler? ItemSelected - { - add { _itemSelected += value; } - remove { _itemSelected -= value; } + set + { + int index = listBox.Items.IndexOf(value); + if (index != -1) + { + listBox.SelectedIndex = index; + } + } } private void MainListBox_SelectedIndexChanged(object sender, EventArgs e) diff --git a/Components/CustomTextBox.Designer.cs b/Components/CustomTextBox.Designer.cs index 898ebd8..69de896 100644 --- a/Components/CustomTextBox.Designer.cs +++ b/Components/CustomTextBox.Designer.cs @@ -28,10 +28,41 @@ /// private void InitializeComponent() { - components = new System.ComponentModel.Container(); - this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + checkBoxNull = new CheckBox(); + textBoxInteger = new TextBox(); + SuspendLayout(); + // + // checkBoxNull + // + checkBoxNull.AutoSize = true; + checkBoxNull.Location = new Point(13, 17); + checkBoxNull.Name = "checkBoxNull"; + checkBoxNull.Size = new Size(15, 14); + checkBoxNull.TabIndex = 0; + checkBoxNull.UseVisualStyleBackColor = true; + // + // textBoxInteger + // + textBoxInteger.Location = new Point(34, 13); + textBoxInteger.Name = "textBoxInteger"; + textBoxInteger.Size = new Size(100, 23); + textBoxInteger.TabIndex = 1; + // + // IntegerInputControl + // + AutoScaleDimensions = new SizeF(7F, 15F); + AutoScaleMode = AutoScaleMode.Font; + Controls.Add(textBoxInteger); + Controls.Add(checkBoxNull); + Name = "IntegerInputControl"; + Size = new Size(146, 49); + ResumeLayout(false); + PerformLayout(); } #endregion + + private CheckBox checkBoxNull; + private TextBox textBoxInteger; } } diff --git a/Components/CustomTextBox.cs b/Components/CustomTextBox.cs index 388b673..2ff1dfd 100644 --- a/Components/CustomTextBox.cs +++ b/Components/CustomTextBox.cs @@ -1,4 +1,5 @@ -using System; +using Components.Exceptions; +using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; @@ -12,9 +13,60 @@ namespace Components { public partial class CustomTextBox : UserControl { + public event EventHandler ValueChanged; + public CustomTextBox() { InitializeComponent(); + + checkBoxNull.CheckedChanged += CheckBoxNull_CheckedChanged; + textBoxInteger.TextChanged += TextBoxInteger_TextChanged; + } + + public int? Value + { + get + { + if (checkBoxNull.Checked) + { + return null; + } + else + { + if (int.TryParse(textBoxInteger.Text, out int result)) + { + return result; + } + else + { + throw new NotIntegerException("Ожидается целое число."); + } + } + } + set + { + if (value.HasValue) + { + checkBoxNull.Checked = false; + textBoxInteger.Text = value.ToString(); + } + else + { + checkBoxNull.Checked = true; + textBoxInteger.Text = string.Empty; + } + } + } + + private void TextBoxInteger_TextChanged(object sender, EventArgs e) + { + ValueChanged?.Invoke(this, e); + } + + private void CheckBoxNull_CheckedChanged(object sender, EventArgs e) + { + textBoxInteger.Enabled = !checkBoxNull.Checked; + ValueChanged?.Invoke(this, e); } } } diff --git a/Components/Exception/NotIntegerException.cs b/Components/Exception/NotIntegerException.cs new file mode 100644 index 0000000..1a62d5b --- /dev/null +++ b/Components/Exception/NotIntegerException.cs @@ -0,0 +1,13 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Components.Exceptions +{ + internal class NotIntegerException: Exception + { + public NotIntegerException(string message) : base(message) { } + } +} diff --git a/Components/Person.cs b/Components/Person.cs index 7358f4a..cd50d71 100644 --- a/Components/Person.cs +++ b/Components/Person.cs @@ -4,11 +4,13 @@ { public string Name { get; set; } public int Age { get; set; } + public string Email { get; set; } - public Person(string name, int age) + public Person(string name, int age, string email) { Name = name; Age = age; + Email = email; } } } diff --git a/WinFormsTest/Form1.Designer.cs b/WinFormsTest/Form1.Designer.cs index de4ef21..908c6a6 100644 --- a/WinFormsTest/Form1.Designer.cs +++ b/WinFormsTest/Form1.Designer.cs @@ -29,6 +29,8 @@ private void InitializeComponent() { customListBox = new Components.CustomListBox(); + customTextBox = new Components.CustomTextBox(); + customDataGridView1 = new Components.CustomDataGridView(); SuspendLayout(); // // customListBox @@ -39,11 +41,27 @@ customListBox.Size = new Size(150, 150); customListBox.TabIndex = 0; // + // customTextBox + // + customTextBox.Location = new Point(250, 40); + customTextBox.Name = "customTextBox"; + customTextBox.Size = new Size(150, 150); + customTextBox.TabIndex = 1; + // + // customDataGridView1 + // + customDataGridView1.Location = new Point(470, 50); + customDataGridView1.Name = "customDataGridView1"; + customDataGridView1.Size = new Size(449, 150); + customDataGridView1.TabIndex = 2; + // // Form1 // AutoScaleDimensions = new SizeF(7F, 15F); AutoScaleMode = AutoScaleMode.Font; - ClientSize = new Size(800, 450); + ClientSize = new Size(1003, 257); + Controls.Add(customDataGridView1); + Controls.Add(customTextBox); Controls.Add(customListBox); Name = "Form1"; Text = "Form1"; @@ -53,5 +71,7 @@ #endregion private Components.CustomListBox customListBox; + private Components.CustomTextBox customTextBox; + private Components.CustomDataGridView customDataGridView1; } } diff --git a/WinFormsTest/Form1.cs b/WinFormsTest/Form1.cs index 7c2dae8..78cf944 100644 --- a/WinFormsTest/Form1.cs +++ b/WinFormsTest/Form1.cs @@ -1,4 +1,5 @@ using Components; +using Components.Object; namespace WinFormsTest { @@ -9,6 +10,7 @@ namespace WinFormsTest InitializeComponent(); InitializeListBox(); + InitializeDataGridView(); } private void InitializeListBox() @@ -22,5 +24,23 @@ namespace WinFormsTest "Hi 5", }); } + + private void InitializeDataGridView() + { + var columns = new List<(string HeaderText, string DataPropertyName, float FillWeight)> + { + ("", "Name", 1), + ("", "Age", 1), + ("Email", "Email", 2) + }; + customDataGridView1.ConfigureColumns(columns); + + var data = new List + { + new Person ("", 30, "ivan@gmail.com" ), + new Person ("", 25, "maria@gmail.com") + }; + customDataGridView1.FillData(data); + } } }