6 Replies - 1015 Views - Last Post: 12 April 2012 - 09:01 AM Rate Topic: -----

#1 amulgarg   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 23-September 11

Initializing an array of Panels

Posted 12 April 2012 - 03:32 AM

I am trying to initialize an array of panels using this for-loop. The designer generates an error stating that the designer-generates code should not be modified.

for (int i = 0; i < 10; i++)
            {
                // this.panel1[i] = new System.Windows.Forms.Panel();
                this.SuspendLayout();
                // 
                // panel1
                // 
                this.panel1[i].Location = new System.Drawing.Point(35, 31);
                this.panel1[i].Name = "panel" + i;
                this.panel1[i].Size = new System.Drawing.Size(200, 204);
                this.panel1[i].TabIndex = 0;
                this.panel1[i].Visible = false;
            } 


Do I need to perform these initializations one by one without a loop?

Is This A Good Question/Topic? 0
  • +

Replies To: Initializing an array of Panels

#2 Robin19   User is offline

  • D.I.C Addict
  • member icon

Reputation: 272
  • View blog
  • Posts: 552
  • Joined: 07-July 10

Re: Initializing an array of Panels

Posted 12 April 2012 - 04:19 AM

Designer-generated code should never be modified. Visual Studio freely overwrites that file and you would lose all of your changes.

You can make them inside a loop, but what you have now would set them all to the same size and the same location. You would only be able to see the 10th one made.

I would make them in the designer and assign them in code.
void SetupPanelArray()
{
   panelArray = new Panel[10];
   panelArray[0] = panel0;
   ...
   panelArray[9] = panel9;
}

Was This Post Helpful? 0
  • +
  • -

#3 amulgarg   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 23-September 11

Re: Initializing an array of Panels

Posted 12 April 2012 - 05:36 AM

Does that mean I have to write code for all the panels separately?
Was This Post Helpful? 0
  • +
  • -

#4 negligible   User is offline

  • D.I.C Regular

Reputation: 62
  • View blog
  • Posts: 302
  • Joined: 02-December 10

Re: Initializing an array of Panels

Posted 12 April 2012 - 05:53 AM

(I wrote this post out wrong, decided just to remove it to avoid confusion. Hannibal's got it covered.)

This post has been edited by negligible: 12 April 2012 - 08:13 AM

Was This Post Helpful? 0
  • +
  • -

#5 h4nnib4l   User is online

  • The Noid
  • member icon

Reputation: 1458
  • View blog
  • Posts: 2,072
  • Joined: 24-August 11

Re: Initializing an array of Panels

Posted 12 April 2012 - 06:49 AM

The location isn't being set from i, it's being explicitly set to the exact same point for each. At least if it was using the counter, they wouldn't be in the exact same place. You don't necessarily have to write code for each panel, but you will need to do some math using your counter, based on where you want to place them. For example, if you want them the same size and side by side, you can use:
this.panel1[i].Location = new System.Drawing.Point(35 * (i + 1), 31);
.

EDIT: Grammar

This post has been edited by h4nnib4l: 12 April 2012 - 06:50 AM

Was This Post Helpful? 0
  • +
  • -

#6 amulgarg   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 23-September 11

Re: Initializing an array of Panels

Posted 12 April 2012 - 08:42 AM

Sorry, I forgot to mention that I need a panel[i+1] to replace a panel[i] at the very same location.
Was This Post Helpful? 0
  • +
  • -

#7 h4nnib4l   User is online

  • The Noid
  • member icon

Reputation: 1458
  • View blog
  • Posts: 2,072
  • Joined: 24-August 11

Re: Initializing an array of Panels

Posted 12 April 2012 - 09:01 AM

Ah, then Robin has it covered.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1