+ форма (заново)

This commit is contained in:
malimova 2024-05-08 13:54:02 +04:00
parent fe8462c3f0
commit 66960902c9
3 changed files with 45 additions and 130 deletions

View File

@ -3,19 +3,19 @@
partial class FormContacts
{
/// <summary>
/// Required designer variable.
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer Contacts = null;
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (Contacts != null))
if (disposing && (components != null))
{
Contacts.Dispose();
components.Dispose();
}
base.Dispose(disposing);
}
@ -23,61 +23,61 @@
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeContact()
private void InitializeComponent()
{
dataGridView = new DataGridView();
dataGridView1 = new DataGridView();
buttonAdd = new Button();
buttonUpd = new Button();
buttonRef = new Button();
buttonDel = new Button();
((System.ComponentModel.ISupportInitialize)dataGridView).BeginInit();
((System.ComponentModel.ISupportInitialize)dataGridView1).BeginInit();
SuspendLayout();
//
// dataGridView
// dataGridView1
//
dataGridView.BackgroundColor = Color.LavenderBlush;
dataGridView.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize;
dataGridView.Location = new Point(12, 90);
dataGridView.Name = "dataGridView";
dataGridView.RowHeadersWidth = 62;
dataGridView.Size = new Size(789, 453);
dataGridView.TabIndex = 0;
dataGridView1.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize;
dataGridView1.Location = new Point(12, 12);
dataGridView1.Name = "dataGridView1";
dataGridView1.RowHeadersWidth = 62;
dataGridView1.Size = new Size(717, 426);
dataGridView1.TabIndex = 0;
//
// buttonAdd
//
buttonAdd.Location = new Point(831, 113);
buttonAdd.Location = new Point(797, 94);
buttonAdd.Name = "buttonAdd";
buttonAdd.Size = new Size(163, 64);
buttonAdd.Size = new Size(112, 34);
buttonAdd.TabIndex = 1;
buttonAdd.Text = "Добавить";
buttonAdd.UseVisualStyleBackColor = true;
buttonAdd.Click += buttonAdd_Click;
//
// buttonUpd
//
buttonUpd.Location = new Point(831, 200);
buttonUpd.Location = new Point(797, 157);
buttonUpd.Name = "buttonUpd";
buttonUpd.Size = new Size(163, 64);
buttonUpd.Size = new Size(112, 34);
buttonUpd.TabIndex = 2;
buttonUpd.Text = "Изменить";
buttonUpd.UseVisualStyleBackColor = true;
//
// buttonRef
//
buttonRef.Location = new Point(831, 286);
buttonRef.Location = new Point(797, 223);
buttonRef.Name = "buttonRef";
buttonRef.Size = new Size(163, 64);
buttonRef.Size = new Size(112, 34);
buttonRef.TabIndex = 3;
buttonRef.Text = "Обновить";
buttonRef.UseVisualStyleBackColor = true;
//
// buttonDel
//
buttonDel.Location = new Point(831, 375);
buttonDel.Location = new Point(797, 284);
buttonDel.Name = "buttonDel";
buttonDel.Size = new Size(163, 64);
buttonDel.Size = new Size(112, 34);
buttonDel.TabIndex = 4;
buttonDel.Text = "Удалить";
buttonDel.UseVisualStyleBackColor = true;
@ -86,22 +86,21 @@
//
AutoScaleDimensions = new SizeF(10F, 25F);
AutoScaleMode = AutoScaleMode.Font;
BackColor = SystemColors.ActiveCaption;
ClientSize = new Size(1019, 555);
ClientSize = new Size(965, 450);
Controls.Add(buttonDel);
Controls.Add(buttonRef);
Controls.Add(buttonUpd);
Controls.Add(buttonAdd);
Controls.Add(dataGridView);
Controls.Add(dataGridView1);
Name = "FormContacts";
Text = "Менеджер контактов";
((System.ComponentModel.ISupportInitialize)dataGridView).EndInit();
((System.ComponentModel.ISupportInitialize)dataGridView1).EndInit();
ResumeLayout(false);
}
#endregion
private DataGridView dataGridView;
private DataGridView dataGridView1;
private Button buttonAdd;
private Button buttonUpd;
private Button buttonRef;

View File

@ -1,109 +1,25 @@
using LabWork.Interfaces;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace LabWork
{
public partial class FormContacts : Form
{
private readonly IContactLogic _logic;
public FormContacts(IContactLogic logic)
public FormContacts()
{
InitializeContact();
_logic = logic;
}
private void FormContacts_Load(object sender, EventArgs e)
{
LoadData();
}
private void LoadData()
{
try
{
var list = _logic.ReadList(null);
if (list != null)
{
dataGridView.DataSource = list;
dataGridView.Columns["Id"].Visible = false;
dataGridView.Columns["ContactName"].AutoSizeMode =
DataGridViewAutoSizeColumnMode.Fill;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
}
private void ButtonAdd_Click(object sender, EventArgs e)
{
var service =
Program.ServiceProvider?.GetService(typeof(FormContact));
if (service is FormContact form)
{
if (form.ShowDialog() == DialogResult.OK)
{
LoadData();
}
}
}
private void ButtonUpd_Click(object sender, EventArgs e)
{
if (dataGridView.SelectedRows.Count == 1)
{
var service =
Program.ServiceProvider?.GetService(typeof(FormContact));
if (service is FormContact form)
{
form.Id = Convert.ToInt32(dataGridView.SelectedRows[0].Cells["Id"].Value);
if (form.ShowDialog() == DialogResult.OK)
{
LoadData();
}
}
}
}
private void ButtonDel_Click(object sender, EventArgs e)
{
if (dataGridView.SelectedRows.Count == 1)
{
if (MessageBox.Show("Удалить запись?", "Вопрос",
MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
int id =
Convert.ToInt32(dataGridView.SelectedRows[0].Cells["Id"].Value);
try
{
if (!_logic.Delete(new ContactBindingModel
{
Id = id
}))
{
throw new Exception("Ошибка при удалении. Дополнительная информация в логах.");
}
LoadData();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Ошибка",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
}
private void ButtonRef_Click(object sender, EventArgs e)
{
LoadData();
InitializeComponent();
}
private void InitializeComponent()
private void buttonAdd_Click(object sender, EventArgs e)
{
SuspendLayout();
//
// FormContacts
//
ClientSize = new Size(278, 244);
Name = "FormContacts";
Load += FormContacts_Load;
ResumeLayout(false);
}
}
}

View File

@ -25,7 +25,7 @@ namespace LabWork
{
services.AddTransient<IContactStorage, ContactStorage>();
services.AddTransient<IContactLogic, ContactLogic>();
//services.AddTransient<FormContact>();
services.AddTransient<FormContact>();
services.AddTransient<FormContacts>();
}
}