Sakul
Sakul

Reputation: 3

c# add a label to a panel both at the runtime

So, I'm kinda new to programming and I try, as the title already says, to add a label to a panel which are both created at the runtime by a buttonclick in c#. Maybe a textbox is better, but it shouldn't do such a difference. I already found some helpful answers but in generall it's always that the panel is already created before the runtime.

So, my problem is: How can I add the label to the panel with newLabel.Parent = panel_name; when there is no created panel yet when I write the code. And is it then possible to add more labels or itemboxes to on panel?

Here's my full code for the button click:

 // for dragging the panels during runtime
    Point move;

    Label[] labels = new Label[1000];
    Panel[] panels = new Panel[1000];

    // To Remove the last created panel
    List<Panel> panelsAdded = new List<Panel>();

    // increments by one for each created label
    int counter = 0;

    // sets the posstion in the window for each created panel
    int counterpos_x = 50;
    int counterpos_y = 50;

    // converted string from the combobox where I want to get the text for the label
    string str_installation;

    // my try... doesn't work
    string panel_name;

    private void btnCreate_Click(object sender, EventArgs e)
    {
        if(counter < 40)
        { 

            Panel myPanel = new Panel();


            myPanel.Tag = "Panel" + counter;
            myPanel.Location = new Point(counterpos_x,counterpos_y)
            myPanel.Height = 150;
            myPanel.Width = 200;
            myPanel.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            panel_name = "Panelnumber" + counter;
            // how do I need to declare this for the label to inherit with newLabel.Parent = panel_name
            myPanel.Name = panel_name;

            // for dragging the panel         
            myPanel.MouseMove += new MouseEventHandler(myPanel_MouseMove);
            myPanel.MouseDown += new MouseEventHandler(myPanel_MouseDown);

            panels[counter] = myPanel;

            this.Controls.Add(myPanel);

            // to remove the latest panel
            panelsAdded.Insert(0, myPanel);

            // convert the selected combobox item into a string for label
            str_installation = this.cbAnlagen.GetItemText(this.cbAnlagen.SelectedItem);

            // create label
            Label newLabel = new Label();
            newLabel.Name = "testLabel";
            newLabel.Text = str_installation;
            newLabel.AutoSize = true;

            // !!here's the problem with the exception CS0029!!
            newLabel.Parent = panel_name;


            counterpos_x += 225;

            if(counter % 8 == 0)
            {
                counterpos_y += 175;
                counterpos_x = 50;
            }

            counter++;
        }
        else
        {
            MessageBox.Show("Maximale Anzahl an Anlagen erreicht.", "Achtung!");
        }
    }

Upvotes: 0

Views: 2276

Answers (2)

Jeroen van Langen
Jeroen van Langen

Reputation: 22038

You should try this:

myPanel.Controls.Add(newLabel);

Instead of setting a parent to a ?name?. You should add the label to the panel (child controls). Same like you did with adding the panel to the form this.Controls.Add(myPanel); which are both derived from Control and support child controls.

Upvotes: 1

Othello  .net dev
Othello .net dev

Reputation: 438

You are working with object, each panel instance is one object of class Panel

https://msdn.microsoft.com/it-it/library/system.windows.forms.panel(v=vs.110).aspx

each panel instance has one property ".Controls", with method "Add(control)

so you have to execute the line

parentPanel.Controls.Add(labelToAdd)

Upvotes: 0

Related Questions