Home > DeveloperSection > Forums > Working with creating dynamic control in c#
marcel ethan
marcel ethan

Total Post:105

Points:735
Posted on    June-15-2013 5:27 AM

 C# C# 
Ratings:


 1 Reply(s)
 858  View(s)
Rate this:
Hi Developers,

How can I create Dynamic Label control using C#?




Sumit Kesarwani

Total Post:378

Points:2694
Posted on    June-15-2013 6:54 AM

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;

        }

    }

}


Don't want to miss updates? Please click the below button!

Follow MindStick