1
0

MainForm.cs 7.5 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 bool IsSelected; // является ли плитка выделенной
  26. public ItemPanel()
  27. {
  28. this.PbxImage = new System.Windows.Forms.PictureBox();
  29. this.LblAddress = new System.Windows.Forms.Label();
  30. this.LblTitle = new System.Windows.Forms.Label();
  31. this.LblPhone = new System.Windows.Forms.Label();
  32. //
  33. // panel1
  34. //
  35. this.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
  36. | System.Windows.Forms.AnchorStyles.Left)
  37. | System.Windows.Forms.AnchorStyles.Right)));
  38. this.BackColor = System.Drawing.Color.White;
  39. this.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
  40. this.Controls.Add(this.LblPhone);
  41. this.Controls.Add(this.LblTitle);
  42. this.Controls.Add(this.LblAddress);
  43. this.Controls.Add(this.PbxImage);
  44. this.Location = new System.Drawing.Point(33, 35);
  45. this.Name = "panel1";
  46. this.Size = new System.Drawing.Size(872, 172);
  47. this.TabIndex = 0;
  48. //
  49. // PbxImage
  50. //
  51. this.PbxImage.Location = new System.Drawing.Point(4, 4);
  52. this.PbxImage.Name = "PbxImage";
  53. this.PbxImage.Size = new System.Drawing.Size(138, 122);
  54. this.PbxImage.SizeMode = System.Windows.Forms.PictureBoxSizeMode.Zoom;
  55. this.PbxImage.TabIndex = 0;
  56. this.PbxImage.TabStop = false;
  57. //
  58. // LblAddress
  59. //
  60. this.LblAddress.AutoSize = true;
  61. this.LblAddress.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(204)));
  62. this.LblAddress.Location = new System.Drawing.Point(4, 133);
  63. this.LblAddress.Name = "LblAddress";
  64. this.LblAddress.Size = new System.Drawing.Size(64, 25);
  65. this.LblAddress.TabIndex = 1;
  66. this.LblAddress.Text = "label1";
  67. //
  68. // LblTitle
  69. //
  70. this.LblTitle.AutoSize = true;
  71. this.LblTitle.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(204)));
  72. this.LblTitle.Location = new System.Drawing.Point(152, 4);
  73. this.LblTitle.Name = "LblTitle";
  74. this.LblTitle.Size = new System.Drawing.Size(64, 25);
  75. this.LblTitle.TabIndex = 2;
  76. this.LblTitle.Text = "label2";
  77. //
  78. // LblPhone
  79. //
  80. this.LblPhone.AutoSize = true;
  81. this.LblPhone.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(204)));
  82. this.LblPhone.Location = new System.Drawing.Point(152, 55);
  83. this.LblPhone.Name = "LblPhone";
  84. this.LblPhone.Size = new System.Drawing.Size(64, 25);
  85. this.LblPhone.TabIndex = 3;
  86. this.LblPhone.Text = "label3";
  87. }
  88. }
  89. string TxtCon = @"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\agent1.mdf;Integrated Security=True;Connect Timeout=30";
  90. struct Agent
  91. {
  92. // название, адрес, телефон, логотип
  93. public string Title, Address, Phone, Logo;
  94. }
  95. List<Agent> LstAgent = new List<Agent>();
  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. }
  141. private void Object_Click(object sender, EventArgs e)
  142. {
  143. // плитка, на которой находится объект, на котором выполнили щелчок
  144. ItemPanel CurrentItem = (sender as Control).Parent as ItemPanel;
  145. Item_Click(CurrentItem, e);
  146. }
  147. private void Item_Click(object sender, EventArgs e)
  148. {
  149. ItemPanel CurrentItem = sender as ItemPanel; // текущая плитка
  150. if (CurrentItem.IsSelected) // если плитка выделена
  151. {
  152. CurrentItem.BackColor = Color.White; // снять выделение
  153. CurrentItem.IsSelected = false;
  154. }
  155. else // если плитка не выделена
  156. {
  157. CurrentItem.BackColor = Color.LightGreen; // выделить плитку
  158. CurrentItem.IsSelected = true;
  159. }
  160. }
  161. private void Form1_Load(object sender, EventArgs e)
  162. {
  163. GetDateFromDB();
  164. FillPanel();
  165. }
  166. }
  167. }