C# 在表单问题中动态添加控件

C# 在表单问题中动态添加控件,c#,.net,arrays,winforms,C#,.net,Arrays,Winforms,我正在处理一个小项目,当我试图从数组生成列表时,创建的控件在添加后会被覆盖,因此我只能看到最后生成的控件。有什么我做得不对的吗?代码如下: string[] radios = { "fillydelphia_radio", "brony_radio", "luna_radio", "alicorn_radio", "sonic_radioboom", "fractured_frequencies", "ponyville_fm"

我正在处理一个小项目,当我试图从数组生成列表时,创建的控件在添加后会被覆盖,因此我只能看到最后生成的控件。有什么我做得不对的吗?代码如下:

    string[] radios = { "fillydelphia_radio", "brony_radio", "luna_radio", "alicorn_radio",
                              "sonic_radioboom", "fractured_frequencies", "ponyville_fm",
                              "everypony_radio", "wonderbolt_radio", "best_pony_radio",
                              "pegabit_sounds" };

    public List()
    {
        InitializeComponent();
        generateInfo();
        int i = 0;
        foreach (string l in radios)
        {
            Label tempname = radioName as Label;
            PictureBox templogo = radioLogo as PictureBox;
            templogo.Name = l + "logo";
            templogo.Location = new Point(templogo.Location.X, templogo.Location.Y + i);
            templogo.ImageLocation = RadiosInfo.getRadioInfo(l, "logo");
            tempname.Name = l + "name";
            tempname.Location = new Point(tempname.Location.X, tempname.Location.Y + i);
            tempname.Text = RadiosInfo.getRadioInfo(l, "name");
            SuspendLayout();
            this.Controls.Add(tempname);
            this.Controls.Add(templogo);
            ResumeLayout();
            i += 50;
        }
    }

    private PictureBox radioLogo = new PictureBox();
    private Label radioName = new Label();
    private Label radioArtist = new Label();
    private Label radioSong = new Label();
    private Label radioGenre = new Label();

很抱歉,代码太长,拼写错误,第一次发到这里,我不是英国人。

在每次迭代中,您都在重复使用控件(radioName和radioLogo)。因此,您只需更改现有控件的属性,而不是添加一大堆控件

您要做的是每次创建新控件。例如:

foreach (string l in radios)
{
    Label tempname = new Label();
    PictureBox templogo = new PictureBox();
    ....
另一方面,在foreach循环之前调用SuspendLayout()和在foreach循环之后调用ResumeLayout()会更有效