Jump to content
  • Advertisement
Sign in to follow this  
Winegums

Creating labels in code in C#

This topic is 3657 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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;     
        }

    }


Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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 :)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!