NEWS

[C#] Hướng dẫn tạo Dynamic Control Runtime để hiển thị danh bạ

[C#] Hướng dẫn tạo Dynamic Control Runtime để hiển thị danh bạ
Đăng bởi: Thảo Meo - Lượt xem: 5563 08:56:24, 19/02/2020C#   In bài viết

Xin chào các bạn, bài viết hôm nay mình sẽ hướng dẫn các bạn các tạo control động Runtime trong lập trình C# Winform.

[C#] How to create Dynamic Control runtime

Trong code, bên dưới đây, mình sẽ tạo danh bạ và trong Flow layout control.

Khi nhấn nút Add Contact, thì sẽ tạo ra một control danh bạ, kèm theo sự kiện Xóa cho từng control.

Dưới đây là giao diện demo ứng dụng:

dynamic_control_csharp

Full source code Create Dynamic Control Runtime C#:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Text;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;


namespace CreateControlRunTime
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        PrivateFontCollection pfc;
        private string _CurrentContactPanelName = null;        
        private int _ContactPanelsAddedCount = 0;
       

        public void CreateContactPanel()
        {
            Panel contactPanel;
            contactPanel = new Panel();           
            {
                var withBlock = contactPanel;
                withBlock.BackColor = Color.White;
                withBlock.Size = new Size(420, 50);
                withBlock.Name = "pnlContact" + (_ContactPanelsAddedCount + 1).ToString();
            }
            flpMain.Controls.Add(contactPanel);
            flpMain.ScrollControlIntoView(contactPanel);
            _CurrentContactPanelName = contactPanel.Name;
            _ContactPanelsAddedCount += 1;
        }

        private void CreateContactDeleteBtn(string panelName)
        {
            Button contactDeleteButton;
            contactDeleteButton = new Button();        
            {
                var withBlock = contactDeleteButton;
                withBlock.AutoSize = false;
                withBlock.Size = new Size(90, 30);
                withBlock.BackColor = Color.Silver;
                withBlock.ForeColor = Color.Black;
                withBlock.Location = new Point(300, 10);
                withBlock.Name = "btnContactDelete" + (_ContactPanelsAddedCount).ToString();
                withBlock.Text = "Delete";
            }
          
            foreach (Control controlObject in flpMain.Controls)
            {
                if (controlObject.Name == panelName)
                    controlObject.Controls.Add(contactDeleteButton);
            }
          
            contactDeleteButton.Click += DynamicButton_Click;
        }

        public void DynamicButton_Click(object sender, EventArgs e)
        {
            string parentPanelName;

            parentPanelName = null;            
            foreach (Control controlObj in flpMain.Controls)
            {
                foreach (Control childControlObj in controlObj.Controls)
                {
                    if (childControlObj.Name == (sender as Button).Name)
                    {
                        childControlObj.Click -= DynamicButton_Click;
                        parentPanelName = childControlObj.Parent.Name;
                    }
                }
            }
          
            foreach (Control controlObj in flpMain.Controls)
            {
                if (controlObj.Name == parentPanelName)
                {
                    flpMain.Controls.Remove(controlObj);
                    controlObj.Dispose();
                }
            }
        }

        public void CreateContactPictureBox(string panelName, string imagePath)
        {
            PictureBox contactPictureBox;
            contactPictureBox = new PictureBox();

            {
                var withBlock = contactPictureBox;
                withBlock.SizeMode = PictureBoxSizeMode.Zoom;
                withBlock.Size = new Size(45, 45);
                withBlock.Location = new Point(5, 5);
                withBlock.Name = "picContactImage" + (_ContactPanelsAddedCount).ToString();
                withBlock.ImageLocation = imagePath;
            }

            Rectangle r = new Rectangle(0, 0, contactPictureBox.Width, contactPictureBox.Height);
            System.Drawing.Drawing2D.GraphicsPath gp = new System.Drawing.Drawing2D.GraphicsPath();

            gp.AddEllipse(0, 0, contactPictureBox.Width - 3, contactPictureBox.Height - 3);
            Region rg = new Region(gp);
            contactPictureBox.Region = rg;

            foreach (Control controlObject in flpMain.Controls)
            {
                if (controlObject.Name == panelName)
                    controlObject.Controls.Add(contactPictureBox);
            }
        }

             private Random rnd = new Random();
        public void CreateContactnameLabel(string panelName, string textToShow)
        {
        Label contactNameLabel;
            Color randomColor = Color.FromArgb(rnd.Next(256), rnd.Next(256), rnd.Next(256));
            contactNameLabel = new Label();
            {
                var withBlock = contactNameLabel;
                withBlock.AutoSize = true;
                withBlock.Location = new Point(60, 10);
                withBlock.Name = "lblContactName" + _ContactPanelsAddedCount.ToString();
                withBlock.Font = new Font(pfc.Families[0], 9.75f, FontStyle.Bold);
                withBlock.ForeColor = randomColor;
                withBlock.Text = textToShow;
            }

            foreach (Control controlObject in flpMain.Controls)
            {
                if (controlObject.Name == panelName)
                    controlObject.Controls.Add(contactNameLabel);
            }
        }

       

        public void CreateContactphoneLabel(string panelName, string textToShow)
        {
            Label contactNameLabel;
            contactNameLabel = new Label();
            {
                var withBlock = contactNameLabel;
                withBlock.AutoSize = true;
                withBlock.Location = new Point(60, 30);
                withBlock.Name = "lblContactPhone" + _ContactPanelsAddedCount.ToString();
                withBlock.Font = new Font(pfc.Families[0], 8.75f, FontStyle.Bold);
                withBlock.ForeColor = Color.Gray;
                withBlock.Text = textToShow;
            }

            foreach (Control controlObject in flpMain.Controls)
            {
                if (controlObject.Name == panelName)
                    controlObject.Controls.Add(contactNameLabel);
            }
        }

        int i = 0;

       

        private void btn_addContact_Click(object sender, EventArgs e)
        {


            if (i==0)
            {
                var strImage = "logo.png";
                CreateContactPanel();
                CreateContactPictureBox(_CurrentContactPanelName, strImage);
                CreateContactnameLabel(_CurrentContactPanelName, "LAPTRINHVB.NET");
                CreateContactphoneLabel(_CurrentContactPanelName, "+(84) 956.123.122");
                CreateContactDeleteBtn(_CurrentContactPanelName);
               
            }
            else if(i == 1)
            {
                var strImage = "Ronaldo.jpg";
                CreateContactPanel();
                CreateContactPictureBox(_CurrentContactPanelName, strImage);
                CreateContactnameLabel(_CurrentContactPanelName, "Ronaldo CR7");
                CreateContactphoneLabel(_CurrentContactPanelName, "+(84) 956.123.122");
                CreateContactDeleteBtn(_CurrentContactPanelName);
            }else if (i == 2)
            {
                var strImage = "midu.jpg";
                CreateContactPanel();
                CreateContactPictureBox(_CurrentContactPanelName, strImage);
                CreateContactnameLabel(_CurrentContactPanelName, "Midu");
                CreateContactphoneLabel(_CurrentContactPanelName, "+(84) 956.123.122");
                CreateContactDeleteBtn(_CurrentContactPanelName);
               

            }
            else if (i == 3)
            {
                var strImage = "thaomeo.jpg";
                CreateContactPanel();
                CreateContactPictureBox(_CurrentContactPanelName, strImage);
                CreateContactnameLabel(_CurrentContactPanelName, "Thảo Meo");
                CreateContactphoneLabel(_CurrentContactPanelName, "+(84) 956.123.122");
                CreateContactDeleteBtn(_CurrentContactPanelName);


            }
            else if (i == 4)
            {
                var strImage = "avatar.jpg";
                CreateContactPanel();
                CreateContactPictureBox(_CurrentContactPanelName, strImage);
                CreateContactnameLabel(_CurrentContactPanelName, "Ribi Sachi");
                CreateContactphoneLabel(_CurrentContactPanelName, "+(84) 956.123.122");
                CreateContactDeleteBtn(_CurrentContactPanelName);


            }

            i++;
            if (i > 4)
            {
                i = 0;
            }
            


        }

        private void Form1_Load(object sender, EventArgs e)
        {
            pfc = new PrivateFontCollection();
            pfc.AddFontFile(@"AnyConv.com__HelveticaNeue.ttf");
        }
    }



}

Thanks for watching!

 

DOWNLOAD SOURCE

THÔNG TIN TÁC GIẢ

BÀI VIẾT LIÊN QUAN

[C#] Hướng dẫn tạo Dynamic Control Runtime để hiển thị danh bạ
Đăng bởi: Thảo Meo - Lượt xem: 5563 08:56:24, 19/02/2020C#   In bài viết

CÁC BÀI CÙNG CHỦ ĐỀ

Đọc tiếp
.