From 1fd999137b4a6c81e7a3214e2e919a86f1cb6588 Mon Sep 17 00:00:00 2001 From: Aleksandr4350 Date: Tue, 3 Dec 2024 20:14:30 +0400 Subject: [PATCH] lab 1 fc --- ProjectLibrary/Entites/Book.cs | 8 +-- ProjectLibrary/Forms/FBook.Designer.cs | 51 +++++++++---------- ProjectLibrary/Forms/FBook.cs | 47 +++++++++++++++-- ProjectLibrary/Forms/FBook.resx | 4 +- ProjectLibrary/Forms/FBooks.cs | 20 +++++++- ProjectLibrary/Forms/FLibrary.cs | 7 ++- .../Forms/FTicket_Extension.Designer.cs | 14 ++--- ProjectLibrary/Forms/FTicket_Extension.resx | 4 +- .../Implementations/BookRepository.cs | 2 +- 9 files changed, 110 insertions(+), 47 deletions(-) diff --git a/ProjectLibrary/Entites/Book.cs b/ProjectLibrary/Entites/Book.cs index 2ad8a04..bb5894b 100644 --- a/ProjectLibrary/Entites/Book.cs +++ b/ProjectLibrary/Entites/Book.cs @@ -7,18 +7,18 @@ public int Id { get; private set; } public string Author { get; private set; } = string.Empty; public string Name { get; private set; } = string.Empty; - public BookType Type { get; private set; } = BookType.None; + public BookType Type { get; set; } = BookType.None; public int LibraryID { get; private set; } - public static Book CreateEntity(int id, string author, string name, BookType type, int libraryID) + public static Book CreateEntity(int id, string author, string name, int libraryID, BookType type = BookType.None) { return new Book { Id = id, Author = author ?? string.Empty, Name = name ?? string.Empty, - Type = type, - LibraryID = libraryID + LibraryID = libraryID, + Type = type }; } } diff --git a/ProjectLibrary/Forms/FBook.Designer.cs b/ProjectLibrary/Forms/FBook.Designer.cs index 103ab72..853de9c 100644 --- a/ProjectLibrary/Forms/FBook.Designer.cs +++ b/ProjectLibrary/Forms/FBook.Designer.cs @@ -33,11 +33,11 @@ lblName = new Label(); txtName = new TextBox(); lblType = new Label(); - cmbType = new ComboBox(); lblLibraryId = new Label(); btnSave = new Button(); buttonCancel_Click = new Button(); comboBoxLibrary = new ComboBox(); + checkedListBox1 = new CheckedListBox(); SuspendLayout(); // // lblAuthor @@ -45,15 +45,15 @@ lblAuthor.AutoSize = true; lblAuthor.Location = new Point(19, 25); lblAuthor.Name = "lblAuthor"; - lblAuthor.Size = new Size(43, 15); + lblAuthor.Size = new Size(54, 20); lblAuthor.TabIndex = 2; lblAuthor.Text = "Автор:"; // // txtAuthor // - txtAuthor.Location = new Point(115, 25); + txtAuthor.Location = new Point(105, 25); txtAuthor.Name = "txtAuthor"; - txtAuthor.Size = new Size(200, 23); + txtAuthor.Size = new Size(246, 27); txtAuthor.TabIndex = 3; // // lblName @@ -61,15 +61,15 @@ lblName.AutoSize = true; lblName.Location = new Point(19, 65); lblName.Name = "lblName"; - lblName.Size = new Size(62, 15); + lblName.Size = new Size(80, 20); lblName.TabIndex = 4; lblName.Text = "Название:"; // // txtName // - txtName.Location = new Point(115, 65); + txtName.Location = new Point(105, 65); txtName.Name = "txtName"; - txtName.Size = new Size(200, 23); + txtName.Size = new Size(246, 27); txtName.TabIndex = 5; // // lblType @@ -77,32 +77,22 @@ lblType.AutoSize = true; lblType.Location = new Point(19, 105); lblType.Name = "lblType"; - lblType.Size = new Size(30, 15); + lblType.Size = new Size(38, 20); lblType.TabIndex = 6; lblType.Text = "Тип:"; // - // cmbType - // - cmbType.DropDownStyle = ComboBoxStyle.DropDownList; - cmbType.FormattingEnabled = true; - cmbType.Items.AddRange(new object[] { "Fiction", "Non-Fiction", "Science", "Biography" }); - cmbType.Location = new Point(115, 102); - cmbType.Name = "cmbType"; - cmbType.Size = new Size(200, 23); - cmbType.TabIndex = 7; - // // lblLibraryId // lblLibraryId.AutoSize = true; - lblLibraryId.Location = new Point(19, 145); + lblLibraryId.Location = new Point(19, 226); lblLibraryId.Name = "lblLibraryId"; - lblLibraryId.Size = new Size(90, 15); + lblLibraryId.Size = new Size(114, 20); lblLibraryId.TabIndex = 8; lblLibraryId.Text = "ID библиотеки:"; // // btnSave // - btnSave.Location = new Point(19, 185); + btnSave.Location = new Point(19, 266); btnSave.Name = "btnSave"; btnSave.Size = new Size(100, 30); btnSave.TabIndex = 10; @@ -112,7 +102,7 @@ // // buttonCancel_Click // - buttonCancel_Click.Location = new Point(239, 185); + buttonCancel_Click.Location = new Point(251, 266); buttonCancel_Click.Name = "buttonCancel_Click"; buttonCancel_Click.Size = new Size(100, 30); buttonCancel_Click.TabIndex = 11; @@ -124,14 +114,24 @@ comboBoxLibrary.DropDownStyle = ComboBoxStyle.DropDownList; comboBoxLibrary.FormattingEnabled = true; comboBoxLibrary.Items.AddRange(new object[] { "Fiction", "Non-Fiction", "Science", "Biography" }); - comboBoxLibrary.Location = new Point(115, 145); + comboBoxLibrary.Location = new Point(151, 223); comboBoxLibrary.Name = "comboBoxLibrary"; - comboBoxLibrary.Size = new Size(200, 23); + comboBoxLibrary.Size = new Size(200, 28); comboBoxLibrary.TabIndex = 12; // + // checkedListBox1 + // + checkedListBox1.FormattingEnabled = true; + checkedListBox1.Location = new Point(105, 105); + checkedListBox1.Name = "checkedListBox1"; + checkedListBox1.Size = new Size(246, 114); + checkedListBox1.TabIndex = 13; + checkedListBox1.SelectedIndexChanged += checkedListBox1_SelectedIndexChanged; + // // FBook // ClientSize = new Size(400, 300); + Controls.Add(checkedListBox1); Controls.Add(comboBoxLibrary); Controls.Add(buttonCancel_Click); Controls.Add(lblAuthor); @@ -139,7 +139,6 @@ Controls.Add(lblName); Controls.Add(txtName); Controls.Add(lblType); - Controls.Add(cmbType); Controls.Add(lblLibraryId); Controls.Add(btnSave); Name = "FBook"; @@ -154,10 +153,10 @@ private System.Windows.Forms.Label lblName; private System.Windows.Forms.TextBox txtName; private System.Windows.Forms.Label lblType; - private System.Windows.Forms.ComboBox cmbType; private System.Windows.Forms.Label lblLibraryId; private System.Windows.Forms.Button btnSave; private Button buttonCancel_Click; private ComboBox comboBoxLibrary; + private CheckedListBox checkedListBox1; } } diff --git a/ProjectLibrary/Forms/FBook.cs b/ProjectLibrary/Forms/FBook.cs index 7a7988c..9732e74 100644 --- a/ProjectLibrary/Forms/FBook.cs +++ b/ProjectLibrary/Forms/FBook.cs @@ -24,9 +24,10 @@ namespace ProjectLibrary.Forms txtAuthor.Text = book.Author; txtName.Text = book.Name; - cmbType.SelectedItem = book.Type; + //cmbType.SelectedItem = book.Type; comboBoxLibrary.SelectedItem = book.LibraryID; _bookId = value; + } catch (Exception ex) { @@ -40,6 +41,10 @@ namespace ProjectLibrary.Forms InitializeComponent(); _bookRepository = bookRepository ?? throw new ArgumentNullException(nameof(bookRepository)); comboBoxLibrary.DataSource = libraryRepository.ReadLibraries(); + foreach(var elem in Enum.GetValues(typeof(BookType))) + { + if (!elem.Equals(BookType.None)) checkedListBox1.Items.Add(elem); + } } private void btnSave_Click(object sender, EventArgs e) @@ -51,12 +56,22 @@ namespace ProjectLibrary.Forms throw new Exception("Имеются незаполненные поля."); } + BookType selectedType = BookType.None; + + foreach (var item in checkedListBox1.CheckedItems) + { + if (item is BookType type) + { + selectedType |= type; + } + } + var book = Book.CreateEntity( _bookId ?? 0, txtAuthor.Text, txtName.Text, - (BookType)cmbType.SelectedValue, - Convert.ToInt32(comboBoxLibrary.SelectedItem) + Convert.ToInt32(comboBoxLibrary.SelectedItem), + selectedType ); if (_bookId.HasValue) @@ -80,5 +95,31 @@ namespace ProjectLibrary.Forms { Close(); } + + private void checkedListBox1_SelectedIndexChanged(object sender, EventArgs e) + { + try + { + BookType selectedType = BookType.None; + + + foreach (var item in checkedListBox1.CheckedItems) + { + if (item is BookType type) + { + selectedType |= type; + } + } + + + var book = _bookRepository.ReadBookById(_bookId ?? 0); + book.Type = selectedType; + } + catch (Exception ex) + { + MessageBox.Show(ex.Message, "Ошибка при обработке выбора", MessageBoxButtons.OK, MessageBoxIcon.Error); + } + } + } } diff --git a/ProjectLibrary/Forms/FBook.resx b/ProjectLibrary/Forms/FBook.resx index af32865..8b2ff64 100644 --- a/ProjectLibrary/Forms/FBook.resx +++ b/ProjectLibrary/Forms/FBook.resx @@ -1,7 +1,7 @@