Search code examples
c#radio-buttonpanel

Dynamic radiobuttons in a dynamic panel in C#


I'm trying to make dynamicly created radiobuttons in my dynamicly created panel, but I'm not recieving what I'm trying to accomplish.

Here is my code:

private void Form1_Load(object sender, EventArgs e)
    {
        //Creating 3 panels
        int counTer = 3;
        for (int x = 0; x <= counTer; x++)
        {
            Panel panel = new Panel();
            panel.Name = "panel" + x;
            panel.Location = new Point(10 * (5 * x), 10);
            panel.Size = new Size(150, 275);
            //panel.BackColor = Color.Black; <-- Only for checking if they exist
            panel.Controls.Add(panel);

            //Creating 10 RadioButtons
            int hoeveelHeid = 10;
            for (int i = 0; i <= hoeveelHeid; i++)
            {
                RadioButton iets= new RadioButton();
                iets.Name = "Waarde" + i;
                iets.Text = "Waarde " + i;
                iets.Location = new Point(5, 20 * i);
                panel.Controls.Add(iets);
            }
        }
    }

I'm not recieving any panels nor radiobuttons, does anyone see the mistake i made?

Thanks.


Solution

  • You are trying to add the panel you created to it's OWN control collection:

    panel.Controls.Add(panel);
    

    which means add the panel to the panel.

    To add the panel to the form use:

    this.Controls.Add (panel);
    

    or even just:

    Controls.Add (panel);