diff --git a/WinFormsLibrary1/Contracts/BindingModels/ProductBindingModel.cs b/WinFormsLibrary1/Contracts/BindingModels/ProductBindingModel.cs index 97d136a..b921cbf 100644 --- a/WinFormsLibrary1/Contracts/BindingModels/ProductBindingModel.cs +++ b/WinFormsLibrary1/Contracts/BindingModels/ProductBindingModel.cs @@ -14,6 +14,6 @@ namespace Contracts.BindingModels public string Name { get; set; } = string.Empty; public string Description { get; set; } = string.Empty; public string Category { get; set; } = string.Empty; - public int CountOnStorage { get; set; } + public int? CountOnStorage { get; set; } } } diff --git a/WinFormsLibrary1/Contracts/ViewModels/CategoryViewModel.cs b/WinFormsLibrary1/Contracts/ViewModels/CategoryViewModel.cs index af6a9b3..287c976 100644 --- a/WinFormsLibrary1/Contracts/ViewModels/CategoryViewModel.cs +++ b/WinFormsLibrary1/Contracts/ViewModels/CategoryViewModel.cs @@ -12,6 +12,6 @@ namespace Contracts.ViewModels { public int Id { get; set; } [DisplayName("Название")] - public string Name { get; set; } = string.Empty; + public string Name { get; set; } = "Название категории"; } } diff --git a/WinFormsLibrary1/Contracts/ViewModels/ProductViewModel.cs b/WinFormsLibrary1/Contracts/ViewModels/ProductViewModel.cs index 9822262..f791b28 100644 --- a/WinFormsLibrary1/Contracts/ViewModels/ProductViewModel.cs +++ b/WinFormsLibrary1/Contracts/ViewModels/ProductViewModel.cs @@ -9,9 +9,9 @@ using System.Threading.Tasks; namespace Contracts.ViewModels { - public class ProductViewModel : IProductModel + public class ProductViewModel { - public int Id { get; set; } + public string Id { get; set; } [DisplayName("Название")] public string Name { get; set; } = string.Empty; [DisplayName("Описание")] @@ -19,6 +19,7 @@ namespace Contracts.ViewModels [DisplayName("Категория продукта")] public string Category { get; set; } = string.Empty; [DisplayName("Количество товаров на складе")] - public int CountOnStorage { get; set; } + public int? CountOnStorage { get; set; } + public string? CountOnStorageS { get; set; } } } diff --git a/WinFormsLibrary1/DatabaseImplement/Migrations/20241112051510_nullableCountOnStorage.Designer.cs b/WinFormsLibrary1/DatabaseImplement/Migrations/20241112051510_nullableCountOnStorage.Designer.cs new file mode 100644 index 0000000..48091db --- /dev/null +++ b/WinFormsLibrary1/DatabaseImplement/Migrations/20241112051510_nullableCountOnStorage.Designer.cs @@ -0,0 +1,76 @@ +// +using System; +using DatabaseImplement; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; + +#nullable disable + +namespace DatabaseImplement.Migrations +{ + [DbContext(typeof(Database))] + [Migration("20241112051510_nullableCountOnStorage")] + partial class nullableCountOnStorage + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "8.0.10") + .HasAnnotation("Relational:MaxIdentifierLength", 63); + + NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder); + + modelBuilder.Entity("DatabaseImplement.Models.Category", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("Name") + .IsRequired() + .HasColumnType("text"); + + b.HasKey("Id"); + + b.ToTable("Categories"); + }); + + modelBuilder.Entity("DatabaseImplement.Models.Product", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("Category") + .IsRequired() + .HasColumnType("text"); + + b.Property("CountOnStorage") + .IsRequired() + .HasColumnType("integer"); + + b.Property("Description") + .IsRequired() + .HasColumnType("text"); + + b.Property("Name") + .IsRequired() + .HasColumnType("text"); + + b.HasKey("Id"); + + b.ToTable("Products"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/WinFormsLibrary1/DatabaseImplement/Migrations/20241112051510_nullableCountOnStorage.cs b/WinFormsLibrary1/DatabaseImplement/Migrations/20241112051510_nullableCountOnStorage.cs new file mode 100644 index 0000000..528173c --- /dev/null +++ b/WinFormsLibrary1/DatabaseImplement/Migrations/20241112051510_nullableCountOnStorage.cs @@ -0,0 +1,22 @@ +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace DatabaseImplement.Migrations +{ + /// + public partial class nullableCountOnStorage : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + + } + } +} diff --git a/WinFormsLibrary1/DatabaseImplement/Migrations/20241112052212_nullableCountOnStorage2.Designer.cs b/WinFormsLibrary1/DatabaseImplement/Migrations/20241112052212_nullableCountOnStorage2.Designer.cs new file mode 100644 index 0000000..dd720ab --- /dev/null +++ b/WinFormsLibrary1/DatabaseImplement/Migrations/20241112052212_nullableCountOnStorage2.Designer.cs @@ -0,0 +1,75 @@ +// +using System; +using DatabaseImplement; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; + +#nullable disable + +namespace DatabaseImplement.Migrations +{ + [DbContext(typeof(Database))] + [Migration("20241112052212_nullableCountOnStorage2")] + partial class nullableCountOnStorage2 + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "8.0.10") + .HasAnnotation("Relational:MaxIdentifierLength", 63); + + NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder); + + modelBuilder.Entity("DatabaseImplement.Models.Category", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("Name") + .IsRequired() + .HasColumnType("text"); + + b.HasKey("Id"); + + b.ToTable("Categories"); + }); + + modelBuilder.Entity("DatabaseImplement.Models.Product", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("Category") + .IsRequired() + .HasColumnType("text"); + + b.Property("CountOnStorage") + .HasColumnType("integer"); + + b.Property("Description") + .IsRequired() + .HasColumnType("text"); + + b.Property("Name") + .IsRequired() + .HasColumnType("text"); + + b.HasKey("Id"); + + b.ToTable("Products"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/WinFormsLibrary1/DatabaseImplement/Migrations/20241112052212_nullableCountOnStorage2.cs b/WinFormsLibrary1/DatabaseImplement/Migrations/20241112052212_nullableCountOnStorage2.cs new file mode 100644 index 0000000..6ef4109 --- /dev/null +++ b/WinFormsLibrary1/DatabaseImplement/Migrations/20241112052212_nullableCountOnStorage2.cs @@ -0,0 +1,36 @@ +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace DatabaseImplement.Migrations +{ + /// + public partial class nullableCountOnStorage2 : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.AlterColumn( + name: "CountOnStorage", + table: "Products", + type: "integer", + nullable: true, + oldClrType: typeof(int), + oldType: "integer"); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.AlterColumn( + name: "CountOnStorage", + table: "Products", + type: "integer", + nullable: false, + defaultValue: 0, + oldClrType: typeof(int), + oldType: "integer", + oldNullable: true); + } + } +} diff --git a/WinFormsLibrary1/DatabaseImplement/Migrations/DatabaseModelSnapshot.cs b/WinFormsLibrary1/DatabaseImplement/Migrations/DatabaseModelSnapshot.cs index 8ee76e6..effd47b 100644 --- a/WinFormsLibrary1/DatabaseImplement/Migrations/DatabaseModelSnapshot.cs +++ b/WinFormsLibrary1/DatabaseImplement/Migrations/DatabaseModelSnapshot.cs @@ -1,4 +1,5 @@ // +using System; using DatabaseImplement; using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Infrastructure; @@ -50,7 +51,7 @@ namespace DatabaseImplement.Migrations .IsRequired() .HasColumnType("text"); - b.Property("CountOnStorage") + b.Property("CountOnStorage") .HasColumnType("integer"); b.Property("Description") diff --git a/WinFormsLibrary1/DatabaseImplement/Models/Product.cs b/WinFormsLibrary1/DatabaseImplement/Models/Product.cs index 29c1e80..c5a6d9b 100644 --- a/WinFormsLibrary1/DatabaseImplement/Models/Product.cs +++ b/WinFormsLibrary1/DatabaseImplement/Models/Product.cs @@ -25,8 +25,7 @@ namespace DatabaseImplement.Models [Required] public string Category { get; private set; } - [Required] - public int CountOnStorage { get; private set; } + public int? CountOnStorage { get; private set; } public static Product? Create(ProductBindingModel model) { @@ -54,11 +53,12 @@ namespace DatabaseImplement.Models public ProductViewModel GetViewModel => new() { - Id = Id, + Id = Id.ToString(), Name = Name, Description = Description, Category = Category, CountOnStorage = CountOnStorage, + CountOnStorageS = CountOnStorage != null ? CountOnStorage.ToString() : "Нет в наличии", }; } } diff --git a/WinFormsLibrary1/Models/Models/IProductModel.cs b/WinFormsLibrary1/Models/Models/IProductModel.cs index 000fd5d..3595e19 100644 --- a/WinFormsLibrary1/Models/Models/IProductModel.cs +++ b/WinFormsLibrary1/Models/Models/IProductModel.cs @@ -12,6 +12,6 @@ namespace Models.Models string Name { get;} string Description { get;} string Category { get;} - int CountOnStorage { get;} + int? CountOnStorage { get;} } } diff --git a/WinFormsLibrary1/WinFormsApp1/Form1.Designer.cs b/WinFormsLibrary1/WinFormsApp1/Form1.Designer.cs index 6735c30..078bb0d 100644 --- a/WinFormsLibrary1/WinFormsApp1/Form1.Designer.cs +++ b/WinFormsLibrary1/WinFormsApp1/Form1.Designer.cs @@ -18,8 +18,8 @@ namespace WinFormsApp1 private void InitializeComponent() { comboBoxUserControl = new ComboBox(); - listBoxUserControl = new ListBox(); dateInputControl1 = new DateInputControl(); + listBoxUserControl = new ListBoxUserControl(); SuspendLayout(); // // comboBoxUserControl @@ -30,29 +30,31 @@ namespace WinFormsApp1 comboBoxUserControl.Size = new Size(160, 28); comboBoxUserControl.TabIndex = 0; // - // listBoxUserControl - // - listBoxUserControl.Location = new Point(13, 231); - listBoxUserControl.Margin = new Padding(4, 5, 4, 5); - listBoxUserControl.Name = "listBoxUserControl"; - listBoxUserControl.Size = new Size(159, 144); - listBoxUserControl.TabIndex = 2; - // // dateInputControl1 // + dateInputControl1.DateFormat = null; dateInputControl1.Location = new Point(13, 107); dateInputControl1.Margin = new Padding(4, 5, 4, 5); dateInputControl1.Name = "dateInputControl1"; dateInputControl1.Size = new Size(166, 96); dateInputControl1.TabIndex = 3; // + // listBoxUserControl + // + listBoxUserControl.Location = new Point(13, 233); + listBoxUserControl.Margin = new Padding(4, 5, 4, 5); + listBoxUserControl.Name = "listBoxUserControl"; + listBoxUserControl.SelectedIndex = -1; + listBoxUserControl.Size = new Size(365, 182); + listBoxUserControl.TabIndex = 4; + // // Form1 // AutoScaleDimensions = new SizeF(8F, 20F); AutoScaleMode = AutoScaleMode.Font; ClientSize = new Size(400, 462); - Controls.Add(dateInputControl1); Controls.Add(listBoxUserControl); + Controls.Add(dateInputControl1); Controls.Add(comboBoxUserControl); Margin = new Padding(4, 5, 4, 5); Name = "Form1"; @@ -61,7 +63,7 @@ namespace WinFormsApp1 } private ComboBox comboBoxUserControl; - private ListBox listBoxUserControl; private DateInputControl dateInputControl1; + private ListBoxUserControl listBoxUserControl; } } \ No newline at end of file diff --git a/WinFormsLibrary1/WinFormsApp1/Form1.cs b/WinFormsLibrary1/WinFormsApp1/Form1.cs index c6a3bb0..58ed45c 100644 --- a/WinFormsLibrary1/WinFormsApp1/Form1.cs +++ b/WinFormsLibrary1/WinFormsApp1/Form1.cs @@ -9,6 +9,19 @@ namespace WinFormsApp1 public Form1() { InitializeComponent(); + listBoxUserControl.SetParams("Id {Id} {Name} {Familia}", '{', '}'); + + listBoxUserControl.AddObject(new User(1, "status1", "Vova", "Stroev", 19, "FIST", 35000.50)); + listBoxUserControl.AddObject(new User(1, "status2", "Vladimir", "Stroev", 20, "FIST", 33000.50)); + listBoxUserControl.AddObject(new User(1, "status3", "Volodya", "Stroev", 21, "FIST", 39000.50)); + listBoxUserControl.AddObject(new User(1, "status3", "Volodya", "Stroev", 21, "FIST", 39000.50)); + listBoxUserControl.AddObject(new User(1, "status3", "Volodya", "Stroev", 21, "FIST", 39000.50)); + listBoxUserControl.AddObject(new User(1, "status3", "Volodya", "Stroev", 21, "FIST", 39000.50)); + listBoxUserControl.AddObject(new User(1, "status3", "Volodya", "Stroev", 21, "FIST", 39000.50)); + listBoxUserControl.AddObject(new User(1, "status3", "Volodya", "Stroev", 21, "FIST", 39000.50)); + listBoxUserControl.AddObject(new User(1, "status3", "Volodya", "Stroev", 21, "FIST", 39000.50)); + listBoxUserControl.AddObject(new User(1, "status3", "Volodya", "Stroev", 21, "FIST", 39000.50)); + listBoxUserControl.AddObject(new User(1, "status3", "Volodya", "Stroev", 21, "FIST", 39000.50)); } private void ComboBoxUserControl_SelectedValueChanged(object sender, EventArgs e) @@ -16,9 +29,9 @@ namespace WinFormsApp1 MessageBox.Show($"ComboBox selected: {comboBoxUserControl.SelectedValue}"); } - private void ListBoxUserControl_SelectedValueChanged(object sender, EventArgs e) + /*private void ListBoxUserControl_SelectedValueChanged(object sender, EventArgs e) { MessageBox.Show($"ListBox selected: {listBoxUserControl.SelectedValue}"); - } + }*/ } } \ No newline at end of file diff --git a/WinFormsLibrary1/WinFormsApp1/Forms.csproj b/WinFormsLibrary1/WinFormsApp1/Forms.csproj index c432a0a..400d512 100644 --- a/WinFormsLibrary1/WinFormsApp1/Forms.csproj +++ b/WinFormsLibrary1/WinFormsApp1/Forms.csproj @@ -9,7 +9,7 @@ - + all diff --git a/WinFormsLibrary1/WinFormsApp1/MainForm.Designer.cs b/WinFormsLibrary1/WinFormsApp1/MainForm.Designer.cs index 005c6b1..65bf98b 100644 --- a/WinFormsLibrary1/WinFormsApp1/MainForm.Designer.cs +++ b/WinFormsLibrary1/WinFormsApp1/MainForm.Designer.cs @@ -29,26 +29,26 @@ private void InitializeComponent() { components = new System.ComponentModel.Container(); - _productListControl = new Library15Gerimovich.OutputTableResults(); + _productTreeView = new Library14Petrushin.HierarchicalTreeView(); wordTablesComponent1 = new Library15Gerimovich.WordTablesComponent(components); SuspendLayout(); // - // _productListControl + // _productTreeView // - _productListControl.Dock = DockStyle.Fill; - _productListControl.Location = new Point(0, 0); - _productListControl.Margin = new Padding(3, 5, 3, 5); - _productListControl.Name = "_productListControl"; - _productListControl.SelectedRow = -1; - _productListControl.Size = new Size(487, 335); - _productListControl.TabIndex = 0; + _productTreeView.Dock = DockStyle.Fill; + _productTreeView.Location = new Point(0, 0); + _productTreeView.Margin = new Padding(3, 5, 3, 5); + _productTreeView.Name = "_productTreeView"; + _productTreeView.SelectedNodeIndex = -1; + _productTreeView.Size = new Size(292, 521); + _productTreeView.TabIndex = 0; // // MainForm // AutoScaleDimensions = new SizeF(8F, 20F); AutoScaleMode = AutoScaleMode.Font; - ClientSize = new Size(487, 335); - Controls.Add(_productListControl); + ClientSize = new Size(292, 521); + Controls.Add(_productTreeView); Margin = new Padding(3, 4, 3, 4); Name = "MainForm"; Text = "Учет продуктов в магазине"; @@ -58,7 +58,7 @@ #endregion - private Library15Gerimovich.OutputTableResults _productListControl; + private Library14Petrushin.HierarchicalTreeView _productTreeView; private Library15Gerimovich.WordTablesComponent wordTablesComponent1; } } \ No newline at end of file diff --git a/WinFormsLibrary1/WinFormsApp1/MainForm.cs b/WinFormsLibrary1/WinFormsApp1/MainForm.cs index e47303c..7e4a5b4 100644 --- a/WinFormsLibrary1/WinFormsApp1/MainForm.cs +++ b/WinFormsLibrary1/WinFormsApp1/MainForm.cs @@ -31,25 +31,14 @@ namespace Forms private void LoadProducts() { var products = _productLogic.ReadList(null); - _productListControl.ClearGrid(); - // Создаем список столбцов - var columns = new List - { - new ColumnInfo("ID", 50, false, "Id"), - new ColumnInfo("Название", 150, true, "Name"), - new ColumnInfo("Описание", 200, true, "Description"), - new ColumnInfo("Категория продукта", 150, true, "Category"), - new ColumnInfo("Количество товаров на складе", 150, true, "CountOnStorage") - }; + var hierarchy = new List { "Category", "CountOnStorageS", "Id", "Name" }; + var alwaysNewBranch = new Dictionary { { "Name", true } }; + _productTreeView.SetHierarchy(hierarchy, alwaysNewBranch); - // Настраиваем столбцы в DataGridView - _productListControl.ConfigureColumns(columns); - - // Добавляем строки foreach (var product in products) { - _productListControl.InsertValue(product); + _productTreeView.AddObjectToTree(product, "Name"); } } @@ -65,7 +54,7 @@ namespace Forms contextMenu.Items.Add("Создать документ с диаграммой", null, (s, ev) => CreateChartDocument()); // Привязка контекстного меню к компоненту вывода списков - _productListControl.ContextMenuStrip = contextMenu; + _productTreeView.ContextMenuStrip = contextMenu; // Настройка горячих клавиш this.KeyDown += MainForm_KeyDown; @@ -77,6 +66,34 @@ namespace Forms this.Controls.Add(toolStrip); } + private void MainForm_KeyDown(object sender, KeyEventArgs e) + { + if (e.Control) + { + switch (e.KeyCode) + { + case Keys.A: + AddProduct(); + break; + case Keys.U: + EditProduct(); + break; + case Keys.D: + DeleteProduct(); + break; + case Keys.S: + CreateDocument(); + break; + case Keys.T: + CreateTableDocument(); + break; + case Keys.C: + CreateChartDocument(); + break; + } + } + } + private void OpenProductForm() { var form = new ProductForm(_productLogic, _categoryLogic); @@ -128,37 +145,8 @@ namespace Forms return base.ProcessCmdKey(ref msg, keyData); } - private void MainForm_KeyDown(object sender, KeyEventArgs e) - { - if (e.Control) - { - switch (e.KeyCode) - { - case Keys.A: - AddProduct(); - break; - case Keys.U: - EditProduct(); - break; - case Keys.D: - DeleteProduct(); - break; - case Keys.S: - CreateDocument(); - break; - case Keys.T: - CreateTableDocument(); - break; - case Keys.C: - CreateChartDocument(); - break; - } - } - } - private void AddProduct() { - // Логика добавления нового продукта var form = new ProductForm(_productLogic, _categoryLogic); if (form.ShowDialog() == DialogResult.OK) { @@ -168,8 +156,11 @@ namespace Forms private void EditProduct() { - // Логика редактирования выбранного продукта - var selectedProduct = _productListControl.GetSelectedObject(); + var selectedProduct = _productTreeView.GetSelectedObject(); + selectedProduct = _productLogic.ReadElement(new ProductSearchModel + { + Id = int.Parse(selectedProduct.Id) + }); if (selectedProduct != null) { var form = new ProductForm(_productLogic, _categoryLogic, selectedProduct); @@ -182,14 +173,17 @@ namespace Forms private void DeleteProduct() { - // Логика удаления выбранного продукта - var selectedProduct = _productListControl.GetSelectedObject(); + var selectedProduct = _productTreeView.GetSelectedObject(); + selectedProduct = _productLogic.ReadElement(new ProductSearchModel + { + Id = int.Parse(selectedProduct.Id) + }); if (selectedProduct != null) { var result = MessageBox.Show("Вы действительно хотите удалить этот продукт?", "Подтверждение", MessageBoxButtons.YesNo); if (result == DialogResult.Yes) { - _productLogic.Delete(new ProductBindingModel { Id = selectedProduct.Id }); + _productLogic.Delete(new ProductBindingModel { Id = int.Parse(selectedProduct.Id) }); LoadProducts(); } } diff --git a/WinFormsLibrary1/WinFormsApp1/ProductForm.cs b/WinFormsLibrary1/WinFormsApp1/ProductForm.cs index 44aa411..835a549 100644 --- a/WinFormsLibrary1/WinFormsApp1/ProductForm.cs +++ b/WinFormsLibrary1/WinFormsApp1/ProductForm.cs @@ -52,11 +52,11 @@ namespace Forms { var model = new ProductBindingModel { - Id = _product?.Id ?? 0, + Id = string.IsNullOrEmpty(_product?.Id) ? 0 : int.Parse(_product?.Id), Name = textBoxName.Text, Description = textBoxDescription.Text, Category = _categoryComboBox.SelectedValue, - CountOnStorage = (int)_countOnStorageControl.DoubleValue + CountOnStorage = _countOnStorageControl.DoubleValue != null ? (int?)_countOnStorageControl.DoubleValue : null }; if (_isNewProduct) diff --git a/WinFormsLibrary1/WinFormsApp1/Program.cs b/WinFormsLibrary1/WinFormsApp1/Program.cs index 7d963d5..5ce4621 100644 --- a/WinFormsLibrary1/WinFormsApp1/Program.cs +++ b/WinFormsLibrary1/WinFormsApp1/Program.cs @@ -28,7 +28,7 @@ namespace WinFormsApp1 ConfigureServices(services); _serviceProvider = services.BuildServiceProvider(); - Application.Run(_serviceProvider.GetRequiredService()); + Application.Run(_serviceProvider.GetRequiredService()); } private static void ConfigureServices(ServiceCollection services) @@ -47,6 +47,7 @@ namespace WinFormsApp1 services.AddTransient(); services.AddTransient(); services.AddTransient(); + services.AddTransient(); } } } \ No newline at end of file diff --git a/WinFormsLibrary1/WinFormsLibrary1/ListBox.Designer.cs b/WinFormsLibrary1/WinFormsLibrary1/ListBox.Designer.cs index 5331df7..7e20fed 100644 --- a/WinFormsLibrary1/WinFormsLibrary1/ListBox.Designer.cs +++ b/WinFormsLibrary1/WinFormsLibrary1/ListBox.Designer.cs @@ -15,26 +15,27 @@ private void InitializeComponent() { - this.listBox = new System.Windows.Forms.ListBox(); - this.SuspendLayout(); + listBox = new ListBox(); + SuspendLayout(); // // listBox // - this.listBox.FormattingEnabled = true; - this.listBox.Location = new System.Drawing.Point(0, 0); - this.listBox.Name = "listBox"; - this.listBox.Size = new System.Drawing.Size(120, 95); - this.listBox.TabIndex = 0; - //this.listBox.SelectedIndexChanged += new System.EventHandler(this.listBox_SelectedIndexChanged); + listBox.FormattingEnabled = true; + listBox.Location = new Point(0, 0); + listBox.Margin = new Padding(4, 5, 4, 5); + listBox.Name = "listBox"; + listBox.Size = new Size(594, 144); + listBox.TabIndex = 0; // // ListBoxUserControl // - this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); - this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.Controls.Add(this.listBox); - this.Name = "ListBoxUserControl"; - this.Size = new System.Drawing.Size(120, 95); - this.ResumeLayout(false); + AutoScaleDimensions = new SizeF(8F, 20F); + AutoScaleMode = AutoScaleMode.Font; + Controls.Add(listBox); + Margin = new Padding(4, 5, 4, 5); + Name = "ListBoxUserControl"; + Size = new Size(598, 146); + ResumeLayout(false); } private System.Windows.Forms.ListBox listBox; diff --git a/WinFormsLibrary1/WinFormsLibrary1/ListBox.cs b/WinFormsLibrary1/WinFormsLibrary1/ListBox.cs index dc905e4..d71b09f 100644 --- a/WinFormsLibrary1/WinFormsLibrary1/ListBox.cs +++ b/WinFormsLibrary1/WinFormsLibrary1/ListBox.cs @@ -116,5 +116,30 @@ namespace WinFormsLibrary1 { return member is PropertyInfo property ? property.PropertyType : ((FieldInfo)member).FieldType; } + + public void AddObject(T obj) + { + if (obj == null) + { + throw new ArgumentNullException("Добавляемый объект не существует!"); + } + if (string.IsNullOrEmpty(_template) || !_startSymbol.HasValue || !_endSymbol.HasValue) + { + throw new Exception("Заполните макетную строку!"); + } + if (!_template.Contains((char)_startSymbol) || !_template.Contains((char)_endSymbol)) + { + throw new Exception("Макетная строка не содержит нужные элементы!"); + } + + string processedString = _template; + foreach (var property in obj.GetType().GetProperties()) + { + string placeholder = $"{_startSymbol}{property.Name}{_endSymbol}"; + processedString = processedString.Replace(placeholder, $"{_startSymbol}{property.GetValue(obj)}{_endSymbol}"); + } + + listBox.Items.Add(processedString); + } } } \ No newline at end of file diff --git a/WinFormsLibrary1/WinFormsLibrary1/ListBox.resx b/WinFormsLibrary1/WinFormsLibrary1/ListBox.resx index 1af7de1..8b2ff64 100644 --- a/WinFormsLibrary1/WinFormsLibrary1/ListBox.resx +++ b/WinFormsLibrary1/WinFormsLibrary1/ListBox.resx @@ -1,17 +1,17 @@  - diff --git a/WinFormsLibrary1/WinFormsLibrary1/WinFormsLibrary1.csproj b/WinFormsLibrary1/WinFormsLibrary1/WinFormsLibrary1.csproj index 45d0c53..13121d5 100644 --- a/WinFormsLibrary1/WinFormsLibrary1/WinFormsLibrary1.csproj +++ b/WinFormsLibrary1/WinFormsLibrary1/WinFormsLibrary1.csproj @@ -6,7 +6,7 @@ true enable True - 1.0.2 + 1.0.3 Library13StroevV