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

c#在运行时向面板添加标签,c#,winforms,C#,Winforms,因此,我对编程有点陌生,正如标题所说,我尝试向面板添加标签,这两个面板都是在运行时通过c#中的按钮单击创建的。也许文本框更好,但它不应该有这么大的区别。我已经找到了一些有用的答案,但总的来说,面板总是在运行时之前创建的 因此,我的问题是:如何将标签添加到面板中 newLabel.Parent=面板名称当我编写代码时还没有创建面板时。然后是否可以在面板上添加更多标签或项目框 这是我点击按钮的完整代码: // for dragging the panels during runtime P

因此,我对编程有点陌生,正如标题所说,我尝试向面板添加标签,这两个面板都是在运行时通过c#中的按钮单击创建的。也许文本框更好,但它不应该有这么大的区别。我已经找到了一些有用的答案,但总的来说,面板总是在运行时之前创建的

因此,我的问题是:如何将标签添加到面板中
newLabel.Parent=面板名称当我编写代码时还没有创建面板时。然后是否可以在面板上添加更多标签或项目框

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

 // 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!");
        }
    }
//用于在运行时拖动面板
点动;
标签[]标签=新标签[1000];
面板[]面板=新面板[1000];
//删除上次创建的面板的步骤
List panelsAdded=新列表();
//为每个创建的标签增加一个
int计数器=0;
//为每个创建的面板设置窗口中的位置
int counterpos_x=50;
int counterpos_y=50;
//从要获取标签文本的组合框中转换的字符串
管柱安装;
//我的尝试。。。不起作用
字符串面板名称;
私有void b创建\u单击(对象发送者,事件参数e)
{
如果(计数器<40)
{ 
Panel myPanel=新面板();
myPanel.Tag=“Panel”+计数器;
myPanel.Location=新点(对策x,对策y)
myPanel.高度=150;
myPanel.宽度=200;
myPanel.BorderStyle=System.Windows.Forms.BorderStyle.FixedSingle;
面板名称=“面板编号”+计数器;
//我需要如何声明这一点,标签才能用newLabel.Parent=panel\u name继承
myPanel.Name=面板名称;
//用于拖动面板
myPanel.MouseMove+=新的MouseEventHandler(myPanel\u MouseMove);
myPanel.MouseDown+=新的MouseEventHandler(myPanel\u MouseDown);
面板[计数器]=我的面板;
this.Controls.Add(myPanel);
//要删除最新的面板,请执行以下操作:
面板已添加。插入(0,myPanel);
//将选定的组合框项转换为标签的字符串
str_installation=this.cbAnlagen.GetItemText(this.cbAnlagen.SelectedItem);
//创建标签
Label newLabel=新标签();
newLabel.Name=“testLabel”;
newLabel.Text=str\u安装;
newLabel.AutoSize=true;
//!!以下是异常CS0029的问题!!
newLabel.Parent=面板名称;
对策x+=225;
如果(计数器%8==0)
{
对价y+=175;
对策x=50;
}
计数器++;
}
其他的
{
MessageBox.Show(“Maximale Anzahl an Anlagen erreicht.”,“Achtung!”);
}
}
您应该尝试以下方法:

myPanel.Controls.Add(newLabel);

而不是将父项设置为“名称”?。应将标签添加到面板(子控件)。与将面板添加到表单
this.Controls.Add(myPanel)中的操作相同,它们都是从
控件派生的,并且支持子控件。

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

每个面板实例都有一个属性“.Controls”,方法为“Add(control)”

因此,您必须执行该行

parentPanel.Controls.Add(labelToAdd)

Thx。对我来说很有效。