Working with creating dynamic control in c#

Total Post:104

Points:728
C# 
C#
 1315  View(s)
Ratings:
Rate this:
Hi Developers,

How can I create Dynamic Label control using C#?


  1. Post:378

    Points:2694
    Re: Working with creating dynamic control in c#

    Hi Marcel,


    Its very easy to create a dynamic control in C#, following, lines of code will solve your problem.

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
     
    namespace MinimizDemo
    {
        public partial class Form4 : Form
        {
            public Form4()
            {
                InitializeComponent();
            }
            int cLeft = 1;
     
            private void button1_Click(object sender, EventArgs e)
            {
                AddNewTextBox();
            }
            public System.Windows.Forms.Label AddNewTextBox()
            {
                System.Windows.Forms.Label txt = new System.Windows.Forms.Label();
                this.Controls.Add(txt);
                txt.Top = cLeft * 25;
                txt.Left = 100;
                txt.Text = "Label " + this.cLeft.ToString();
                cLeft = cLeft + 1;
                return txt;
            }
        }
    }

      Modified On Mar-30-2018 04:00:58 AM

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!