MainForm.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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.Threading.Tasks;
  9. using System.Windows.Forms;
  10. using System.Data.SqlClient;
  11. namespace Агенты
  12. {
  13. public partial class MainForm : Form
  14. {
  15. public MainForm()
  16. {
  17. InitializeComponent();
  18. }
  19. class ItemPanel : Panel
  20. {
  21. public System.Windows.Forms.Label LblPhone;
  22. public System.Windows.Forms.Label LblTitle;
  23. public System.Windows.Forms.Label LblAddress;
  24. public System.Windows.Forms.PictureBox PbxImage;
  25. public ItemPanel()
  26. {
  27. this.PbxImage = new System.Windows.Forms.PictureBox();
  28. this.LblAddress = new System.Windows.Forms.Label();
  29. this.LblTitle = new System.Windows.Forms.Label();
  30. this.LblPhone = new System.Windows.Forms.Label();
  31. //
  32. // panel1
  33. //
  34. this.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
  35. | System.Windows.Forms.AnchorStyles.Left)
  36. | System.Windows.Forms.AnchorStyles.Right)));
  37. this.BackColor = System.Drawing.Color.White;
  38. this.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
  39. this.Controls.Add(this.LblPhone);
  40. this.Controls.Add(this.LblTitle);
  41. this.Controls.Add(this.LblAddress);
  42. this.Controls.Add(this.PbxImage);
  43. this.Location = new System.Drawing.Point(33, 35);
  44. this.Name = "panel1";
  45. this.Size = new System.Drawing.Size(872, 172);
  46. this.TabIndex = 0;
  47. //
  48. // PbxImage
  49. //
  50. this.PbxImage.Location = new System.Drawing.Point(4, 4);
  51. this.PbxImage.Name = "PbxImage";
  52. this.PbxImage.Size = new System.Drawing.Size(138, 122);
  53. this.PbxImage.SizeMode = System.Windows.Forms.PictureBoxSizeMode.Zoom;
  54. this.PbxImage.TabIndex = 0;
  55. this.PbxImage.TabStop = false;
  56. //
  57. // LblAddress
  58. //
  59. this.LblAddress.AutoSize = true;
  60. this.LblAddress.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(204)));
  61. this.LblAddress.Location = new System.Drawing.Point(4, 133);
  62. this.LblAddress.Name = "LblAddress";
  63. this.LblAddress.Size = new System.Drawing.Size(64, 25);
  64. this.LblAddress.TabIndex = 1;
  65. this.LblAddress.Text = "label1";
  66. //
  67. // LblTitle
  68. //
  69. this.LblTitle.AutoSize = true;
  70. this.LblTitle.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(204)));
  71. this.LblTitle.Location = new System.Drawing.Point(152, 4);
  72. this.LblTitle.Name = "LblTitle";
  73. this.LblTitle.Size = new System.Drawing.Size(64, 25);
  74. this.LblTitle.TabIndex = 2;
  75. this.LblTitle.Text = "label2";
  76. //
  77. // LblPhone
  78. //
  79. this.LblPhone.AutoSize = true;
  80. this.LblPhone.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(204)));
  81. this.LblPhone.Location = new System.Drawing.Point(152, 55);
  82. this.LblPhone.Name = "LblPhone";
  83. this.LblPhone.Size = new System.Drawing.Size(64, 25);
  84. this.LblPhone.TabIndex = 3;
  85. this.LblPhone.Text = "label3";
  86. }
  87. }
  88. string TxtCon = @"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\agent1.mdf;Integrated Security=True;Connect Timeout=30";
  89. struct Agent
  90. {
  91. // название, адрес, телефон, логотип
  92. public string Title, Address, Phone, Logo;
  93. }
  94. List<Agent> LstAgent = new List<Agent>();
  95. ItemPanel CurrentPanel; // текущая плитка
  96. void GetDateFromDB()
  97. {
  98. SqlConnection Con = new SqlConnection(TxtCon);
  99. Con.Open();
  100. string TxtQuery = "SELECT Title, Address, Phone, Logo FROM Agent";
  101. SqlCommand Query = new SqlCommand(TxtQuery, Con);
  102. SqlDataReader Res = Query.ExecuteReader();
  103. LstAgent.Clear();
  104. while (Res.Read())
  105. {
  106. Agent Agent1 = new Agent();
  107. Agent1.Title = Res["Title"].ToString(); // название
  108. Agent1.Address = Res["Address"].ToString(); // адрес
  109. Agent1.Phone = Res["Phone"].ToString(); // телефон
  110. Agent1.Logo = Res["Logo"].ToString(); // логотип
  111. LstAgent.Add(Agent1);
  112. }
  113. Con.Close();
  114. }
  115. void FillPanel()
  116. {
  117. MainPanel.Controls.Clear();
  118. // перебор агентов
  119. for(int i = 0; i <= LstAgent.Count-1; i++)
  120. {
  121. ItemPanel Item = new ItemPanel();
  122. Item.LblTitle.Text = LstAgent[i].Title;
  123. Item.LblPhone.Text = LstAgent[i].Phone;
  124. Item.LblAddress.Text = LstAgent[i].Address;
  125. try
  126. {
  127. Item.PbxImage.Image = Image.FromFile(Application.StartupPath + LstAgent[i].Logo);
  128. }
  129. catch
  130. {
  131. Item.PbxImage.Image = Image.FromFile(Application.StartupPath + "\\agents\\picture.png");
  132. }
  133. Item.Click += Item_Click; // щелчок на панель
  134. Item.PbxImage.Click += Object_Click;
  135. Item.LblAddress.Click += Object_Click;
  136. Item.LblPhone.Click += Object_Click;
  137. Item.LblTitle.Click += Object_Click;
  138. MainPanel.Controls.Add(Item); // добавить плитку на панель
  139. // если плитка самая первая, то сделать ее текущей
  140. if (MainPanel.Controls.Count == 1)
  141. {
  142. CurrentPanel = Item;
  143. CurrentPanel.BackColor = Color.LightGreen;
  144. }
  145. }
  146. }
  147. private void Object_Click(object sender, EventArgs e)
  148. {
  149. CurrentPanel.BackColor = Color.White; // снять выделение с текущей плитки
  150. // сделать текущей панель, на которой щелкнули
  151. CurrentPanel = (sender as Control).Parent as ItemPanel;
  152. CurrentPanel.BackColor = Color.LightGreen; // выделить панель
  153. }
  154. private void Item_Click(object sender, EventArgs e)
  155. {
  156. CurrentPanel.BackColor = Color.White; // снять выделение с текущей плитки
  157. CurrentPanel = sender as ItemPanel; // сделать текущей панель, на которой щелкнули
  158. CurrentPanel.BackColor = Color.LightGreen; // выделить панель
  159. }
  160. private void Form1_Load(object sender, EventArgs e)
  161. {
  162. GetDateFromDB();
  163. FillPanel();
  164. }
  165. }
  166. }