-2

I want to add one of panel1,2,... into panel0 when I click on button1 .

enter image description here

It's my code

private void button3_Click(object sender, EventArgs e)
        {
            int i = 0;
            int j = 0;


            Panel[] pnl = new Panel[3];

            while ( i<2)
            {
                pnl[i].Parent = panel0;                  // erorr

                pnl[i].BackColor = Color.PeachPuff;

                pnl[i].Size = new Size(50, 10);
                pnl[i].Location = new Point(j+5,10);
                panel0.Controls.Add(pnl[i]);
                pnl[i].BringToFront();

                i++;
                j = j + 13;
            }
         }

enter image description here

Have a suggestion ?

slugster
  • 47,434
  • 13
  • 92
  • 138
jsmlz
  • 1
  • 3
  • Could you let us know what's the error you got? – Imantas Jan 02 '19 at 12:04
  • 2
    well, your panel hasn't initialized yet, you only call the array constructor, but you should still use `pnl[i] = new Panel()` before you can access its members. For the rest, you should choose between having it as parent, or assigning it to the controls of `panel0`, as both do pretty much the same – Icepickle Jan 02 '19 at 12:06
  • 1
    what technology is this? Winforms, WPF, webforms, Xamarin? – Liam Jan 02 '19 at 12:07
  • It's windows form – jsmlz Jan 02 '19 at 12:13

1 Answers1

0

change Panel[] pnl = new Panel[3] to:

var pnl  = new List<Panel>{existingPanel1, existingPanel2, existingPanel3};
CodeMan
  • 642
  • 5
  • 11