1

I am working on a little project and when i'm trying to generate a list from a array, the created control is overwritten after adding it so I can only see the last generated one. Is there something that i'm doing not correctly ? Here the code :

    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();

Sorry for the long code and for my spelling mistake, first time posting here and i'm not english.

1 Answer 1

3

You are reusing the controls (radioName and radioLogo) in each iteration. So instead of adding a whole bunch of controls, you are simply changing the properties of the existing controls.

What you'll want to do is create new controls each time. For example:

foreach (string l in radios)
{
    Label tempname = new Label();
    PictureBox templogo = new PictureBox();
    ....

On another note, it'll be more efficient to call SuspendLayout() before your foreach loop, and ResumeLayout() after your foreach loop.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.