Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Data.Linq;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- namespace laba8
- {
- public partial class Form5 : Form
- {
- Table<User> users;
- Table<Priv> privs;
- Table<UserPriv> userPrivs;
- DataContext context;
- User user;
- public Form5()
- {
- InitializeComponent();
- context = appcontext.GetDataContext();
- users = context.GetTable<User>();
- privs = context.GetTable<Priv>();
- userPrivs = context.GetTable<UserPriv>();
- ListBox1Update();
- for (int i = 0; i < privs.Count(); i++)
- {
- listBox3.Items.Add(privs.Skip(i).First().Name);
- }
- }
- private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
- {
- var index = listBox1.SelectedIndex;
- if (index >= 0)
- {
- user = users.Skip(index).First();
- var curUserPrivs = userPrivs.Where(v => v.UserID == user.UserID);
- listBox2.Items.Clear();
- for (int i = 0; i < curUserPrivs.Count(); i++)
- {
- listBox2.Items.Add(privs.First(v => v.PrivID == curUserPrivs.Skip(i).First().PrivID).Name);
- }
- textBox1.Text = user.UserName.Trim(' ');
- textBox2.Text = user.Password.Trim(' ');
- textBox3.Text = user.FullName.Trim(' ');
- panel1.Visible = true;
- }
- }
- private void button1_Click(object sender, EventArgs e)
- {
- var index = listBox1.SelectedIndex;
- var user = new User { FullName = "New", UserName = "User", Password = "1", UserID = users.Select(v => v.UserID).Max() + 1 };
- users.InsertOnSubmit(user);
- context.SubmitChanges();
- ListBox1Update();
- listBox1.SelectedIndex = index;
- }
- private void button2_Click(object sender, EventArgs e)
- {
- }
- private void button3_Click(object sender, EventArgs e)
- {
- }
- private void button4_Click(object sender, EventArgs e)
- {
- var index = listBox1.SelectedIndex;
- var user = new User { FullName = "Новый пользователь", UserName = "User", Password = "1", UserID = users.Select(v => v.UserID).Max() + 1 };
- users.InsertOnSubmit(user);
- context.SubmitChanges();
- ListBox1Update();
- listBox1.SelectedIndex = index;
- }
- private void ListBox1Update()
- {
- listBox1.Items.Clear();
- for (int i = 0; i < users.Count(); i++)
- {
- listBox1.Items.Add(users.Skip(i).First().FullName);
- }
- }
- private void listBox1_SelectedIndexChanged_1(object sender, EventArgs e)
- {
- var index = listBox1.SelectedIndex;
- if (index >= 0)
- {
- user = users.Skip(index).First();
- var curUserPrivs = userPrivs.Where(v => v.UserID == user.UserID);
- listBox2.Items.Clear();
- for (int i = 0; i < curUserPrivs.Count(); i++)
- {
- listBox2.Items.Add(privs.First(v => v.PrivID == curUserPrivs.Skip(i).First().PrivID).Name);
- }
- textBox1.Text = user.UserName.Trim(' ');
- textBox2.Text = user.Password.Trim(' ');
- textBox3.Text = user.FullName.Trim(' ');
- panel1.Visible = true;
- }
- }
- private void button5_Click(object sender, EventArgs e)
- {
- var index = listBox1.SelectedIndex;
- user.UserName = textBox1.Text;
- user.Password = textBox2.Text;
- user.FullName = textBox3.Text;
- context.SubmitChanges();
- ListBox1Update();
- listBox1.SelectedIndex = index;
- }
- private void button3_Click_1(object sender, EventArgs e)
- {
- var index = listBox3.SelectedIndex;
- if (index >= 0)
- {
- var priv = privs.Skip(index).First();
- if (userPrivs.FirstOrDefault(v => v.PrivID == priv.PrivID && v.UserID == user.UserID) == null)
- {
- var userPriv = new UserPriv { PrivID = priv.PrivID, UserID = user.UserID, Id = userPrivs.Select(v => v.Id).Max() + 1 };
- userPrivs.InsertOnSubmit(userPriv);
- listBox2.Items.Add(priv.Name);
- context.SubmitChanges();
- }
- }
- }
- private void button2_Click_1(object sender, EventArgs e)
- {
- var index = listBox1.SelectedIndex;
- if (index >= 0)
- {
- var user = users.Skip(index).First();
- if (user.UserID != Program.user.UserID)
- {
- var prvs = userPrivs.Where(v => v.UserID == user.UserID).ToList();
- userPrivs.DeleteAllOnSubmit(prvs);
- users.DeleteOnSubmit(user);
- context.SubmitChanges();
- ListBox1Update();
- panel1.Visible = false;
- }
- else
- {
- MessageBox.Show("Невозможно удалить себя!");
- }
- }
- }
- private void button4_Click_1(object sender, EventArgs e)
- {
- var index = listBox2.SelectedIndex;
- if (index >= 0)
- {
- if (user.UserID != Program.user.UserID)
- {
- var userPriv = userPrivs.Where(v => v.UserID == user.UserID).Skip(index).First();
- userPrivs.DeleteOnSubmit(userPriv);
- context.SubmitChanges();
- listBox2.Items.RemoveAt(index);
- }
- else
- {
- MessageBox.Show("Невозможно лишить себя прав!");
- }
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment