Diplom_B/ZakForm.cs

175 lines
4.7 KiB
C#
Raw Normal View History

2021-07-16 16:43:55 +05:00
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 Diplom_B.DB;
namespace Diplom_B
{
2021-07-17 09:33:09 +05:00
public partial class ZakForm : Form
2021-07-16 16:43:55 +05:00
{
private Task errDrop;
private void ShowError(string msg = null)
{
errorLabel.Text = string.IsNullOrEmpty(msg) ? "Неизвестная ошибка." : msg;
errorLabel.Visible = true;
errDrop = new Task(() =>
{
var fd = errDrop.Id;
Task.Delay(5000).Wait();
if (errDrop.Id == fd)
if (InvokeRequired) Invoke((Action)(() => { errorLabel.Visible = false; }));
else errorLabel.Visible = false;
});
errDrop.Start();
}
private void ClearBoxes()
{
idLabel.Text = "";
nameBox.Text = "";
adressBox.Text = "";
phoneBox.Text = "";
emailBox.Text = "";
}
private void UpdateZakTable(Zakazchik[] arr, bool reset_cursor = false)
{
var selected = (!reset_cursor && zakGridView.SelectedRows.Count > 0) ? zakGridView.SelectedRows[0].Index : -1;
{
var r = zakGridView.Rows;
while (r.Count > 0)
r.Remove(r[0]);
var c = zakGridView.Columns;
while (c.Count > 0)
c.Remove(c[0]);
}
{
var c = zakGridView.Columns;
c.Add("Id", "№");
c["Id"].Width = 40;
c.Add("Name", "Наименование");
c["Name"].Width = 120;
c.Add("Adress", "Адрес");
c["Adress"].Width = 120;
c["Adress"].DefaultCellStyle.WrapMode = DataGridViewTriState.True;
c.Add("Phone", "Телефон");
c["Phone"].Width = 100;
c.Add("Email", "Эл. почта");
c["Email"].Width = 100;
}
{
var r = zakGridView.Rows;
foreach (var zak in arr)
r.Add(new object[] {
zak.Id,
zak.Name,
zak.Adress,
zak.Phone,
zak.Email
});
}
if (zakGridView.Rows.Count > 0)
zakGridView.Rows[0].Selected = true;
if (selected != -1 && selected < zakGridView.Rows.Count)
for (var i = 0; i < zakGridView.Rows.Count; i++)
zakGridView.Rows[i].Selected = (i == selected);
zakGridView_CurrentCellChanged(this, new EventArgs());
}
2021-07-17 09:33:09 +05:00
public ZakForm()
2021-07-16 16:43:55 +05:00
{
InitializeComponent();
try { UpdateZakTable(WorkDB.ListZakazchik(searchBox.Text)); }
catch { ShowError(); }
}
private void zakGridView_CurrentCellChanged(object sender, EventArgs e)
{
ClearBoxes();
if (zakGridView.SelectedRows.Count != 1)
return;
{
var zak = WorkDB.GetZakazchik((int)zakGridView.SelectedRows[0].Cells[0].Value);
if (zak == null)
return;
idLabel.Text = zak.Id.ToString();
nameBox.Text = zak.Name;
adressBox.Text = zak.Adress;
phoneBox.Text = zak.Phone;
emailBox.Text = zak.Email;
}
}
private Task filterDrop;
private void searchBox_TextChanged(object sender, EventArgs e)
{
filterDrop = new Task(() =>
{
var fd = filterDrop.Id;
Task.Delay(1000).Wait();
if (filterDrop.Id == fd)
if (InvokeRequired) Invoke((Action)(() => { UpdateZakTable(WorkDB.ListZakazchik(searchBox.Text)); }));
else UpdateZakTable(WorkDB.ListZakazchik(searchBox.Text));
});
filterDrop.Start();
}
private void resetSearchButton_Click(object sender, EventArgs e)
{
searchBox.Text = "";
filterDrop = new Task(() => { return; });
UpdateZakTable(WorkDB.ListZakazchik(searchBox.Text));
}
private void createZakButton_Click(object sender, EventArgs e)
{
try
{
var r = new Zakazchik()
{
Name = nameBox.Text,
Adress = adressBox.Text,
Phone = phoneBox.Text,
Email = emailBox.Text
};
WorkDB.AddZakazchik(r);
UpdateZakTable(WorkDB.ListZakazchik(searchBox.Text));
}
catch { ShowError(); }
}
private void changeZakButton_Click(object sender, EventArgs e)
{
if (!int.TryParse(idLabel.Text, out int idRes)) { ShowError("Заказчик не выбран."); return; }
var zak = WorkDB.GetZakazchik(idRes);
if (zak == null) { ShowError("Нет заказчика в БД."); return; }
try
{
zak.Name = nameBox.Text;
zak.Adress = adressBox.Text;
zak.Phone = phoneBox.Text;
zak.Email = emailBox.Text;
WorkDB.ChangeZakazchik(zak);
}
catch { ShowError(); }
UpdateZakTable(WorkDB.ListZakazchik(searchBox.Text));
}
private void deleteZakButton_Click(object sender, EventArgs e)
{
if (!int.TryParse(idLabel.Text, out int idRes)) { ShowError("Заказчик не выбран."); return; }
var zak = WorkDB.GetZakazchik(idRes);
if (zak == null) { ShowError("Заказчик не существует."); return; }
try { WorkDB.DeleteZakazchik(zak); }
catch { ShowError(); }
UpdateZakTable(WorkDB.ListZakazchik(searchBox.Text));
}
private void resetZakButton_Click(object sender, EventArgs e)
{
ClearBoxes();
}
}
}