MainForm.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. class ItemPanel : Panel
  16. {
  17. public System.Windows.Forms.PictureBox PbxLogo;
  18. public System.Windows.Forms.Label LblPhone;
  19. public System.Windows.Forms.Label LblTitle;
  20. public string ID;
  21. public ItemPanel()
  22. {
  23. this.PbxLogo = new System.Windows.Forms.PictureBox();
  24. this.LblTitle = new System.Windows.Forms.Label();
  25. this.LblPhone = 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.LblPhone);
  32. this.Controls.Add(this.LblTitle);
  33. this.Controls.Add(this.PbxLogo);
  34. this.Location = new System.Drawing.Point(76, 43);
  35. this.Name = "panel1";
  36. this.Size = new System.Drawing.Size(296, 200);
  37. this.TabIndex = 0;
  38. //
  39. // PbxLogo
  40. //
  41. this.PbxLogo.Location = new System.Drawing.Point(13, 18);
  42. this.PbxLogo.Name = "PbxLogo";
  43. this.PbxLogo.Size = new System.Drawing.Size(119, 120);
  44. this.PbxLogo.SizeMode = System.Windows.Forms.PictureBoxSizeMode.Zoom;
  45. this.PbxLogo.TabIndex = 0;
  46. this.PbxLogo.TabStop = false;
  47. //
  48. // LblTitle
  49. //
  50. this.LblTitle.AutoSize = true;
  51. this.LblTitle.Location = new System.Drawing.Point(13, 145);
  52. this.LblTitle.Name = "LblTitle";
  53. this.LblTitle.Size = new System.Drawing.Size(46, 17);
  54. this.LblTitle.TabIndex = 1;
  55. this.LblTitle.Text = "label1";
  56. //
  57. // LblPhone
  58. //
  59. this.LblPhone.AutoSize = true;
  60. this.LblPhone.Location = new System.Drawing.Point(13, 171);
  61. this.LblPhone.Name = "LblPhone";
  62. this.LblPhone.Size = new System.Drawing.Size(46, 17);
  63. this.LblPhone.TabIndex = 2;
  64. this.LblPhone.Text = "label2";
  65. }
  66. }
  67. public MainForm()
  68. {
  69. InitializeComponent();
  70. }
  71. string TxtCon = @"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\agent1.mdf;Integrated Security=True;Connect Timeout=30";
  72. struct Agent
  73. {
  74. // название, адрес, телефон, логотип
  75. public string ID, Title, Address, Phone, Logo;
  76. }
  77. List<Agent> LstAgent = new List<Agent>();
  78. ItemPanel CurrentItem;
  79. void GetDateFromDB()
  80. {
  81. //SqlConnection Con = new SqlConnection(TxtCon);
  82. //Con.Open();
  83. //string TxtQuery = "SELECT ID, Title, Address, Phone, Logo FROM Agent";
  84. //SqlCommand Query = new SqlCommand(TxtQuery, Con);
  85. //SqlDataReader Res = Query.ExecuteReader();
  86. //LstAgent.Clear();
  87. //while (Res.Read())
  88. //{
  89. // Agent Agent1 = new Agent();
  90. // Agent1.Title = Res["Title"].ToString(); // название
  91. // Agent1.Address = Res["Address"].ToString(); // адрес
  92. // Agent1.Phone = Res["Phone"].ToString(); // телефон
  93. // Agent1.Logo = Res["Logo"].ToString(); // логотип
  94. // Agent1.ID = Res["ID"].ToString();
  95. // LstAgent.Add(Agent1);
  96. //}
  97. //Con.Close();
  98. string[] Titles= new string[] {"Ковалев", "Бонда", "Кривошеев", "Лебедкин"};
  99. string[] Addresses = new string[] { "Новозыбков", "Брянск", "Москва", "Париж" };
  100. string[] Phones = new string[] { "3-15-24", "+8(48343)336985", "+375(940)45225621", "0-00-012" };
  101. string[] Logos = new string[] { "agent_1.png", "agent_2.png", "agent_3.png", "agent_4.png", "agent_5.png", "agent_6.png", "agent_7.png" };
  102. LstAgent.Clear();
  103. Random rnd = new Random();
  104. for (int i=0; i<20; i++)
  105. {
  106. Agent Agent1 = new Agent();
  107. Agent1.Title = Titles[rnd.Next(0, Titles.Length)]; // название
  108. Agent1.Address = Addresses[rnd.Next(0, Addresses.Length)];
  109. Agent1.Phone = Phones[rnd.Next(0, Phones.Length)];
  110. Agent1.Logo = Logos[rnd.Next(0, Logos.Length)]; //
  111. LstAgent.Add(Agent1);
  112. }
  113. }
  114. void FillPanel()
  115. {
  116. MainPanel.Controls.Clear();
  117. foreach(Agent Agent1 in LstAgent)
  118. {
  119. ItemPanel Item = new ItemPanel();
  120. Item.LblTitle.Text = Agent1.Title;
  121. Item.LblPhone.Text = Agent1.Phone;
  122. Item.ID = Agent1.ID;
  123. try
  124. {
  125. Item.PbxLogo.Image = Image.FromFile(Application.StartupPath + "\\agents\\" + Agent1.Logo);
  126. }
  127. catch
  128. {
  129. Item.PbxLogo.Image = Image.FromFile(Application.StartupPath + "\\agents\\picture.png");
  130. }
  131. MainPanel.Controls.Add(Item);
  132. Item.Click += Item_Click;
  133. Item.PbxLogo.Click += Object_Click;
  134. Item.LblTitle.Click += Object_Click;
  135. Item.LblPhone.Click += Object_Click;
  136. if (MainPanel.Controls.Count == 1)
  137. {
  138. CurrentItem = Item;
  139. CurrentItem.BackColor = Color.LightGreen;
  140. }
  141. }
  142. }
  143. private void Object_Click(object sender, EventArgs e)
  144. {
  145. CurrentItem.BackColor = Color.White;
  146. CurrentItem = (sender as Control).Parent as ItemPanel;
  147. CurrentItem.BackColor = Color.LightGreen;
  148. }
  149. private void Item_Click(object sender, EventArgs e)
  150. {
  151. CurrentItem.BackColor = Color.White;
  152. CurrentItem = sender as ItemPanel;
  153. CurrentItem.BackColor = Color.LightGreen;
  154. }
  155. private void Form1_Load(object sender, EventArgs e)
  156. {
  157. GetDateFromDB();
  158. FillPanel();
  159. }
  160. private void button1_Click(object sender, EventArgs e)
  161. {
  162. //MessageBox.Show(CurrentItem.ID);
  163. GetDateFromDB();
  164. FillPanel();
  165. }
  166. }
  167. }