Form1.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. class Furnitura : Panel
  16. {
  17. public System.Windows.Forms.Label LblCount;
  18. public System.Windows.Forms.Label LblName;
  19. public System.Windows.Forms.PictureBox PbxPhoto;
  20. public string ID; // ID фурнитуры
  21. public Furnitura()
  22. {
  23. this.PbxPhoto = new System.Windows.Forms.PictureBox();
  24. this.LblName = new System.Windows.Forms.Label();
  25. this.LblCount = new System.Windows.Forms.Label();
  26. //
  27. // panel1
  28. //
  29. this.BackColor = System.Drawing.Color.White;
  30. this.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
  31. this.Controls.Add(this.LblCount);
  32. this.Controls.Add(this.LblName);
  33. this.Controls.Add(this.PbxPhoto);
  34. this.Location = new System.Drawing.Point(45, 43);
  35. this.Name = "panel1";
  36. this.Size = new System.Drawing.Size(182, 175);
  37. this.TabIndex = 0;
  38. //
  39. // PbxPhoto
  40. //
  41. this.PbxPhoto.Location = new System.Drawing.Point(38, 21);
  42. this.PbxPhoto.Name = "PbxPhoto";
  43. this.PbxPhoto.Size = new System.Drawing.Size(100, 86);
  44. this.PbxPhoto.SizeMode = System.Windows.Forms.PictureBoxSizeMode.Zoom;
  45. this.PbxPhoto.TabIndex = 0;
  46. this.PbxPhoto.TabStop = false;
  47. //
  48. // LblName
  49. //
  50. this.LblName.AutoSize = true;
  51. this.LblName.Location = new System.Drawing.Point(15, 114);
  52. this.LblName.Name = "LblName";
  53. this.LblName.Size = new System.Drawing.Size(44, 16);
  54. this.LblName.TabIndex = 1;
  55. this.LblName.Text = "label1";
  56. //
  57. // LblCount
  58. //
  59. this.LblCount.AutoSize = true;
  60. this.LblCount.Location = new System.Drawing.Point(15, 140);
  61. this.LblCount.Name = "LblCount";
  62. this.LblCount.Size = new System.Drawing.Size(44, 16);
  63. this.LblCount.TabIndex = 2;
  64. this.LblCount.Text = "label2";
  65. }
  66. }
  67. public Form1()
  68. {
  69. InitializeComponent();
  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.LblCount.Click += Object_Click;
  97. ItemFur.LblName.Click += Object_Click;
  98. flowLayoutPanel1.Controls.Add(ItemFur);
  99. // если добавилась первая плитка, то сделать ее выделенной
  100. if (flowLayoutPanel1.Controls.Count == 1)
  101. {
  102. CurrentFur = ItemFur;
  103. CurrentFur.BackColor = Color.LightGreen;
  104. LblID.Text = CurrentFur.ID;
  105. }
  106. }
  107. Con.Close();
  108. }
  109. private void Object_Click(object sender, EventArgs e)
  110. {
  111. CurrentFur.BackColor = Color.White;
  112. CurrentFur = (sender as Control).Parent as Furnitura;
  113. CurrentFur.BackColor = Color.LightGreen;
  114. LblID.Text = CurrentFur.ID;
  115. }
  116. private void ItemFur_Click(object sender, EventArgs e)
  117. {
  118. CurrentFur.BackColor = Color.White;
  119. CurrentFur = sender as Furnitura;
  120. CurrentFur.BackColor = Color.LightGreen;
  121. LblID.Text = CurrentFur.ID;
  122. }
  123. private void Form1_Load(object sender, EventArgs e)
  124. {
  125. FillPanel();
  126. }
  127. private void button1_Click(object sender, EventArgs e)
  128. {
  129. MessageBox.Show("ID выбранной фурнитуры: " + CurrentFur.ID);
  130. }
  131. }
  132. }