Что-то написал

This commit is contained in:
Kirill 2024-10-03 21:07:20 +04:00
parent a46d1fefe8
commit 29ef211385
9 changed files with 257 additions and 14 deletions

View File

@ -28,10 +28,31 @@
/// </summary> /// </summary>
private void InitializeComponent() private void InitializeComponent()
{ {
components = new System.ComponentModel.Container(); dataGridView = new DataGridView();
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; ((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 #endregion
private DataGridView dataGridView;
} }
} }

View File

@ -15,6 +15,89 @@ namespace Components
public CustomDataGridView() public CustomDataGridView()
{ {
InitializeComponent(); 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<T>() 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<T>(List<T> 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);
}
} }
} }
} }

View File

@ -32,13 +32,14 @@ namespace Components
public string SelectedItem public string SelectedItem
{ {
get { return (string?)listBox.SelectedItem ?? string.Empty; } get { return (string?)listBox.SelectedItem ?? string.Empty; }
set { listBox.SelectedItem = value; } set
}
public event EventHandler? ItemSelected
{ {
add { _itemSelected += value; } int index = listBox.Items.IndexOf(value);
remove { _itemSelected -= value; } if (index != -1)
{
listBox.SelectedIndex = index;
}
}
} }
private void MainListBox_SelectedIndexChanged(object sender, EventArgs e) private void MainListBox_SelectedIndexChanged(object sender, EventArgs e)

View File

@ -28,10 +28,41 @@
/// </summary> /// </summary>
private void InitializeComponent() private void InitializeComponent()
{ {
components = new System.ComponentModel.Container(); checkBoxNull = new CheckBox();
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; 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 #endregion
private CheckBox checkBoxNull;
private TextBox textBoxInteger;
} }
} }

View File

@ -1,4 +1,5 @@
using System; using Components.Exceptions;
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.ComponentModel; using System.ComponentModel;
using System.Data; using System.Data;
@ -12,9 +13,60 @@ namespace Components
{ {
public partial class CustomTextBox : UserControl public partial class CustomTextBox : UserControl
{ {
public event EventHandler ValueChanged;
public CustomTextBox() public CustomTextBox()
{ {
InitializeComponent(); 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);
} }
} }
} }

View File

@ -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) { }
}
}

View File

@ -4,11 +4,13 @@
{ {
public string Name { get; set; } public string Name { get; set; }
public int Age { 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; Name = name;
Age = age; Age = age;
Email = email;
} }
} }
} }

View File

@ -29,6 +29,8 @@
private void InitializeComponent() private void InitializeComponent()
{ {
customListBox = new Components.CustomListBox(); customListBox = new Components.CustomListBox();
customTextBox = new Components.CustomTextBox();
customDataGridView1 = new Components.CustomDataGridView();
SuspendLayout(); SuspendLayout();
// //
// customListBox // customListBox
@ -39,11 +41,27 @@
customListBox.Size = new Size(150, 150); customListBox.Size = new Size(150, 150);
customListBox.TabIndex = 0; 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 // Form1
// //
AutoScaleDimensions = new SizeF(7F, 15F); AutoScaleDimensions = new SizeF(7F, 15F);
AutoScaleMode = AutoScaleMode.Font; AutoScaleMode = AutoScaleMode.Font;
ClientSize = new Size(800, 450); ClientSize = new Size(1003, 257);
Controls.Add(customDataGridView1);
Controls.Add(customTextBox);
Controls.Add(customListBox); Controls.Add(customListBox);
Name = "Form1"; Name = "Form1";
Text = "Form1"; Text = "Form1";
@ -53,5 +71,7 @@
#endregion #endregion
private Components.CustomListBox customListBox; private Components.CustomListBox customListBox;
private Components.CustomTextBox customTextBox;
private Components.CustomDataGridView customDataGridView1;
} }
} }

View File

@ -1,4 +1,5 @@
using Components; using Components;
using Components.Object;
namespace WinFormsTest namespace WinFormsTest
{ {
@ -9,6 +10,7 @@ namespace WinFormsTest
InitializeComponent(); InitializeComponent();
InitializeListBox(); InitializeListBox();
InitializeDataGridView();
} }
private void InitializeListBox() private void InitializeListBox()
@ -22,5 +24,23 @@ namespace WinFormsTest
"Hi 5", "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<Person>
{
new Person ("Èâàí", 30, "ivan@gmail.com" ),
new Person ("Ìàðèÿ", 25, "maria@gmail.com")
};
customDataGridView1.FillData(data);
}
} }
} }