This commit is contained in:
platoff aeeee 2024-10-01 10:20:10 +04:00
parent a9749528c7
commit 4def6e88ff
8 changed files with 337 additions and 181 deletions

View File

@ -0,0 +1,23 @@
using Library15Gerimovich;
namespace Library15Gerimovich
{
public class ColumnInfo
{
public string Header = string.Empty;
public int Width;
public bool IsVisible;
public string Name = string.Empty;
public ColumnInfo(string Header, int Width, bool IsVisible, string Name)
{
this.Header = Header;
this.Width = Width;
this.IsVisible = IsVisible;
this.Name = Name;
}
}
}

View File

@ -41,7 +41,7 @@
"MediaSoft"});
this.MainListBox.Location = new System.Drawing.Point(3, 3);
this.MainListBox.Name = "MainListBox";
this.MainListBox.Size = new System.Drawing.Size(144, 109);
this.MainListBox.Size = new System.Drawing.Size(144, 139);
this.MainListBox.TabIndex = 0;
//
// DefaultList

View File

@ -19,18 +19,10 @@ namespace Library15Gerimovich
InitializeComponent();
}
// метод, передающий строку
public void SetItems(string Items)
{
string[] itemArray = Items.Split(','); // разбиваем по запятым
MainListBox.Items.Clear();
foreach (var item in itemArray)
{
MainListBox.Items.Add(item.Trim()); // Trim для удаления пробелов
}
MainListBox.Items.Add(Items);
}
public void ClearList()
{
@ -55,48 +47,65 @@ namespace Library15Gerimovich
{
_itemSelected?.Invoke(this, e);
}
//public event EventHandler? SelectedValueChanged;
//public string SelectedValue
//{
// get
// {
// return checkedListBox1.SelectedItem?.ToString() ?? string.Empty;
// }
// set
// {
// int index = checkedListBox1.Items.IndexOf(value);
// if (index >= 0)
// {
// checkedListBox1.SelectedIndex = index;
// }
// else
// {
// checkedListBox1.ClearSelected();
// }
// }
//}
//public DefaultList()
//{
// InitializeComponent();
//}
//public void AddItemToList(string item)
//{
// ListBox.Items.Add(item);
//}
//private void SelectedIndexChanged(object sender, EventArgs e)
//{
// SelectedValueChanged?.Invoke(this, e);
//}
//public void ClearComboBox()
//{
// checkedListBox1.Items.Clear();
//}
}
}
// метод, передающий строку
//public void SetItems(string Items)
//{
// string[] itemArray = Items.Split(','); // разбиваем по запятым
// MainListBox.Items.Clear();
// foreach (var item in itemArray)
// {
// MainListBox.Items.Add(item.Trim()); // Trim для удаления пробелов
// }
//}
//public event EventHandler? SelectedValueChanged;
//public string SelectedValue
//{
// get
// {
// return checkedListBox1.SelectedItem?.ToString() ?? string.Empty;
// }
// set
// {
// int index = checkedListBox1.Items.IndexOf(value);
// if (index >= 0)
// {
// checkedListBox1.SelectedIndex = index;
// }
// else
// {
// checkedListBox1.ClearSelected();
// }
// }
//}
//public DefaultList()
//{
// InitializeComponent();
//}
//public void AddItemToList(string item)
//{
// ListBox.Items.Add(item);
//}
//private void SelectedIndexChanged(object sender, EventArgs e)
//{
// SelectedValueChanged?.Invoke(this, e);
//}
//public void ClearComboBox()
//{
// checkedListBox1.Items.Clear();
//}

View File

@ -35,10 +35,10 @@
// dataGridViewOut
//
this.dataGridViewOut.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
this.dataGridViewOut.Location = new System.Drawing.Point(3, 25);
this.dataGridViewOut.Location = new System.Drawing.Point(3, 3);
this.dataGridViewOut.Name = "dataGridViewOut";
this.dataGridViewOut.RowTemplate.Height = 25;
this.dataGridViewOut.Size = new System.Drawing.Size(240, 150);
this.dataGridViewOut.Size = new System.Drawing.Size(397, 222);
this.dataGridViewOut.TabIndex = 0;
//
// OutputTableResults
@ -47,7 +47,7 @@
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.Controls.Add(this.dataGridViewOut);
this.Name = "OutputTableResults";
this.Size = new System.Drawing.Size(259, 245);
this.Size = new System.Drawing.Size(407, 232);
((System.ComponentModel.ISupportInitialize)(this.dataGridViewOut)).EndInit();
this.ResumeLayout(false);

View File

