c#在运行时向面板添加标签

时间:2017-08-30 08:25:58

标签: c# winforms

所以,我对编程很陌生,我尝试,正如标题所说,为面板添加标签,这些标签都是在运行时通过c#中的按钮单击创建的。也许文本框更好,但它不应该做这样的差异。我已经找到了一些有用的答案,但总的来说,面板已经在运行时之前创建了。

所以,我的问题是:如何将标签添加到面板中 当我编写代码时没有创建面板时newLabel.Parent = panel_name;。那么是否可以在面板上添加更多标签或代码?

这是按钮点击的完整代码:

 // 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!");
        }
    }

2 个答案:

答案 0 :(得分:1)

你应该试试这个:

myPanel.Controls.Add(newLabel);

而不是将父级设置为?名称?您应该将标签添加到面板(子控件)。与将面板添加到this.Controls.Add(myPanel);形式的面板相同,这些面板均来自Control并支持子控件。

答案 1 :(得分:0)

您正在使用object,每个面板实例都是Panel类的一个对象

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

每个面板实例都有一个属性&#34; .Controls&#34;,with method&#34; Add(control)

所以你必须执行

parentPanel.Controls.Add(labelToAdd)