Form1.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using System.Windows.Forms;
  11. namespace Пример_плиточного_интерфейса
  12. {
  13. public partial class Form1 : Form
  14. {
  15. public Form1()
  16. {
  17. InitializeComponent();
  18. }
  19. class Furnitura : Panel
  20. {
  21. public System.Windows.Forms.Label LblCount;
  22. public System.Windows.Forms.Label LblName;
  23. public System.Windows.Forms.PictureBox PbxPhoto;
  24. public string ID; // ID фурнитуры
  25. public Furnitura()
  26. {
  27. this.PbxPhoto = new System.Windows.Forms.PictureBox();
  28. this.LblName = new System.Windows.Forms.Label();
  29. this.LblCount = new System.Windows.Forms.Label();
  30. //
  31. // panel1
  32. //
  33. this.BackColor = System.Drawing.Color.White;
  34. this.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
  35. this.Controls.Add(this.LblCount);
  36. this.Controls.Add(this.LblName);
  37. this.Controls.Add(this.PbxPhoto);
  38. this.Location = new System.Drawing.Point(38, 57);
  39. this.Name = "panel1";
  40. this.Size = new System.Drawing.Size(171, 153);
  41. this.TabIndex = 0;
  42. //
  43. // PbxPhoto
  44. //
  45. this.PbxPhoto.Location = new System.Drawing.Point(29, 17);
  46. this.PbxPhoto.Name = "PbxPhoto";
  47. this.PbxPhoto.Size = new System.Drawing.Size(109, 76);
  48. this.PbxPhoto.SizeMode = System.Windows.Forms.PictureBoxSizeMode.Zoom;
  49. this.PbxPhoto.TabIndex = 0;
  50. this.PbxPhoto.TabStop = false;
  51. //
  52. // LblName
  53. //
  54. this.LblName.AutoSize = true;
  55. this.LblName.Location = new System.Drawing.Point(14, 109);
  56. this.LblName.Name = "LblName";
  57. this.LblName.Size = new System.Drawing.Size(35, 13);
  58. this.LblName.TabIndex = 1;
  59. this.LblName.Text = "label1";
  60. //
  61. // LblCount
  62. //
  63. this.LblCount.AutoSize = true;
  64. this.LblCount.Location = new System.Drawing.Point(14, 131);
  65. this.LblCount.Name = "LblCount";
  66. this.LblCount.Size = new System.Drawing.Size(35, 13);
  67. this.LblCount.TabIndex = 2;
  68. this.LblCount.Text = "label2";
  69. }
  70. }
  71. string TxtCon = @"Data Source=213.155.192.79,3002;Initial Catalog=fab2022;Persist Security Info=True;User ID=mil;Password=mil";
  72. Furnitura CurrentFur;
  73. void FillPanel()
  74. {
  75. SqlConnection Con = new SqlConnection(TxtCon);
  76. Con.Open();
  77. SqlCommand Query = new SqlCommand("select * from furnitura", Con);
  78. SqlDataReader Res = Query.ExecuteReader();
  79. flowLayoutPanel1.Controls.Clear();
  80. while(Res.Read())
  81. {
  82. Furnitura ItemFur = new Furnitura();
  83. ItemFur.LblName.Text = "Название: " + Res["namefur"];
  84. ItemFur.LblCount.Text = "Кол-во на складе: " + Res["countfur"];
  85. try
  86. {
  87. ItemFur.PbxPhoto.Image = Image.FromFile(Application.StartupPath + "\\Фурнитуры\\" + Res["photo"]);
  88. }
  89. catch
  90. {
  91. ItemFur.PbxPhoto.Image = Image.FromFile(Application.StartupPath + "\\Фурнитуры\\picture.png");
  92. }
  93. ItemFur.ID = Res["idfur"].ToString();
  94. ItemFur.Click += ItemFur_Click;
  95. ItemFur.PbxPhoto.Click += Object_Click;
  96. ItemFur.LblName.Click += Object_Click;
  97. ItemFur.LblCount.Click += Object_Click;
  98. flowLayoutPanel1.Controls.Add(ItemFur);
  99. // если плитка самая первая, то она становится выделенной
  100. if (flowLayoutPanel1.Controls.Count == 1)
  101. {
  102. ItemFur.BackColor = Color.LightGreen;
  103. CurrentFur = ItemFur;
  104. }
  105. }
  106. Con.Close();
  107. }
  108. private void Object_Click(object sender, EventArgs e)
  109. {
  110. CurrentFur.BackColor = Color.White;
  111. CurrentFur = (sender as Control).Parent as Furnitura;
  112. CurrentFur.BackColor = Color.LightGreen;
  113. }
  114. private void ItemFur_Click(object sender, EventArgs e)
  115. {
  116. CurrentFur.BackColor = Color.White;
  117. CurrentFur = sender as Furnitura;
  118. CurrentFur.BackColor = Color.LightGreen;
  119. }
  120. private void Form1_Load(object sender, EventArgs e)
  121. {
  122. FillPanel();
  123. }
  124. private void button1_Click(object sender, EventArgs e)
  125. {
  126. MessageBox.Show("ID выбранной фурнитуры: " + CurrentFur.ID);
  127. }
  128. }
  129. }