@ -14,101 +14,80 @@ namespace Library15Gerimovich
{
public partial class OutputTableResults : UserControl
{
private readonly List<string> _columnsFildsNames = new();
public OutputTableResults()
{
InitializeComponent();
}
public void ConfigureColumns(params CustomDataTableColumnParameter[] columnParameters)
{
dataGridViewOut.Columns.Clear();
_columnsFildsNames.Clear();
for (int i = 0; i < columnParameters.Length; i++)
{
dataGridViewOut.Columns
.Add(columnParameters[i].HeaderName, columnParameters[i].HeaderName);
var column = dataGridViewOut.Columns[i];
column.Visible = columnParameters[i].Visible;
_columnsFildsNames.Add(column.Name);
if (columnParameters[i].Width != 0)
{
column.Width = columnParameters[i].Width;
}
else
{
column.AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells;
}
}
}
/// <summary>
/// Отчистить таблицу
/// </summary>
public void Clear()
=> dataGridViewOut.Rows.Clear();
/// <summary>
/// Индекс выбранной строки
/// </summary>
// установка и получение индекса выбранной строки
public int SelectedRow
{
get => dataGridViewOut.CurrentRow.Index;
get => dataGridViewOut.SelectedRows.Count == 1 ? dataGridViewOut.SelectedRows[0].Index : -1;
set
{
dataGridViewOut.CurrentRow.Selected = false;
dataGridViewOut.Rows[value].Selected = true;
if (value >= 0)
{
dataGridViewOut.Rows[value].Selected = true;
}
}
}
//public TType? GetRow<TType>(int rowIndex)
//{
// Type type = typeof(TType);
// var element = type.GetConstructor([])?.Invoke([]);
// if (element is null)
// {
// return default;
// }
// var row = dataGridViewOut.Rows[rowIndex].Cells;
// for (int i = 0; i < dataGridViewOut.ColumnCount; i++)
// {
// if (string.IsNullOrEmpty(_columnsFildsNames[i]))
// {
// continue;
// }
// var field = type.GetProperty(_columnsFildsNames[i]);
// field?.SetValue(element, row[i].Value);
// }
// return (TType)element;
//}
public void Fill<TType>(IList<TType> insertValues)
// Конфигурация столбцов.
// Для каждого столбца указываются заголовок, ширина, видимость и имя свойства/поля объекта
public void ConfigureColumns(List<ColumnInfo> Columns)
{
Clear();
dataGridViewOut.ColumnCount = Columns.Count;
Type type = typeof(TType);
for (int i = 0; i < insertValues.Count(); ++i)
for (int Index = 0; Index < Columns.Count; ++Index)
{
var row = insertValues[i];
dataGridViewOut.Rows.Add();
dataGridViewOut.Columns[Index].HeaderText = Columns[Index].Header;
dataGridViewOut.Columns[Index].Width = Columns[Index].Width;
dataGridViewOut.Columns[Index].Visible = Columns[Index].IsVisible;
dataGridViewOut.Columns[Index].Name = Columns[Index].Name;
}
}
for (int j = 0; j < _columnsFildsNames.Count; ++j)
{
dataGridViewOut.Rows[i].Cells[j].Value
= type.GetProperty(_columnsFildsNames[j])?.GetValue(row)!;
}
// очистка строк
public void ClearGrid()
{
dataGridViewOut.Rows.Clear();
}
// получение объекта из выбранной строки
public T GetSelectedObject<T>() where T : new()
{
T RetVal = new T();
var Properties = typeof(T).GetProperties();
var Cells = dataGridViewOut.SelectedRows[0].Cells;
for (int Index = 0; Index < dataGridViewOut.ColumnCount; ++Index)
{
DataGridViewColumn Column = dataGridViewOut.Columns[Index];
var Cell = Cells[Index];
var Property = Properties.FirstOrDefault(x => x.Name == Column.Name);
Property?.SetValue(RetVal, Cell.Value);
}
return RetVal;
}
// заполнение таблицы через передаваемый объект класса T
public void InsertValue<T>(T Object)
{
var Type = typeof(T);
var Properties = Type.GetProperties();
int NewRowIndex = dataGridViewOut.Rows.Add();
foreach (DataGridViewColumn Column in dataGridViewOut.Columns)
{
var Property = Properties.FirstOrDefault(x => x.Name == Column.Name);
if (Property == null)
throw new InvalidOperationException($"В типе {Type.Name} не найдено свойство с именем {Column.Name}");
var Value = Property.GetValue(Object);
dataGridViewOut.Rows[NewRowIndex].Cells[Column.Index].Value = Value;
}
}
}

View File

@ -28,50 +28,80 @@
/// </summary>
private void InitializeComponent()
{
this.userControl11 = new Library15Gerimovich.UserControl1();
this.defaultList1 = new Library15Gerimovich.DefaultList();
this.inputRealNumber1 = new Library15Gerimovich.InputRealNumber();
this.outputTableResults1 = new Library15Gerimovich.OutputTableResults();
this.defaultList = new Library15Gerimovich.DefaultList();
this.inputRealNumber = new Library15Gerimovich.InputRealNumber();
this.outputTableResults = new Library15Gerimovich.OutputTableResults();
this.CheckRealButton = new System.Windows.Forms.Button();
this.ClearListButton = new System.Windows.Forms.Button();
this.ClearDatagridButton = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// userControl11
// defaultList
//
this.userControl11.Avatar = null;
this.userControl11.Location = new System.Drawing.Point(12, 242);
this.userControl11.Name = "userControl11";
this.userControl11.Size = new System.Drawing.Size(150, 144);
this.userControl11.TabIndex = 0;
this.defaultList.Location = new System.Drawing.Point(12, 12);
this.defaultList.Name = "defaultList";
this.defaultList.SelectedItem = "";
this.defaultList.Size = new System.Drawing.Size(150, 150);
this.defaultList.TabIndex = 1;
this.defaultList.ItemSelected += new System.EventHandler(this.defaultList_ItemSelected);
//
// defaultList1
// inputRealNumber
//
this.defaultList1.Location = new System.Drawing.Point(12, 12);
this.defaultList1.Name = "defaultList1";
this.defaultList1.Size = new System.Drawing.Size(150, 150);
this.defaultList1.TabIndex = 1;
this.inputRealNumber.Location = new System.Drawing.Point(168, 14);
this.inputRealNumber.Name = "inputRealNumber";
this.inputRealNumber.Size = new System.Drawing.Size(135, 101);
this.inputRealNumber.TabIndex = 2;
this.inputRealNumber.ValueChanged += new System.EventHandler(this.InputComponent_ValueChanged);
//
// inputRealNumber1
// outputTableResults
//
this.inputRealNumber1.Location = new System.Drawing.Point(202, 34);
this.inputRealNumber1.Name = "inputRealNumber1";
this.inputRealNumber1.Size = new System.Drawing.Size(135, 101);
this.inputRealNumber1.TabIndex = 2;
this.outputTableResults.Location = new System.Drawing.Point(324, 12);
this.outputTableResults.Name = "outputTableResults";
this.outputTableResults.SelectedRow = -1;
this.outputTableResults.Size = new System.Drawing.Size(414, 251);
this.outputTableResults.TabIndex = 3;
//
// outputTableResults1
// CheckRealButton
//
this.outputTableResults1.Location = new System.Drawing.Point(422, 34);
this.outputTableResults1.Name = "outputTableResults1";
this.outputTableResults1.Size = new System.Drawing.Size(259, 245);
this.outputTableResults1.TabIndex = 3;
this.CheckRealButton.Location = new System.Drawing.Point(178, 121);
this.CheckRealButton.Name = "CheckRealButton";
this.CheckRealButton.Size = new System.Drawing.Size(75, 23);
this.CheckRealButton.TabIndex = 4;
this.CheckRealButton.Text = "Проверить";
this.CheckRealButton.UseVisualStyleBackColor = true;
this.CheckRealButton.Click += new System.EventHandler(this.CheckRealButton_Click_1);
//
// ClearListButton
//
this.ClearListButton.Location = new System.Drawing.Point(44, 179);
this.ClearListButton.Name = "ClearListButton";
this.ClearListButton.Size = new System.Drawing.Size(75, 23);
this.ClearListButton.TabIndex = 5;
this.ClearListButton.Text = "Очистить";
this.ClearListButton.UseVisualStyleBackColor = true;
this.ClearListButton.Click += new System.EventHandler(this.ClearListButton_Click);
//
// ClearDatagridButton
//
this.ClearDatagridButton.Location = new System.Drawing.Point(656, 275);
this.ClearDatagridButton.Name = "ClearDatagridButton";
this.ClearDatagridButton.Size = new System.Drawing.Size(75, 23);
this.ClearDatagridButton.TabIndex = 6;
this.ClearDatagridButton.Text = "Очистить";
this.ClearDatagridButton.UseVisualStyleBackColor = true;
this.ClearDatagridButton.Click += new System.EventHandler(this.ClearDatagridButton_Click);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(800, 450);
this.Controls.Add(this.outputTableResults1);
this.Controls.Add(this.inputRealNumber1);
this.Controls.Add(this.defaultList1);
this.Controls.Add(this.userControl11);
this.ClientSize = new System.Drawing.Size(739, 355);
this.Controls.Add(this.ClearDatagridButton);
this.Controls.Add(this.ClearListButton);
this.Controls.Add(this.CheckRealButton);
this.Controls.Add(this.outputTableResults);
this.Controls.Add(this.inputRealNumber);
this.Controls.Add(this.defaultList);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
@ -79,10 +109,11 @@
}
#endregion
private Library15Gerimovich.UserControl1 userControl11;
private Library15Gerimovich.DefaultList defaultList1;
private Library15Gerimovich.InputRealNumber inputRealNumber1;
private Library15Gerimovich.OutputTableResults outputTableResults1;
private Library15Gerimovich.DefaultList defaultList;
private Library15Gerimovich.InputRealNumber inputRealNumber;
private Library15Gerimovich.OutputTableResults outputTableResults;
private Button CheckRealButton;
private Button ClearListButton;
private Button ClearDatagridButton;
}
}

