Guest User

Form 5 - Настроойки

a guest
Dec 11th, 2018
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.44 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Data.Linq;
  9. using System.Threading.Tasks;
  10. using System.Windows.Forms;
  11.  
  12. namespace laba8
  13. {
  14. public partial class Form5 : Form
  15. {
  16. Table<User> users;
  17. Table<Priv> privs;
  18. Table<UserPriv> userPrivs;
  19. DataContext context;
  20. User user;
  21. public Form5()
  22. {
  23. InitializeComponent();
  24.  
  25. context = appcontext.GetDataContext();
  26. users = context.GetTable<User>();
  27. privs = context.GetTable<Priv>();
  28. userPrivs = context.GetTable<UserPriv>();
  29.  
  30. ListBox1Update();
  31.  
  32. for (int i = 0; i < privs.Count(); i++)
  33. {
  34. listBox3.Items.Add(privs.Skip(i).First().Name);
  35. }
  36.  
  37.  
  38. }
  39. private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
  40. {
  41. var index = listBox1.SelectedIndex;
  42. if (index >= 0)
  43. {
  44. user = users.Skip(index).First();
  45. var curUserPrivs = userPrivs.Where(v => v.UserID == user.UserID);
  46. listBox2.Items.Clear();
  47. for (int i = 0; i < curUserPrivs.Count(); i++)
  48. {
  49. listBox2.Items.Add(privs.First(v => v.PrivID == curUserPrivs.Skip(i).First().PrivID).Name);
  50. }
  51. textBox1.Text = user.UserName.Trim(' ');
  52. textBox2.Text = user.Password.Trim(' ');
  53. textBox3.Text = user.FullName.Trim(' ');
  54. panel1.Visible = true;
  55. }
  56. }
  57. private void button1_Click(object sender, EventArgs e)
  58. {
  59. var index = listBox1.SelectedIndex;
  60. var user = new User { FullName = "New", UserName = "User", Password = "1", UserID = users.Select(v => v.UserID).Max() + 1 };
  61. users.InsertOnSubmit(user);
  62. context.SubmitChanges();
  63. ListBox1Update();
  64. listBox1.SelectedIndex = index;
  65.  
  66.  
  67. }
  68.  
  69. private void button2_Click(object sender, EventArgs e)
  70. {
  71.  
  72. }
  73.  
  74. private void button3_Click(object sender, EventArgs e)
  75. {
  76.  
  77. }
  78.  
  79. private void button4_Click(object sender, EventArgs e)
  80. {
  81. var index = listBox1.SelectedIndex;
  82. var user = new User { FullName = "Новый пользователь", UserName = "User", Password = "1", UserID = users.Select(v => v.UserID).Max() + 1 };
  83. users.InsertOnSubmit(user);
  84. context.SubmitChanges();
  85. ListBox1Update();
  86. listBox1.SelectedIndex = index;
  87. }
  88.  
  89. private void ListBox1Update()
  90. {
  91. listBox1.Items.Clear();
  92. for (int i = 0; i < users.Count(); i++)
  93. {
  94. listBox1.Items.Add(users.Skip(i).First().FullName);
  95. }
  96. }
  97.  
  98. private void listBox1_SelectedIndexChanged_1(object sender, EventArgs e)
  99. {
  100. var index = listBox1.SelectedIndex;
  101. if (index >= 0)
  102. {
  103. user = users.Skip(index).First();
  104. var curUserPrivs = userPrivs.Where(v => v.UserID == user.UserID);
  105. listBox2.Items.Clear();
  106. for (int i = 0; i < curUserPrivs.Count(); i++)
  107. {
  108. listBox2.Items.Add(privs.First(v => v.PrivID == curUserPrivs.Skip(i).First().PrivID).Name);
  109. }
  110. textBox1.Text = user.UserName.Trim(' ');
  111. textBox2.Text = user.Password.Trim(' ');
  112. textBox3.Text = user.FullName.Trim(' ');
  113. panel1.Visible = true;
  114. }
  115. }
  116.  
  117. private void button5_Click(object sender, EventArgs e)
  118. {
  119. var index = listBox1.SelectedIndex;
  120. user.UserName = textBox1.Text;
  121. user.Password = textBox2.Text;
  122. user.FullName = textBox3.Text;
  123. context.SubmitChanges();
  124. ListBox1Update();
  125. listBox1.SelectedIndex = index;
  126. }
  127.  
  128. private void button3_Click_1(object sender, EventArgs e)
  129. {
  130. var index = listBox3.SelectedIndex;
  131. if (index >= 0)
  132. {
  133. var priv = privs.Skip(index).First();
  134.  
  135. if (userPrivs.FirstOrDefault(v => v.PrivID == priv.PrivID && v.UserID == user.UserID) == null)
  136. {
  137. var userPriv = new UserPriv { PrivID = priv.PrivID, UserID = user.UserID, Id = userPrivs.Select(v => v.Id).Max() + 1 };
  138. userPrivs.InsertOnSubmit(userPriv);
  139. listBox2.Items.Add(priv.Name);
  140. context.SubmitChanges();
  141. }
  142. }
  143. }
  144.  
  145. private void button2_Click_1(object sender, EventArgs e)
  146. {
  147. var index = listBox1.SelectedIndex;
  148. if (index >= 0)
  149. {
  150. var user = users.Skip(index).First();
  151. if (user.UserID != Program.user.UserID)
  152. {
  153. var prvs = userPrivs.Where(v => v.UserID == user.UserID).ToList();
  154. userPrivs.DeleteAllOnSubmit(prvs);
  155. users.DeleteOnSubmit(user);
  156. context.SubmitChanges();
  157.  
  158. ListBox1Update();
  159. panel1.Visible = false;
  160. }
  161. else
  162. {
  163. MessageBox.Show("Невозможно удалить себя!");
  164. }
  165. }
  166. }
  167.  
  168. private void button4_Click_1(object sender, EventArgs e)
  169. {
  170. var index = listBox2.SelectedIndex;
  171. if (index >= 0)
  172. {
  173. if (user.UserID != Program.user.UserID)
  174. {
  175. var userPriv = userPrivs.Where(v => v.UserID == user.UserID).Skip(index).First();
  176. userPrivs.DeleteOnSubmit(userPriv);
  177. context.SubmitChanges();
  178. listBox2.Items.RemoveAt(index);
  179. }
  180. else
  181. {
  182. MessageBox.Show("Невозможно лишить себя прав!");
  183. }
  184. }
  185. }
  186. }
  187. }
Add Comment
Please, Sign In to add comment