Skip to content

Creating new labels within a form

Dave Fitter

Asked Dec 18, 2008, 4:44 AM EST1 answer
Share
LinkedInX

Help needed! I want to pass text from a text box to a label, but then once that label has text in it,  for the program to create a new label underneath the previous one, so when i add some more text it fills the new label and so on and so on.  The code below will take the text you write and put it into the label, but i cant figure out how to do it with more than one, except by using if / else if statements which i do not want to do.

C#
namespace WindowsApplication1
{
   public partial class Form1 : Form
   {
   public Form1()
      {
      InitializeComponent();
      }
      private void button1_Click(object sender, EventArgs e)
      {
      string a = textBox1.Text;
      label1.Text = a;

       }
}

1 Answer

Know the answer? Post it. Somebody with the same question will find it here.

  • Bechir Bejaoui
    0
    then try this code

    public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
                button1.Location = new Point(100, 40);
                textBox1.Location = new Point(100, 80);
            }
            string text;
            int x = 40;
            int y = 40;
            Label oLabel;
            private void button1_Click(object sender, EventArgs e)
            {
               
                text = "";
                text = textBox1.Text;
                oLabel = new Label();
                oLabel.Text = text;
                this.Controls.Add(oLabel);
                oLabel.Visible = true;
                oLabel.Location = new Point(x, y);
                y += 20;

            }
        }
    0