WORKING WITH CREATING DYNAMIC CONTROL IN C#

marcel ethan

Total Post:105

Points:735
Posted by  marcel ethan
C# 
C#
 1119  View(s)
Ratings:
Rate this:
Hi Developers,

How can I create Dynamic Label control using C#?


  1. Sumit Kesarwani

    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;

            }

        }

    }

Answer

NEWSLETTER

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