diff --git a/LabWork/LabWork/FormContact.Designer.cs b/LabWork/LabWork/FormContact.Designer.cs new file mode 100644 index 0000000..619f6be --- /dev/null +++ b/LabWork/LabWork/FormContact.Designer.cs @@ -0,0 +1,118 @@ +namespace LabWork +{ + partial class FormContact + { + /// + /// Required designer variable. + /// + private System.ComponentModel.IContainer components = null; + + /// + /// Clean up any resources being used. + /// + /// true if managed resources should be disposed; otherwise, false. + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Windows Form Designer generated code + + /// + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. + /// + private void InitializeComponent() + { + textBoxName = new TextBox(); + textBoxNumber = new TextBox(); + buttonSave = new Button(); + buttonCancel = new Button(); + label1 = new Label(); + label2 = new Label(); + SuspendLayout(); + // + // textBoxName + // + textBoxName.Location = new Point(190, 51); + textBoxName.Name = "textBoxName"; + textBoxName.Size = new Size(438, 31); + textBoxName.TabIndex = 0; + // + // textBoxNumber + // + textBoxNumber.Location = new Point(190, 132); + textBoxNumber.Name = "textBoxNumber"; + textBoxNumber.Size = new Size(438, 31); + textBoxNumber.TabIndex = 1; + // + // buttonSave + // + buttonSave.Location = new Point(475, 207); + buttonSave.Name = "buttonSave"; + buttonSave.Size = new Size(112, 43); + buttonSave.TabIndex = 2; + buttonSave.Text = "Сохранить"; + buttonSave.UseVisualStyleBackColor = true; + buttonSave.Click += buttonSave_Click; + // + // buttonCancel + // + buttonCancel.Location = new Point(616, 207); + buttonCancel.Name = "buttonCancel"; + buttonCancel.Size = new Size(112, 43); + buttonCancel.TabIndex = 3; + buttonCancel.Text = "Отменить"; + buttonCancel.UseVisualStyleBackColor = true; + buttonCancel.Click += buttonCancel_Click; + // + // label1 + // + label1.AutoSize = true; + label1.Location = new Point(55, 54); + label1.Name = "label1"; + label1.Size = new Size(127, 25); + label1.TabIndex = 4; + label1.Text = "Имя контакта:"; + // + // label2 + // + label2.AutoSize = true; + label2.Location = new Point(109, 135); + label2.Name = "label2"; + label2.Size = new Size(73, 25); + label2.TabIndex = 5; + label2.Text = "Номер:"; + // + // FormContact + // + AutoScaleDimensions = new SizeF(10F, 25F); + AutoScaleMode = AutoScaleMode.Font; + ClientSize = new Size(800, 288); + Controls.Add(label2); + Controls.Add(label1); + Controls.Add(buttonCancel); + Controls.Add(buttonSave); + Controls.Add(textBoxNumber); + Controls.Add(textBoxName); + Name = "FormContact"; + Text = "Редактирование контакта"; + Load += FormComponent_Load; + ResumeLayout(false); + PerformLayout(); + } + + #endregion + + private TextBox textBoxName; + private TextBox textBoxNumber; + private Button buttonSave; + private Button buttonCancel; + private Label label1; + private Label label2; + } +} \ No newline at end of file diff --git a/LabWork/LabWork/FormContact.cs b/LabWork/LabWork/FormContact.cs new file mode 100644 index 0000000..017bcdd --- /dev/null +++ b/LabWork/LabWork/FormContact.cs @@ -0,0 +1,87 @@ +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; +using LabWork.Interfaces; + +namespace LabWork +{ + public partial class FormContact : Form + { + private readonly IContactLogic _logic; + private int? _id; + public int Id { set { _id = value; } } + public FormContact(IContactLogic logic) + { + InitializeComponent(); + _logic = logic; + } + private void FormComponent_Load(object sender, EventArgs e) + { + if (_id.HasValue) + { + try + { + var view = _logic.ReadElement(new ContactSearchModel + { + Id = + _id.Value + }); + if (view != null) + { + textBoxName.Text = view.ContactName; + textBoxNumber.Text = view.Number.ToString(); + } + } + catch (Exception ex) + { + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, + MessageBoxIcon.Error); + } + } + } + private void buttonSave_Click(object sender, EventArgs e) + { + if (string.IsNullOrEmpty(textBoxName.Text)) + { + MessageBox.Show("Заполните название", "Ошибка", + MessageBoxButtons.OK, MessageBoxIcon.Error); + return; + } + try + { + var model = new ContactBindingModel + { + Id = _id ?? 0, + ContactName = textBoxName.Text, + Number = Convert.ToDouble(textBoxNumber.Text) + }; + var operationResult = _id.HasValue ? _logic.Update(model) : + _logic.Create(model); + if (!operationResult) + { + throw new Exception("Ошибка при сохранении. Дополнительная информация в логах."); + } + MessageBox.Show("Сохранение прошло успешно", "Сообщение", + MessageBoxButtons.OK, MessageBoxIcon.Information); + DialogResult = DialogResult.OK; + Close(); + } + catch (Exception ex) + { + MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, + MessageBoxIcon.Error); + } + } + private void buttonCancel_Click(object sender, EventArgs e) + { + DialogResult = DialogResult.Cancel; + Close(); + } + } +} diff --git a/LabWork/LabWork/FormContact.resx b/LabWork/LabWork/FormContact.resx new file mode 100644 index 0000000..af32865 --- /dev/null +++ b/LabWork/LabWork/FormContact.resx @@ -0,0 +1,120 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git a/LabWork/LabWork/FormContacts.cs b/LabWork/LabWork/FormContacts.cs index 3eef2cb..86b0b81 100644 --- a/LabWork/LabWork/FormContacts.cs +++ b/LabWork/LabWork/FormContacts.cs @@ -1,10 +1,109 @@ +using LabWork.Interfaces; + namespace LabWork { public partial class FormContacts : Form { - public FormContacts() + private readonly IContactLogic _logic; + public FormContacts(IContactLogic logic) { 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(); + } + + private void InitializeComponent() + { + SuspendLayout(); + // + // FormContacts + // + ClientSize = new Size(278, 244); + Name = "FormContacts"; + Load += FormContacts_Load; + ResumeLayout(false); } } } diff --git a/LabWork/LabWork/FormContacts.resx b/LabWork/LabWork/FormContacts.resx index f7ea5a3..af32865 100644 --- a/LabWork/LabWork/FormContacts.resx +++ b/LabWork/LabWork/FormContacts.resx @@ -56,7 +56,7 @@ mimetype: application/x-microsoft.net.object.bytearray.base64 value : The object must be serialized into a byte array - : using a System.ContactModel.TypeConverter + : using a System.ComponentModel.TypeConverter : and then encoded with base64 encoding. -->