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

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>
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;
}
}

View File

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

View File

@ -28,10 +28,41 @@
/// </summary>
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;
}
}

View File

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

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 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;
}
}
}

View File

@ -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;
}
}

View File

@ -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<Person>
{
new Person ("Èâàí", 30, "ivan@gmail.com" ),
new Person ("Ìàðèÿ", 25, "maria@gmail.com")
};
customDataGridView1.FillData(data);
}
}
}