Diplom_B/IzvForm.cs

243 lines
7.0 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System;
using System.IO;
using System.Threading.Tasks;
using System.Windows.Forms;
using Diplom_B.DB;
namespace Diplom_B
{
public partial class IzvForm : 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 string fileName = "";
private byte[] fileStruct = null;
private void UpdateLink(string name = null, byte[] strct = null)
{
var size = 23;
linkFileLabel.Text = "";
linkFileLabel.Visible = false;
fileName = name;
fileStruct = strct;
if (string.IsNullOrEmpty(fileName) || fileStruct == null || fileStruct.Length <= 0)
return;
linkFileLabel.Text = (fileName.Length > size) ? fileName.Substring(0, size - 3) + "..." : fileName;
linkFileLabel.Visible = true;
}
private void ClearBoxes()
{
idLabel.Text = "";
izvNumBox.Text = "";
invNumBox.Text = "";
izmNumBox.Text = "";
ukazZadBox.Text = "";
ukazVnedrBox.Text = "";
UpdateLink();
}
private void UpdateIzvTable(Izveschenie[] arr, bool reset_cursor = false)
{
var selected = (!reset_cursor && izvGridView.SelectedRows.Count > 0) ? izvGridView.SelectedRows[0].Index : -1;
{
var r = izvGridView.Rows;
while (r.Count > 0)
r.Remove(r[0]);
var c = izvGridView.Columns;
while (c.Count > 0)
c.Remove(c[0]);
}
{
var c = izvGridView.Columns;
c.Add("Id", "№");
c["Id"].Width = 40;
c.Add("IzvNum", "Извещение №");
c["IzvNum"].Width = 90;
c.Add("InvNum", "Инв. №");
c["InvNum"].Width = 60;
c.Add("IzmNum", "Изм. №");
c["IzmNum"].Width = 60;
c.Add("UkazZad", "Указания о заделе");
c["UkazZad"].Width = 130;
c.Add("UkazVnedr", "Указания о внедр.");
c["UkazVnedr"].Width = 130;
}
{
var r = izvGridView.Rows;
foreach (var izv in arr)
r.Add(new object[] {
izv.Id,
izv.IzvNum,
izv.InvNum,
izv.IzmNum,
izv.UkazZad,
izv.UkazVnedr
});
}
if (izvGridView.Rows.Count > 0)
izvGridView.Rows[0].Selected = true;
if (selected != -1 && selected < izvGridView.Rows.Count)
for (var i = 0; i < izvGridView.Rows.Count; i++)
izvGridView.Rows[i].Selected = (i == selected);
izvGridView_CurrentCellChanged(this, new EventArgs());
}
public IzvForm()
{
InitializeComponent();
try { UpdateIzvTable(WorkDB.ListIzveschenie(searchBox.Text)); }
catch { ShowError(); }
}
private void fileLoadButton_Click(object sender, EventArgs e)
{
if (selectFileDialog.ShowDialog() == DialogResult.Cancel)
{
ShowError("Файл не выбран.");
return;
}
try
{
UpdateLink();
var fn = selectFileDialog.FileName;
if (string.IsNullOrEmpty(fn))
{
ShowError("Ошибка в названии файла.");
return;
}
UpdateLink(Path.GetFileName(fn), File.ReadAllBytes(fn));
}
catch { ShowError(); }
}
private void linkFileLabel_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(fileName) || fileStruct == null || fileStruct.Length <= 0)
{
ShowError("Ошибка файла.");
return;
}
saveFileDialog.FileName = fileName;
if (saveFileDialog.ShowDialog() == DialogResult.Cancel) return;
try { File.WriteAllBytes(saveFileDialog.FileName, fileStruct); }
catch { ShowError("Ошибка сохранения файла."); }
}
private void resetFileButton_Click(object sender, EventArgs e)
{
UpdateLink();
}
private void izvGridView_CurrentCellChanged(object sender, EventArgs e)
{
ClearBoxes();
if (izvGridView.SelectedRows.Count != 1)
return;
{
var izv = WorkDB.GetIzveschenie((int)izvGridView.SelectedRows[0].Cells[0].Value);
if (izv == null)
return;
idLabel.Text = izv.Id.ToString();
izvNumBox.Text = izv.IzvNum;
invNumBox.Text = izv.InvNum.ToString();
izmNumBox.Text = izv.IzmNum.ToString();
ukazZadBox.Text = izv.UkazZad;
ukazVnedrBox.Text = izv.UkazVnedr;
UpdateLink(izv.FileName, izv.FileStruct);
}
}
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)(() => { UpdateIzvTable(WorkDB.ListIzveschenie(searchBox.Text)); }));
else UpdateIzvTable(WorkDB.ListIzveschenie(searchBox.Text));
});
filterDrop.Start();
}
private void resetSearch_Click(object sender, EventArgs e)
{
searchBox.Text = "";
filterDrop = new Task(() => { return; });
UpdateIzvTable(WorkDB.ListIzveschenie(searchBox.Text));
}
private void createIzvButton_Click(object sender, EventArgs e)
{
if (!int.TryParse(invNumBox.Text, out int invNum)) { ShowError("Ошибка инвентарный №."); return; }
if (!int.TryParse(izmNumBox.Text, out int izmNum)) { ShowError("Ошибка извещение №."); return; }
try
{
var r = new Izveschenie()
{
IzvNum = izvNumBox.Text,
InvNum = invNum,
IzmNum = izmNum,
UkazZad = ukazZadBox.Text,
UkazVnedr = ukazVnedrBox.Text,
FileName = fileName,
FileStruct = fileStruct
};
WorkDB.AddIzveschenie(r);
UpdateIzvTable(WorkDB.ListIzveschenie(searchBox.Text));
}
catch { ShowError(); }
}
private void changeIzvButton_Click(object sender, EventArgs e)
{
if (!int.TryParse(idLabel.Text, out int idRes)) { ShowError("Извещение не выбрано."); return; }
var izv = WorkDB.GetIzveschenie(idRes);
if (izv == null) { ShowError("Нет извещения в БД."); return; }
if (!int.TryParse(invNumBox.Text, out int invNum)) { ShowError("Ошибка инвентарный №."); return; }
if (!int.TryParse(izmNumBox.Text, out int izmNum)) { ShowError("Ошибка извещение №."); return; }
try
{
izv.IzvNum = izvNumBox.Text;
izv.InvNum = invNum;
izv.IzmNum = izmNum;
izv.UkazZad = ukazZadBox.Text;
izv.UkazVnedr = ukazVnedrBox.Text;
izv.FileName = fileName;
izv.FileStruct = fileStruct;
WorkDB.ChangeIzveschenie(izv);
}
catch { ShowError(); }
UpdateIzvTable(WorkDB.ListIzveschenie(searchBox.Text));
}
private void deleteIzvButton_Click(object sender, EventArgs e)
{
if (!int.TryParse(idLabel.Text, out int idRes)) { ShowError("Извещение не выбрано."); return; }
var izv = WorkDB.GetIzveschenie(idRes);
if (izv == null) { ShowError("Извещения не существует."); return; }
try
{
WorkDB.DeleteIzdelie(izv);
}
catch { ShowError(); }
UpdateIzvTable(WorkDB.ListIzveschenie(searchBox.Text));
}
private void ResetIzvButton_Click(object sender, EventArgs e)
{
ClearBoxes();
}
}
}