fMain.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Data.SqlClient;
  6. using System.Drawing;
  7. using System.IO;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. using System.Windows.Forms;
  12. namespace SUBD
  13. {
  14. public partial class fMain : Form
  15. {
  16. public static string txtCon = @"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\BDPraktik.mdf;Integrated Security=True;Connect Timeout=30";
  17. int CountPerPage = 5;
  18. int NBegin = 5;
  19. public string idSelectedUser="";
  20. class ItemPanel : Panel
  21. {
  22. //public System.Windows.Forms.Panel panel1;
  23. public System.Windows.Forms.Label lRole;
  24. public System.Windows.Forms.Label lAdr;
  25. public System.Windows.Forms.Label lOtch;
  26. public System.Windows.Forms.Label lName;
  27. public System.Windows.Forms.Label lFam;
  28. public System.Windows.Forms.PictureBox pbImage;
  29. public System.Windows.Forms.Label lPol;
  30. public System.Windows.Forms.Label lid;
  31. public System.Windows.Forms.Label label1;
  32. public System.Windows.Forms.Label label2;
  33. public System.Windows.Forms.Label label3;
  34. public System.Windows.Forms.Label label4;
  35. public System.Windows.Forms.Label label5;
  36. public System.Windows.Forms.Label label6;
  37. public ItemPanel()
  38. {
  39. this.lid = new System.Windows.Forms.Label();
  40. this.lPol = new System.Windows.Forms.Label();
  41. this.lRole = new System.Windows.Forms.Label();
  42. this.lAdr = new System.Windows.Forms.Label();
  43. this.lOtch = new System.Windows.Forms.Label();
  44. this.lName = new System.Windows.Forms.Label();
  45. this.lFam = new System.Windows.Forms.Label();
  46. this.pbImage = new System.Windows.Forms.PictureBox();
  47. this.label1 = new System.Windows.Forms.Label();
  48. this.label2 = new System.Windows.Forms.Label();
  49. this.label3 = new System.Windows.Forms.Label();
  50. this.label4 = new System.Windows.Forms.Label();
  51. this.label5 = new System.Windows.Forms.Label();
  52. this.label6 = new System.Windows.Forms.Label();
  53. //
  54. // panel1
  55. //
  56. this.BackColor = System.Drawing.Color.White;
  57. this.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
  58. this.Controls.Add(this.label1);
  59. this.Controls.Add(this.label2);
  60. this.Controls.Add(this.label3);
  61. this.Controls.Add(this.label4);
  62. this.Controls.Add(this.label5);
  63. this.Controls.Add(this.label6);
  64. this.Controls.Add(this.lid);
  65. this.Controls.Add(this.lPol);
  66. this.Controls.Add(this.lRole);
  67. this.Controls.Add(this.lAdr);
  68. this.Controls.Add(this.lOtch);
  69. this.Controls.Add(this.lName);
  70. this.Controls.Add(this.lFam);
  71. this.Controls.Add(this.pbImage);
  72. this.Font = new System.Drawing.Font("Courier New", 9F);
  73. this.Location = new System.Drawing.Point(15, 178);
  74. this.Name = "panel1";
  75. this.Size = new System.Drawing.Size(790, 225);
  76. this.TabIndex = 0;
  77. //
  78. // lid
  79. //
  80. this.lid.AutoSize = true;
  81. this.lid.Location = new System.Drawing.Point(22, 188);
  82. this.lid.Name = "lid";
  83. this.lid.Size = new System.Drawing.Size(17, 17);
  84. this.lid.TabIndex = 7;
  85. this.lid.Text = "a";
  86. this.lid.Visible = false;
  87. //
  88. // lPol
  89. //
  90. this.lPol.AutoSize = true;
  91. this.lPol.Location = new System.Drawing.Point(360, 168);
  92. this.lPol.Name = "lPol";
  93. this.lPol.Size = new System.Drawing.Size(62, 17);
  94. this.lPol.TabIndex = 6;
  95. this.lPol.Text = "label1";
  96. //
  97. // lRole
  98. //
  99. this.lRole.AutoSize = true;
  100. this.lRole.Font = new System.Drawing.Font("Courier New", 9F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
  101. this.lRole.Location = new System.Drawing.Point(360, 140);
  102. this.lRole.Name = "lRole";
  103. this.lRole.Size = new System.Drawing.Size(62, 17);
  104. this.lRole.TabIndex = 5;
  105. this.lRole.Text = "label1";
  106. //
  107. // lAdr
  108. //
  109. this.lAdr.AutoSize = true;
  110. this.lAdr.Location = new System.Drawing.Point(360, 112);
  111. this.lAdr.Name = "lAdr";
  112. this.lAdr.Size = new System.Drawing.Size(62, 17);
  113. this.lAdr.TabIndex = 4;
  114. this.lAdr.Text = "label1";
  115. //
  116. // lOtch
  117. //
  118. this.lOtch.AutoSize = true;
  119. this.lOtch.Font = new System.Drawing.Font("Courier New", 9F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
  120. this.lOtch.Location = new System.Drawing.Point(360, 85);
  121. this.lOtch.Name = "lOtch";
  122. this.lOtch.Size = new System.Drawing.Size(62, 17);
  123. this.lOtch.TabIndex = 3;
  124. this.lOtch.Text = "label3";
  125. //
  126. // lName
  127. //
  128. this.lName.AutoSize = true;
  129. this.lName.Font = new System.Drawing.Font("Courier New", 9F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
  130. this.lName.Location = new System.Drawing.Point(360, 57);
  131. this.lName.Name = "lName";
  132. this.lName.Size = new System.Drawing.Size(62, 17);
  133. this.lName.TabIndex = 2;
  134. this.lName.Text = "label2";
  135. //
  136. // lFam
  137. //
  138. this.lFam.AutoSize = true;
  139. this.lFam.Font = new System.Drawing.Font("Courier New", 9F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
  140. this.lFam.Location = new System.Drawing.Point(360, 29);
  141. this.lFam.Name = "lFam";
  142. this.lFam.Size = new System.Drawing.Size(62, 17);
  143. this.lFam.TabIndex = 1;
  144. this.lFam.Text = "label1";
  145. //
  146. // pbImage
  147. //
  148. this.pbImage.Location = new System.Drawing.Point(25, 28);
  149. this.pbImage.Name = "pbImage";
  150. this.pbImage.Size = new System.Drawing.Size(169, 157);
  151. this.pbImage.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
  152. this.pbImage.TabIndex = 0;
  153. this.pbImage.TabStop = false;
  154. //
  155. // label1
  156. //
  157. this.label1.AutoSize = true;
  158. this.label1.Location = new System.Drawing.Point(200, 168);
  159. this.label1.Name = "label1";
  160. this.label1.Size = new System.Drawing.Size(44, 17);
  161. this.label1.TabIndex = 13;
  162. this.label1.Text = "Пол:";
  163. //
  164. // label2
  165. //
  166. this.label2.AutoSize = true;
  167. this.label2.Font = new System.Drawing.Font("Courier New", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
  168. this.label2.Location = new System.Drawing.Point(200, 140);
  169. this.label2.Name = "label2";
  170. this.label2.Size = new System.Drawing.Size(98, 17);
  171. this.label2.TabIndex = 12;
  172. this.label2.Text = "Должность:";
  173. //
  174. // label3
  175. //
  176. this.label3.AutoSize = true;
  177. this.label3.Location = new System.Drawing.Point(200, 112);
  178. this.label3.Name = "label3";
  179. this.label3.Size = new System.Drawing.Size(161, 17);
  180. this.label3.TabIndex = 11;
  181. this.label3.Text = "Место жительства:";
  182. //
  183. // label4
  184. //
  185. this.label4.AutoSize = true;
  186. this.label4.Font = new System.Drawing.Font("Courier New", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
  187. this.label4.Location = new System.Drawing.Point(200, 84);
  188. this.label4.Name = "label4";
  189. this.label4.Size = new System.Drawing.Size(89, 17);
  190. this.label4.TabIndex = 10;
  191. this.label4.Text = "Отчество:";
  192. //
  193. // label5
  194. //
  195. this.label5.AutoSize = true;
  196. this.label5.Font = new System.Drawing.Font("Courier New", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
  197. this.label5.Location = new System.Drawing.Point(200, 56);
  198. this.label5.Name = "label5";
  199. this.label5.Size = new System.Drawing.Size(44, 17);
  200. this.label5.TabIndex = 9;
  201. this.label5.Text = "Имя:";
  202. //
  203. // label6
  204. //
  205. this.label6.AutoSize = true;
  206. this.label6.Font = new System.Drawing.Font("Courier New", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
  207. this.label6.Location = new System.Drawing.Point(200, 28);
  208. this.label6.Name = "label6";
  209. this.label6.Size = new System.Drawing.Size(80, 17);
  210. this.label6.TabIndex = 8;
  211. this.label6.Text = "Фамилия:";
  212. }
  213. }
  214. public fMain()
  215. {
  216. InitializeComponent();
  217. }
  218. private void bAddUser_Click(object sender, EventArgs e)
  219. {
  220. fAddUser f = new fAddUser();
  221. f.ShowDialog();
  222. GetData();
  223. FillPanel();
  224. }
  225. private void bDelUser_Click(object sender, EventArgs e)
  226. {
  227. if (MessageBox.Show("вы действительно хотите удалить пользователя?", "Запрос на удаление", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
  228. {
  229. if (idSelectedUser != "")
  230. {
  231. SqlConnection con = new SqlConnection(txtCon);
  232. con.Open();
  233. string txt = $"delete from Teacher where IdTeacher={idSelectedUser}";
  234. SqlCommand com = new SqlCommand(txt, con);
  235. com.ExecuteNonQuery();
  236. con.Close();
  237. MessageBox.Show("Вы удалили пользователя", "Внимание");
  238. MessageBox.Show("Запрос: " + txt);
  239. GetData();
  240. FillPanel();
  241. idSelectedUser = "";
  242. }
  243. else
  244. {
  245. MessageBox.Show("Выберите пользователя");
  246. }
  247. }
  248. }
  249. struct Agent
  250. {
  251. public string idTeacher, famTeacher, NameTeacher, OtchTeacher, adr, pol, role;
  252. public Image img;
  253. }
  254. List<Agent> lstAgent = new List<Agent>();
  255. ItemPanel CurrentItem;
  256. string usl = "";
  257. void GetData()
  258. {
  259. usl = "";
  260. NBegin = 0;
  261. lstAgent.Clear();
  262. if (CmbGender.SelectedIndex == 0) usl += "";
  263. if (CmbGender.SelectedIndex == 1) usl += " and Pol=N'муж'";
  264. if (CmbGender.SelectedIndex == 2) usl += " and Pol=N'жен'";
  265. if (CmbFind.Text == "фамилии" && TbxFind.Text != "")
  266. usl += $" and FamTeacher Like N'{TbxFind.Text}%'";
  267. if (CmbFind.Text == "имени" && TbxFind.Text != "")
  268. usl += $" and NameTeacher Like N'{TbxFind.Text}%'";
  269. if (CmbFind.Text == "отчеству" && TbxFind.Text != "")
  270. usl += $" and OtchTeacher Like N'{TbxFind.Text}%'";
  271. SqlConnection con = new SqlConnection(txtCon);
  272. con.Open();
  273. string txt = $@"select IdTeacher, FamTeacher, NameTeacher, OtchTeacher, Adr, Pol, Photo, NameRole
  274. from Teacher, RoleTeacher
  275. where Teacher.Role=RoleTeacher.IdRole {usl}";
  276. SqlCommand com = new SqlCommand(txt, con);
  277. SqlDataReader rez = com.ExecuteReader();
  278. while (rez.Read())
  279. {
  280. Agent agent1 = new Agent();
  281. agent1.idTeacher = rez["IdTeacher"].ToString();
  282. agent1.famTeacher = rez["FamTeacher"].ToString();
  283. agent1.NameTeacher = rez["NameTeacher"].ToString();
  284. agent1.OtchTeacher = rez["OtchTeacher"].ToString();
  285. agent1.adr = rez["Adr"].ToString();
  286. agent1.pol = rez["Pol"].ToString();
  287. agent1.role = rez["NameRole"].ToString();
  288. try
  289. {
  290. byte[] masphoto = (byte[])rez["Photo"];
  291. MemoryStream msPhoto = new MemoryStream(masphoto);
  292. agent1.img = Image.FromStream(msPhoto);
  293. }
  294. catch
  295. {
  296. agent1.img = Properties.Resources.imgPng;
  297. }
  298. lstAgent.Add(agent1);
  299. }
  300. con.Close();
  301. }
  302. void FillPanel()
  303. {
  304. MainPanel.Controls.Clear();
  305. //foreach (Agent agent1 in lstAgent)
  306. //{
  307. // ItemPanel item = new ItemPanel();
  308. // item.lFam.Text = agent1.famTeacher;
  309. // item.lid.Text = agent1.idTeacher;
  310. // item.lName.Text = agent1.NameTeacher;
  311. // item.lOtch.Text = agent1.OtchTeacher;
  312. // item.lPol.Text = agent1.pol;
  313. // item.lRole.Text = agent1.role;
  314. // item.pbImage.Image = agent1.img;
  315. // item.lAdr.Text = agent1.adr;
  316. // MainPanel.Controls.Add(item);
  317. //}
  318. for (int i = NBegin; i <= NBegin + CountPerPage - 1; i++)
  319. {
  320. if (i > lstAgent.Count - 1)
  321. break;
  322. ItemPanel item = new ItemPanel();
  323. item.lFam.Text = lstAgent[i].famTeacher;
  324. item.lid.Text = lstAgent[i].idTeacher;
  325. item.lName.Text = lstAgent[i].NameTeacher;
  326. item.lOtch.Text = lstAgent[i].OtchTeacher;
  327. item.lPol.Text = lstAgent[i].pol;
  328. item.lRole.Text = lstAgent[i].role;
  329. try
  330. {
  331. item.pbImage.Image = lstAgent[i].img;
  332. }
  333. catch
  334. {
  335. item.pbImage.Image = Properties.Resources.imgPng;
  336. }
  337. item.lAdr.Text = lstAgent[i].adr;
  338. MainPanel.Controls.Add(item);
  339. item.Click += Item_Click;
  340. item.lAdr.Click += Elem_Click;
  341. item.lFam.Click += Elem_Click;
  342. item.lid.Click += Elem_Click;
  343. item.lName.Click += Elem_Click;
  344. item.lOtch.Click += Elem_Click;
  345. item.lPol.Click += Elem_Click;
  346. item.lRole.Click += Elem_Click;
  347. item.pbImage.Click += Elem_Click;
  348. if (MainPanel.Controls.Count == 1)
  349. {
  350. CurrentItem = item;
  351. CurrentItem.BackColor = Color.LightGreen;
  352. }
  353. }
  354. if (CountPerPage == 0)
  355. CountPerPage = 1;
  356. int Cnt = lstAgent.Count / CountPerPage;
  357. if (lstAgent.Count % CountPerPage != 0)
  358. Cnt++;
  359. PanelPages.Controls.Clear();
  360. PanelPages.Controls.Add(lNext);
  361. lback.Dock = DockStyle.Left;
  362. lNext.Dock = DockStyle.Left;
  363. for (int i = Cnt; i >= 1; i--)
  364. {
  365. Label lbl = new Label();
  366. lbl.Text = i.ToString();
  367. lbl.Dock = DockStyle.Left;
  368. lbl.AutoSize = true;
  369. if ((NBegin + CountPerPage) / CountPerPage == i)
  370. {
  371. lbl.BackColor = Color.LightGray;
  372. lbl.Font = new Font(lbl.Font, FontStyle.Underline);
  373. }
  374. PanelPages.Controls.Add(lbl);
  375. lbl.Click += Lbl_Click;
  376. }
  377. PanelPages.Controls.Add(lback);
  378. }
  379. private void Elem_Click(object sender, EventArgs e)
  380. {
  381. CurrentItem.BackColor = Color.White;
  382. CurrentItem = (sender as Control).Parent as ItemPanel;
  383. CurrentItem.BackColor = Color.LightGreen;
  384. idSelectedUser = CurrentItem.lid.Text;
  385. }
  386. private void Item_Click(object sender, EventArgs e)
  387. {
  388. CurrentItem.BackColor = Color.White;
  389. CurrentItem = sender as ItemPanel;
  390. CurrentItem.BackColor = Color.LightGreen;
  391. idSelectedUser = CurrentItem.lid.Text;
  392. }
  393. private void Lbl_Click(object sender, EventArgs e)
  394. {
  395. int n = int.Parse((sender as Label).Text);
  396. NBegin = (n - 1) * CountPerPage;
  397. FillPanel();
  398. }
  399. private void fMain_Load(object sender, EventArgs e)
  400. {
  401. this.Icon = Properties.Resources.imgIco;
  402. pbPhoto.Image = Properties.Resources.imgPng;
  403. GetData();
  404. FillPanel();
  405. CmbGender.SelectedIndex = 0;
  406. CmbFind.SelectedIndex = 0;
  407. CmbCountLines.SelectedIndex = 0;
  408. }
  409. private void lback_Click(object sender, EventArgs e)
  410. {
  411. if (NBegin > 0)
  412. {
  413. NBegin -= CountPerPage;
  414. FillPanel();
  415. }
  416. }
  417. private void lNext_Click(object sender, EventArgs e)
  418. {
  419. if (NBegin + CountPerPage < lstAgent.Count)
  420. {
  421. NBegin += CountPerPage;
  422. FillPanel();
  423. }
  424. }
  425. private void CmbCountLines_SelectedIndexChanged(object sender, EventArgs e)
  426. {
  427. try
  428. {
  429. int s = int.Parse(CmbCountLines.Text);
  430. CountPerPage = s;
  431. }
  432. catch
  433. {
  434. CountPerPage = lstAgent.Count;
  435. }
  436. NBegin = 0;
  437. FillPanel();
  438. }
  439. private void CmbGender_SelectedIndexChanged(object sender, EventArgs e)
  440. {
  441. GetData();
  442. FillPanel();
  443. }
  444. private void TbxFind_TextChanged(object sender, EventArgs e)
  445. {
  446. GetData();
  447. FillPanel();
  448. }
  449. private void CmbFind_SelectedIndexChanged(object sender, EventArgs e)
  450. {
  451. GetData();
  452. FillPanel();
  453. }
  454. }
  455. }