View File

@ -1,3 +1,6 @@
using Library15Gerimovich;
using Library15Gerimovich.Exceptions;
namespace WinFormsAppTest
{
public partial class Form1 : Form
@ -5,21 +8,102 @@ namespace WinFormsAppTest
public Form1()
{
InitializeComponent();
InitializeDefaultList();
InitializeInputRealNumber();
InitializeOutputTableResults();
}
private void CreateImage()
//private void CreateImage()
//{
// Bitmap bmp = new(userControl11.Width - 10,
// userControl11.Height - 10);
// Graphics gr = Graphics.FromImage(bmp);
// gr.DrawEllipse(new Pen(Color.Red), 10, 10, 20, 20);
// userControl11.Avatar = bmp;
//}
//private void userControl11_AvatarChanged(object sender, EventArgs e)
//{
// var width = userControl11.Avatar.Width;
// MessageBox.Show($"Change avatar, width={width}");
//}
private void InitializeDefaultList()
{
Bitmap bmp = new(userControl11.Width - 10,
userControl11.Height - 10);
Graphics gr = Graphics.FromImage(bmp);
gr.DrawEllipse(new Pen(Color.Red), 10, 10, 20, 20);
userControl11.Avatar = bmp;
}
private void userControl11_AvatarChanged(object sender, EventArgs e)
{
var width = userControl11.Avatar.Width;
MessageBox.Show($"Change avatar, width={width}");
defaultList.SetItems("RITG, SimbirSOft, Mediasoft");
}
private void InitializeInputRealNumber()
{
inputRealNumber.DoubleValue = 1500;
}
private void InitializeOutputTableResults()
{
outputTableResults.ConfigureColumns(new List<ColumnInfo>
{
new ColumnInfo("", 0, false, "Id"),
new ColumnInfo("Surname", 150, true, "Surname"),
new ColumnInfo("Name", 150, true, "Name"),
new ColumnInfo("Age", 50, true, "Age"),
});
TestObject TestOB = new TestObject(1, "Ôàìèëèÿ", "èìÿ", 10);
TestObject TestOB2 = new TestObject(1, "Èâàíîâ", "Èâàí", 29);
outputTableResults.InsertValue(TestOB);
outputTableResults.InsertValue(TestOB2);
}
private void defaultList_ItemSelected(object sender, EventArgs e)
{
MessageBox.Show(defaultList.SelectedItem);
}
private void InputComponent_ValueChanged(object sender, EventArgs e)
{
}
private void ClearListButton_Click(object sender, EventArgs e)
{
defaultList.ClearList();
}
private void PrintObjectButton_Click(object sender, EventArgs e)
{
var Test = outputTableResults.GetSelectedObject<TestObject>();
MessageBox.Show(Test.ToString());
}
private void CheckRealButton_Click_1(object sender, EventArgs e)
{
try
{
double? Value = inputRealNumber.DoubleValue;
if (Value != null)
{
MessageBox.Show($"Ââåäåíî âåùåñòâåííîå ÷èñëî: {Value}");
}
else
{
MessageBox.Show("Çíà÷åíèå - null");
}
}
catch (MalformedRealException ex)
{
MessageBox.Show(ex.Message);
}
catch (EmptyValueException ex)
{
MessageBox.Show(ex.Message);
}
}
private void ClearDatagridButton_Click(object sender, EventArgs e)
{
outputTableResults.ClearGrid();
}
}
}

View File

@ -0,0 +1,30 @@
namespace WinFormsAppTest
{
public class TestObject
{
public int Id { get; set; }
public string Surname { get; set; } = string.Empty;
public string Name { get; set; } = string.Empty;
public int Age { get; set; }
public TestObject()
{
}
public TestObject(int Id, string Surname, string Name, int Age)
{
this.Id = Id;
this.Surname = Surname;
this.Name = Name;
this.Age = Age;
}
public override string ToString()
{
return $"[{Id}] {Surname} {Name}. Возраст: {Age}";
}
}
}