Creating labels in code in C#

Started by
1 comment, last by Winegums 15 years, 10 months ago
Hi, I'm trying to write an app in C# which involves generating labels and buttons within the source code. I've used C# before, but I suppose I'm a bit rusty and I can't see what's wrong with my code. I googled and came up with this link, but I can't see what the difference is between my code and his (in implementation at least)


    public partial class Form1 : Form
    {
        private int verticalPosition = 30;
        private int horizontalPosition = 10;

        public Form1()
        {
            InitializeComponent();
        }

        private void CreateThreadButton_Click(object sender, EventArgs e)
        {
            if (threadNameTextBox.Text == "")
            {
                MessageBox.Show("Please enter a thread name");
            }
            else
            {
                CreateNewThread(threadNameTextBox.Text);
            }
        }

        private void CreateNewThread(string p)
        {
            System.Windows.Forms.Label label = new Label();
            label.Text = p;
            label.Location = new Point(horizontalPosition, verticalPosition);

            this.Controls.Add(label);
            verticalPosition += 10;     
        }

    }


Advertisement
System.Windows.Forms.Label label = new Label();label.Text = "Hello World!";label.Location = new Point(0, 0);this.Controls.Add(label);


Worked fine for me, are you sure your CreateNewThread function is being called, if so, make sure the label isnt being positioned behind any other controls, altough I would of thought they would allways be on top of the last when they where added.
turns out they were being created on top of each other, but newer ones were buried underneath older ones (which i agree is odd)

anyway thanks :)

This topic is closed to new replies.

Advertisement