Загрузка данных
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using LibraryApp.Console.Models;
using LibraryApp.WinForms.Views;
namespace LibraryApp.WinForms.Forms
{
public partial class BookForm : Form, IBookView
{
private DataGridView dgvBooks;
private TextBox txtTitle, txtAuthor, txtYear;
private Button btnAdd, btnIssue, btnReturn;
private Label lblTitle, lblAuthor, lblYear;
public BookForm()
{
InitializeComponent();
SetupUI();
}
private void InitializeComponent()
{
this.Text = "Библиотека книг";
this.Size = new System.Drawing.Size(800, 600);
this.StartPosition = FormStartPosition.CenterScreen;
}
private void SetupUI()
{
// DataGridView
dgvBooks = new DataGridView
{
Location = new System.Drawing.Point(12, 12),
Size = new System.Drawing.Size(760, 300),
ReadOnly = true,
AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill,
SelectionMode = DataGridViewSelectionMode.FullRowSelect
};
// Поля ввода
int y = 330;
lblTitle = new Label { Text = "Название:", Location = new System.Drawing.Point(12, y), Size = new System.Drawing.Size(80, 25) };
txtTitle = new TextBox { Location = new System.Drawing.Point(100, y), Size = new System.Drawing.Size(200, 25) };
y += 35;
lblAuthor = new Label { Text = "Автор:", Location = new System.Drawing.Point(12, y), Size = new System.Drawing.Size(80, 25) };
txtAuthor = new TextBox { Location = new System.Drawing.Point(100, y), Size = new System.Drawing.Size(200, 25) };
y += 35;
lblYear = new Label { Text = "Год:", Location = new System.Drawing.Point(12, y), Size = new System.Drawing.Size(80, 25) };
txtYear = new TextBox { Location = new System.Drawing.Point(100, y), Size = new System.Drawing.Size(200, 25) };
// Кнопки
y += 45;
btnAdd = new Button
{
Text = "Добавить книгу",
Location = new System.Drawing.Point(12, y),
Size = new System.Drawing.Size(150, 40)
};
btnIssue = new Button
{
Text = "Выдать книгу",
Location = new System.Drawing.Point(180, y),
Size = new System.Drawing.Size(150, 40)
};
btnReturn = new Button
{
Text = "Вернуть книгу",
Location = new System.Drawing.Point(348, y),
Size = new System.Drawing.Size(150, 40)
};
// Добавление контролов
this.Controls.AddRange(new Control[] {
dgvBooks,
lblTitle, txtTitle,
lblAuthor, txtAuthor,
lblYear, txtYear,
btnAdd, btnIssue, btnReturn
});
// Подписка на события
btnAdd.Click += (s, e) => AddBook?.Invoke(this, EventArgs.Empty);
btnIssue.Click += (s, e) => IssueBook?.Invoke(this, EventArgs.Empty);
btnReturn.Click += (s, e) => ReturnBook?.Invoke(this, EventArgs.Empty);
// Подписка на выделение строки
dgvBooks.SelectionChanged += (s, e) =>
{
if (dgvBooks.SelectedRows.Count > 0)
{
var row = dgvBooks.SelectedRows[0];
SelectedBookId = row.Cells[0].Value?.ToString();
}
};
}
// Реализация IBookView
public string BookTitle
{
get => txtTitle.Text;
set => txtTitle.Text = value;
}
public string BookAuthor
{
get => txtAuthor.Text;
set => txtAuthor.Text = value;
}
public string BookYear
{
get => txtYear.Text;
set => txtYear.Text = value;
}
public string SelectedBookId { get; set; }
public event EventHandler AddBook;
public event EventHandler IssueBook;
public event EventHandler ReturnBook;
public event EventHandler RefreshBooks;
public void ShowBooks(List<Book> books)
{
dgvBooks.DataSource = null;
dgvBooks.DataSource = books;
}
public void ShowMessage(string message)
{
MessageBox.Show(message, "Сообщение", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
public void ClearFields()
{
txtTitle.Clear();
txtAuthor.Clear();
txtYear.Clear();
}
}
}