| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183 | using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Windows.Forms;using System.Data.SqlClient;namespace Агенты{    public partial class MainForm : Form    {        public MainForm()        {            InitializeComponent();        }        class ItemPanel : Panel        {            public System.Windows.Forms.Label LblPhone;            public System.Windows.Forms.Label LblTitle;            public System.Windows.Forms.Label LblAddress;            public System.Windows.Forms.PictureBox PbxImage;            public bool IsSelected;  // является ли плитка выделенной            public ItemPanel()            {                this.PbxImage = new System.Windows.Forms.PictureBox();                this.LblAddress = new System.Windows.Forms.Label();                this.LblTitle = new System.Windows.Forms.Label();                this.LblPhone = new System.Windows.Forms.Label();                //                 // panel1                //                 this.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)                | System.Windows.Forms.AnchorStyles.Left)                | System.Windows.Forms.AnchorStyles.Right)));                this.BackColor = System.Drawing.Color.White;                this.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;                this.Controls.Add(this.LblPhone);                this.Controls.Add(this.LblTitle);                this.Controls.Add(this.LblAddress);                this.Controls.Add(this.PbxImage);                this.Location = new System.Drawing.Point(33, 35);                this.Name = "panel1";                this.Size = new System.Drawing.Size(872, 172);                this.TabIndex = 0;                //                 // PbxImage                //                 this.PbxImage.Location = new System.Drawing.Point(4, 4);                this.PbxImage.Name = "PbxImage";                this.PbxImage.Size = new System.Drawing.Size(138, 122);                this.PbxImage.SizeMode = System.Windows.Forms.PictureBoxSizeMode.Zoom;                this.PbxImage.TabIndex = 0;                this.PbxImage.TabStop = false;                //                 // LblAddress                //                 this.LblAddress.AutoSize = true;                this.LblAddress.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(204)));                this.LblAddress.Location = new System.Drawing.Point(4, 133);                this.LblAddress.Name = "LblAddress";                this.LblAddress.Size = new System.Drawing.Size(64, 25);                this.LblAddress.TabIndex = 1;                this.LblAddress.Text = "label1";                //                 // LblTitle                //                 this.LblTitle.AutoSize = true;                this.LblTitle.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(204)));                this.LblTitle.Location = new System.Drawing.Point(152, 4);                this.LblTitle.Name = "LblTitle";                this.LblTitle.Size = new System.Drawing.Size(64, 25);                this.LblTitle.TabIndex = 2;                this.LblTitle.Text = "label2";                //                 // LblPhone                //                 this.LblPhone.AutoSize = true;                this.LblPhone.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(204)));                this.LblPhone.Location = new System.Drawing.Point(152, 55);                this.LblPhone.Name = "LblPhone";                this.LblPhone.Size = new System.Drawing.Size(64, 25);                this.LblPhone.TabIndex = 3;                this.LblPhone.Text = "label3";            }        }        string TxtCon = @"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\agent1.mdf;Integrated Security=True;Connect Timeout=30";        struct Agent        {            // название, адрес, телефон, логотип            public string Title, Address, Phone, Logo;        }        List<Agent> LstAgent = new List<Agent>();        void GetDateFromDB()        {            SqlConnection Con = new SqlConnection(TxtCon);            Con.Open();            string TxtQuery = "SELECT Title, Address, Phone, Logo FROM Agent";            SqlCommand Query = new SqlCommand(TxtQuery, Con);            SqlDataReader Res = Query.ExecuteReader();            LstAgent.Clear();            while (Res.Read())            {                Agent Agent1 = new Agent();                Agent1.Title = Res["Title"].ToString();  // название                Agent1.Address = Res["Address"].ToString();  // адрес                Agent1.Phone = Res["Phone"].ToString();  // телефон                Agent1.Logo = Res["Logo"].ToString();  // логотип                LstAgent.Add(Agent1);            }            Con.Close();        }        void FillPanel()        {            MainPanel.Controls.Clear();            // перебор агентов            for(int i = 0; i <= LstAgent.Count-1; i++)            {                ItemPanel Item = new ItemPanel();                Item.LblTitle.Text = LstAgent[i].Title;                Item.LblPhone.Text = LstAgent[i].Phone;                Item.LblAddress.Text = LstAgent[i].Address;                try                {                    Item.PbxImage.Image = Image.FromFile(Application.StartupPath + LstAgent[i].Logo);                }                catch                {                    Item.PbxImage.Image = Image.FromFile(Application.StartupPath + "\\agents\\picture.png");                }                Item.Click += Item_Click;  // щелчок на панели                Item.PbxImage.Click += Object_Click;                Item.LblAddress.Click += Object_Click;                Item.LblPhone.Click += Object_Click;                Item.LblTitle.Click += Object_Click;                MainPanel.Controls.Add(Item);  // добавить плитку на панель            }        }        private void Object_Click(object sender, EventArgs e)        {            // плитка, на которой находится объект, на котором выполнили щелчок            ItemPanel CurrentItem = (sender as Control).Parent as ItemPanel;            Item_Click(CurrentItem, e);        }        private void Item_Click(object sender, EventArgs e)        {            ItemPanel CurrentItem = sender as ItemPanel;  // текущая плитка            if (CurrentItem.IsSelected)  // если плитка выделена            {                CurrentItem.BackColor = Color.White;  // снять выделение                CurrentItem.IsSelected = false;            }            else  // если плитка не выделена            {                CurrentItem.BackColor = Color.LightGreen;  // выделить плитку                CurrentItem.IsSelected = true;            }        }        private void Form1_Load(object sender, EventArgs e)        {            GetDateFromDB();            FillPanel();        }    }}
 |