Diplom_B/SetForm.cs

272 lines
9.9 KiB
C#
Raw Permalink Normal View History

2021-07-17 09:33:09 +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;
2021-07-17 22:32:53 +05:00
using Diplom_B.DB;
2021-07-17 09:33:09 +05:00
namespace Diplom_B
{
public partial class SetForm : Form
{
public SetForm()
{
InitializeComponent();
2021-07-17 22:32:53 +05:00
Init();
defaultBox.Items.Clear();
defaultBox.Items.AddRange(Program.user.FormsName);
UpdateTable(WorkDB.ListUser(searchBox.Text));
2021-07-17 09:33:09 +05:00
}
2021-07-17 22:32:53 +05:00
private void Init()
{
if (Program.user == null) this.Close();
{
mainMenuStrip.Items[0].Enabled = Program.user.Usr.Dog > 0;
mainMenuStrip.Items[1].Enabled = Program.user.Usr.Doc > 0;
mainMenuStrip.Items[2].Enabled = Program.user.Usr.Izv > 0;
mainMenuStrip.Items[3].Enabled = Program.user.Usr.Post > 0;
mainMenuStrip.Items[4].Enabled = Program.user.Usr.Izd > 0;
mainMenuStrip.Items[5].Enabled = Program.user.Usr.Zak > 0;
mainMenuStrip.Items[6].Enabled = Program.user.Usr.Set > 0;
mainMenuStrip.Items[6].Enabled = false;
}
{
deleteButton.Enabled = Program.user.Usr.Set > 2;
createButton.Enabled = Program.user.Usr.Set > 2;
changeButton.Enabled = Program.user.Usr.Set > 1;
}
}
private void ClearBoxes()
{
idLable.Text = "";
nameBox.Text = "";
passBox.Text = "";
defaultBox.SelectedIndex = 0;
chkDog1.Checked = chkDog2.Checked = chkDog3.Checked = false;
chkDoc1.Checked = chkDoc2.Checked = chkDoc3.Checked = false;
chkIzv1.Checked = chkIzv2.Checked = chkIzv3.Checked = false;
chkPost1.Checked = chkPost2.Checked = chkPost3.Checked = false;
chkIzd1.Checked = chkIzd2.Checked = chkIzd3.Checked = false;
chkZak1.Checked = chkZak2.Checked = chkZak3.Checked = false;
chkSet1.Checked = chkSet2.Checked = chkSet3.Checked = false;
}
private void clearButton_Click(object sender, EventArgs e)
{
ClearBoxes();
}
private void UpdateTable(DB.User[] arr, bool reset_cursor = false)
{
var selected = (!reset_cursor && usrGridView.SelectedRows.Count > 0) ? usrGridView.SelectedRows[0].Index : -1;
{
var r = usrGridView.Rows;
while (r.Count > 0)
r.Remove(r[0]);
var c = usrGridView.Columns;
while (c.Count > 0)
c.Remove(c[0]);
}
{
var c = usrGridView.Columns;
c.Add("Id", "№");
c["Id"].Width = 40;
c.Add("Name", "Наим.");
c["Name"].Width = 60;
c.Add("DefaultForm", "Окно");
c["DefaultForm"].Width = 100;
}
{
var r = usrGridView.Rows;
foreach (var usr in arr)
r.Add(new object[] {
usr.Id,
usr.Name,
Program.user.FormsName[usr.Default]
});
}
if (usrGridView.Rows.Count > 0)
usrGridView.Rows[0].Selected = true;
if (selected != -1 && selected < usrGridView.Rows.Count)
for (var i = 0; i < usrGridView.Rows.Count; i++)
usrGridView.Rows[i].Selected = (i == selected);
usrGridView_CurrentCellChanged(this, new EventArgs());
}
private Task errDrop;
private void ShowError(string msg = null)
{
errorLable.Text = string.IsNullOrEmpty(msg) ? "Неизвестная ошибка." : msg;
errorLable.Visible = true;
errDrop = new Task(() =>
{
var fd = errDrop.Id;
Task.Delay(5000).Wait();
if (errDrop.Id == fd)
if (InvokeRequired) Invoke((Action)(() => { errorLable.Visible = false; }));
else errorLable.Visible = false;
});
errDrop.Start();
}
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)(() => { UpdateTable(WorkDB.ListUser(searchBox.Text)); }));
else UpdateTable(WorkDB. ListUser(searchBox.Text));
});
filterDrop.Start();
}
private void usrGridView_CurrentCellChanged(object sender, EventArgs e)
{
ClearBoxes();
if (usrGridView.SelectedRows.Count != 1)
return;
{
var usr = WorkDB.GetUser((int)usrGridView.SelectedRows[0].Cells[0].Value);
if (usr == null)
return;
idLable.Text = usr.Id.ToString();
nameBox.Text = usr.Name;
passBox.Text = usr.Pass;
defaultBox.SelectedIndex = usr.Default;
chkDog1.Checked = usr.Dog > 0; chkDog2.Checked = usr.Dog > 1; chkDog3.Checked = usr.Dog > 2;
chkDoc1.Checked = usr.Doc > 0; chkDoc2.Checked = usr.Doc > 1; chkDoc3.Checked = usr.Doc > 2;
chkIzv1.Checked = usr.Izv > 0; chkIzv2.Checked = usr.Izv > 1; chkIzv3.Checked = usr.Izv > 2;
chkPost1.Checked = usr.Post > 0; chkPost2.Checked = usr.Post > 1; chkPost3.Checked = usr.Post > 2;
chkIzd1.Checked = usr.Izd > 0; chkIzd2.Checked = usr.Izd > 1; chkIzd3.Checked = usr.Izd > 2;
chkZak1.Checked = usr.Zak > 0; chkZak2.Checked = usr.Zak > 1; chkZak3.Checked = usr.Zak > 2;
chkSet1.Checked = usr.Set > 0; chkSet2.Checked = usr.Set > 1; chkSet3.Checked = usr.Set > 2;
}
}
private void resetSearchButton_Click(object sender, EventArgs e)
{
searchBox.Text = "";
filterDrop = new Task(() => { return; });
UpdateTable(WorkDB.ListUser(searchBox.Text));
}
private void createButton_Click(object sender, EventArgs e)
{
if(WorkDB.GetUserList().Contains(nameBox.Text)) { ShowError("Пользователь существует."); return; }
if(string.IsNullOrEmpty(passBox.Text)) { ShowError("Пароль пустой."); return; }
try
{
var r = new DB.User()
{
Name = nameBox.Text,
Pass = passBox.Text,
Dog = (chkDog3.Checked) ? 3 : (chkDog2.Checked) ? 2 : (chkDog1.Checked) ? 1 : 0,
Doc = (chkDoc3.Checked) ? 3 : (chkDoc2.Checked) ? 2 : (chkDoc1.Checked) ? 1 : 0,
Izv = (chkIzv3.Checked) ? 3 : (chkIzv2.Checked) ? 2 : (chkIzv1.Checked) ? 1 : 0,
Post = (chkPost3.Checked) ? 3 : (chkPost2.Checked) ? 2 : (chkPost1.Checked) ? 1 : 0,
Izd = (chkIzd3.Checked) ? 3 : (chkIzd2.Checked) ? 2 : (chkIzd1.Checked) ? 1 : 0,
Zak = (chkZak3.Checked) ? 3 : (chkZak2.Checked) ? 2 : (chkZak1.Checked) ? 1 : 0,
Set = (chkSet3.Checked) ? 3 : (chkSet2.Checked) ? 2 : (chkSet1.Checked) ? 1 : 0,
};
var flag = false;
switch (defaultBox.SelectedIndex)
{
case 0: flag = r.Dog > 0; break;
case 1: flag = r.Doc > 0; break;
case 2: flag = r.Izv > 0; break;
case 3: flag = r.Post > 0; break;
case 4: flag = r.Izd > 0; break;
case 5: flag = r.Zak > 0; break;
case 6: flag = r.Set > 0; break;
}
if (!flag) { ShowError("Форма недоступна."); return; }
r.Default = defaultBox.SelectedIndex;
WorkDB.AddUser(r);
UpdateTable(WorkDB.ListUser(searchBox.Text));
}
catch { ShowError(); }
}
private void changeButton_Click(object sender, EventArgs e)
{
try
{
if (!int.TryParse(idLable.Text, out int idRes)) { ShowError("Польз. не выбран."); return; }
var usr = WorkDB.GetUser(idRes);
if (usr == null) { ShowError("Нет польз. в БД."); return; }
if (string.IsNullOrEmpty(passBox.Text)) { ShowError("Пароль пустой."); return; }
if (usr.Name != nameBox.Text && WorkDB.GetUserList().Contains(nameBox.Text)) { ShowError("Польз. есть в БД."); return; }
usr.Name = (usr.Name == "admin") ? "admin" : nameBox.Text;
usr.Pass = passBox.Text;
usr.Dog = (chkDog3.Checked || usr.Name == "admin") ? 3 : (chkDog2.Checked) ? 2 : (chkDog1.Checked) ? 1 : 0;
usr.Doc = (chkDoc3.Checked || usr.Name == "admin") ? 3 : (chkDoc2.Checked) ? 2 : (chkDoc1.Checked) ? 1 : 0;
usr.Izv = (chkIzv3.Checked || usr.Name == "admin") ? 3 : (chkIzv2.Checked) ? 2 : (chkIzv1.Checked) ? 1 : 0;
usr.Post = (chkPost3.Checked || usr.Name == "admin") ? 3 : (chkPost2.Checked) ? 2 : (chkPost1.Checked) ? 1 : 0;
usr.Izd = (chkIzd3.Checked || usr.Name == "admin") ? 3 : (chkIzd2.Checked) ? 2 : (chkIzd1.Checked) ? 1 : 0;
usr.Zak = (chkZak3.Checked || usr.Name == "admin") ? 3 : (chkZak2.Checked) ? 2 : (chkZak1.Checked) ? 1 : 0;
usr.Set = (chkSet3.Checked || usr.Name == "admin") ? 3 : (chkSet2.Checked) ? 2 : (chkSet1.Checked) ? 1 : 0;
var flag = false;
switch (defaultBox.SelectedIndex)
{
case 0: flag = usr.Dog > 0; break;
case 1: flag = usr.Doc > 0; break;
case 2: flag = usr.Izv > 0; break;
case 3: flag = usr.Post > 0; break;
case 4: flag = usr.Izd > 0; break;
case 5: flag = usr.Zak > 0; break;
case 6: flag = usr.Set > 0; break;
}
if (!flag) { ShowError("Форма недоступна."); return; }
usr.Default = defaultBox.SelectedIndex;
WorkDB.ChangeUser(usr);
UpdateTable(WorkDB.ListUser(searchBox.Text));
}
catch { ShowError(); }
}
private void deleteButton_Click(object sender, EventArgs e)
{
if (!int.TryParse(idLable.Text, out int idRes)) { ShowError("Польз. не выбран."); return; }
var usr = WorkDB.GetUser(idRes);
if (usr == null) { ShowError("Польз. не существует."); return; }
if (usr.Name == "admin") { ShowError("Нельзя улвить польз."); return; }
try
{
WorkDB.DeleteUser(usr);
}
catch { ShowError(); }
UpdateTable(WorkDB.ListUser(searchBox.Text));
}
private void MenuItem_Click(object sender, EventArgs e)
{
object form = null;
if ((ToolStripMenuItem)sender == mainMenuStrip.Items[0]) { form = new DogForm(); }
if ((ToolStripMenuItem)sender == mainMenuStrip.Items[1]) { form = new DocForm(); }
if ((ToolStripMenuItem)sender == mainMenuStrip.Items[2]) { form = new IzvForm(); }
if ((ToolStripMenuItem)sender == mainMenuStrip.Items[3]) { form = new PostForm(); }
if ((ToolStripMenuItem)sender == mainMenuStrip.Items[4]) { form = new IzdForm(); }
if ((ToolStripMenuItem)sender == mainMenuStrip.Items[5]) { form = new ZakForm(); }
if ((ToolStripMenuItem)sender == mainMenuStrip.Items[6]) { form = new SetForm(); }
if (form != null)
{
this.Hide();
((Form)form).Closed += (s, args) => this.Close();
((Form)form).Show();
}
}
private void StatusButton_Click(object sender, EventArgs e)
{
var form = new StatForm();
form.ShowDialog();
}
}
2021-07-17 09:33:09 +05:00
}