Diplom_O/MainForms/ShtatForm.cs

170 lines
4.1 KiB
C#
Raw Normal View History

2021-07-21 16:57:47 +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_O.DataBase;
namespace Diplom_O
{
public partial class ShtatForm : Form
{
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 Task filterDrop;
private void findBox_TextChanged(object sender, EventArgs e)
{
filterDrop = new Task(() =>
{
var fd = filterDrop.Id;
Task.Delay(1000).Wait();
if (filterDrop.Id == fd)
if (InvokeRequired) Invoke((Action)(() => { resetShatTable(); }));
else resetShatTable();
});
filterDrop.Start();
}
private void dropFindButton_Click(object sender, EventArgs e)
{
findBox.Text = "";
filterDrop = new Task(() => { return; });
resetShatTable();
}
private void resetShatTable()
{
try
{
{
shtatGridView.Rows.Clear();
shtatGridView.Columns.Clear();
var c = shtatGridView.Columns;
c.Add("Id", "№");
c.Add("Dolj", "Должность");
c.Add("Kolvo", "Кол-во");
c.Add("Free", "Своб.");
c[0].Width = 40;
c[1].Width = 120;
c[2].Width = 40;
c[3].Width = 40;
}
{
var arr = FuncDB.ListShtat(findBox.Text);
var r = shtatGridView.Rows;
foreach (var shtat in arr)
if (shtat.ost != 0)
r.Add(new object[] {
shtat.shtat.Id,
shtat.shtat.Doljnost,
shtat.shtat.Size,
shtat.ost
});
}
if (shtatGridView.Rows.Count > 0)
shtatGridView.Rows[0].Selected = true;
}
catch (Exception e) { ShowError(e.Message); }
}
private Shtat selectedShtat()
{
try
{
if (shtatGridView.SelectedRows.Count != 1) throw new Exception("Должность не выбрана.");
var shtat = FuncDB.GetShtat((int)shtatGridView.SelectedRows[0].Cells[0].Value);
return shtat;
}
catch (Exception e) { ShowError(e.Message); return null; }
}
public Shtat formResult = null;
public ShtatForm(bool select = false)
{
try
{
InitializeComponent();
Init(select);
resetShatTable();
}
catch { this.Close(); }
}
public void Init(bool s)
{
if (s) selectButton.Visible = true;
shtatMI.Enabled = false;
}
private void addButton_Click(object sender, EventArgs e)
{
var res = new SupportForms.WorkShtatForm();
res.ShowDialog();
if (!res.isCanceled)
resetShatTable();
}
private void changeButton_Click(object sender, EventArgs e)
{
var shtat = selectedShtat();
if (shtat == null) { ShowError("Должность не выбрана."); return; }
var res = new SupportForms.WorkShtatForm(shtat);
res.ShowDialog();
if (!res.isCanceled)
resetShatTable();
}
private void delButton_Click(object sender, EventArgs e)
{
var shtat = selectedShtat();
if (shtat == null) { ShowError("Должность не выбрана."); return; }
try
{
FuncDB.DelShtat(shtat);
resetShatTable();
}
catch (Exception ex) { ShowError(ex.Message); }
}
public void MI_Click(object sender, EventArgs e)
{
object form = null;
var idxMenu = -1;
try { idxMenu = menuStrip.Items.IndexOf((ToolStripMenuItem)sender); }
catch { };
switch (idxMenu)
{
case 0: form = new ShtatForm(); break;
case 1: form = new RabForm(); break;
case 2: form = new OtpForm(); break;
case 3: form = new ChelForm(); break;
}
if (form != null)
{
this.Hide();
((Form)form).Closed += (s, args) => this.Close();
((Form)form).Show();
return;
}
ShowError("Ошибка перехода на новую форму.");
}
private void freeShtatCheckBox_CheckedChanged(object sender, EventArgs e)
{
resetShatTable();
}
}
}