Hello,

I am currently trying to develop a window form application whose content is added dynamically. What I am trying to do here is to display a number of panels with different sources across the screen horizontally. I am getting the data from a DB and depending on the number of sources (collections) through a foreach loop a number of panels is printed on screen. Only the first panel is being diplayed somehow and I cannot figure out how to show the other panels.

This is my code so far:

List<string> collectionName = crudObj.getCollectionNames();

            _horizontalIter = 0;
            _panelIter = 0;            

            foreach (string colName in collectionName)
            {
                // Panel
                Panel pnlCollection = new Panel();
                pnlCollection.Name = "pnl" + colName;
                pnlCollection.TabIndex = 0;
                pnlCollection.Width = 250;
                pnlCollection.Location = new Point(_panelIter + 2, pnlButtons.Height + 10);
                pnlCollection.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;


                List<string> stationName = crudObj.getStationNamesByCollection(colName);

                foreach (string statName in stationName)
                {
                    // Form controls
                    Label lblCollectionHeader = new Label();
                    Label lblStationName = new Label();
                    Label lblStationStatus = new Label();
                    Label lblStationToggle = new Label(); 

                    // Control dimensions
                    lblCollectionHeader.Width = 130;
                    lblStationName.Width = 80;
                    lblStationStatus.Width = 60;
                    lblStationToggle.Width = 60;

                    // Control text format
                    lblCollectionHeader.Font = new Font("MS Sans Seriff", 13, FontStyle.Bold);
                    lblStationName.Font = new Font("MS Sans Seriff", 11, FontStyle.Regular);
                    lblStationStatus.Font = new Font("MS Sans Seriff", 11, FontStyle.Regular);
                    lblStationToggle.Font = new Font("MS Sans Seriff", 11, FontStyle.Regular);

                    // Control location                    
                    lblCollectionHeader.Location = new Point(pnlCollection.Location.X + 70, 5);
                    lblStationName.Location = new Point(pnlCollection.Location.X + 5,
                        60 + 2 * _horizontalIter * lblStationName.Height);
                    lblStationStatus.Location = new Point(lblStationName.Width + 20,
                        60 + 2 * _horizontalIter * lblStationName.Height);
                    lblStationToggle.Location = new Point(lblStationStatus.Width + 100,
                        60 + 2 * _horizontalIter * lblStationName.Height);

                    // Contol naming and text assignment
                    lblCollectionHeader.Name = "lblCollectionHeader";
                    lblCollectionHeader.Text = colName;
                    lblStationName.Name = "lbl" + statName;
                    lblStationName.Text = statName;
                    lblStationStatus.Name = "lbl" + statName + "Status";
                    lblStationStatus.Text = "OFF";
                    lblStationToggle.Name = "lbl" + statName + "Toggle";
                    lblStationToggle.Text = "RED";

                    pnlCollection.Height = (lblCollectionHeader.Height + lblStationName.Height 
                        + lblStationStatus.Height + lblStationToggle.Height + 10) * 2;

                    // Add controls on screen 
                    pnlCollection.Controls.Add(lblCollectionHeader);
                    pnlCollection.Controls.Add(lblStationName);
                    pnlCollection.Controls.Add(lblStationStatus);
                    pnlCollection.Controls.Add(lblStationToggle);
                    this.Controls.Add(pnlCollection);                    
                    //this.Refresh();

                    _horizontalIter++;
                }

                _panelIter += pnlCollection.Width;                

                // Add controls on screen                
                //this.Controls.Add(pnlCollection);                
            }

Some help is greatly appreciated.

Thank you

Johan

_horizontalIter is defined as zero and I see no change in it? so e.g. on line 42 the multiplication stays zero. I that the intention?
What is crudObj?

The horizontalAlter is used to place controls beside each other horizontally. crudObj is the instance of the class (object). It consists of various methods that get/save data in the DB.

Could you tell me where pnlButtons.Height(line 13) comes from?

Thats a panel at the top left of the screen that has a home button as content. Code:

// Panel to group buttonS
Panel pnlButtons = new Panel();
pnlButtons.Name = "pnlButtons";
pnlButtons.BackColor = SystemColors.GradientInactiveCaption;
pnlButtons.Width = 200;
pnlButtons.Height = 48;
pnlButtons.Location = new Point(2, 6);
pnlButtons.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;

With a little modified code I became the following:

private void Form1_Load(object sender, EventArgs e)
        {
            List<string> collectionName = new List<string> { "collection1", "collection2","collection3" };
            int _horizontalIter = 0;
            int _panelIter = 0;
            foreach (string colName in collectionName)
            {
                // Panel
                Panel pnlCollection = new Panel();
                pnlCollection.Name = "pnl" + colName;
                pnlCollection.TabIndex = 0;
                pnlCollection.Width = 250;
                pnlCollection.Location = new Point(_panelIter + 2, 20+10);//(*20=pnlButtons.Height *)
                pnlCollection.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
                List<string> stationName = new List<string> { "station1", "station2","station3" };
                foreach (string statName in stationName)
                {
                    // Form controls
                    Label lblCollectionHeader = new Label();
                    Label lblStationName = new Label();
                    Label lblStationStatus = new Label();
                    Label lblStationToggle = new Label();
                    // Control dimensions
                    lblCollectionHeader.Width = 130;
                    lblStationName.Width = 80;
                    lblStationStatus.Width = 60;
                    lblStationToggle.Width = 60;
                    // Control text format
                    lblCollectionHeader.Font = new Font("MS Sans Seriff", 13, FontStyle.Bold);
                    lblStationName.Font = new Font("MS Sans Seriff", 11, FontStyle.Regular);
                    lblStationStatus.Font = new Font("MS Sans Seriff", 11, FontStyle.Regular);
                    lblStationToggle.Font = new Font("MS Sans Seriff", 11, FontStyle.Regular);
                    // Control location                    
                    lblCollectionHeader.Location = new Point(pnlCollection.Location.X + 70, 5);
                    lblStationName.Location = new Point(pnlCollection.Location.X + 5,
                        60 + 2 * _horizontalIter * lblStationName.Height);
                    lblStationStatus.Location = new Point(lblStationName.Width + 20,
                        60 + 2 * _horizontalIter * lblStationName.Height);
                    lblStationToggle.Location = new Point(lblStationStatus.Width + 100,
                        60 + 2 * _horizontalIter * lblStationName.Height);
                    // Contol naming and text assignment
                    lblCollectionHeader.Name = "lblCollectionHeader";
                    lblCollectionHeader.Text = colName;
                    lblStationName.Name = "lbl" + statName;
                    lblStationName.Text = statName;
                    lblStationStatus.Name = "lbl" + statName + "Status";
                    lblStationStatus.Text = "OFF";
                    lblStationToggle.Name = "lbl" + statName + "Toggle";
                    lblStationToggle.Text = "RED";
                    pnlCollection.Height = (lblCollectionHeader.Height + lblStationName.Height
                        + lblStationStatus.Height + lblStationToggle.Height + 10) * 2;
                    // Add controls on screen 
                    pnlCollection.Controls.Add(lblCollectionHeader);
                    pnlCollection.Controls.Add(lblStationName);
                    pnlCollection.Controls.Add(lblStationStatus);
                    pnlCollection.Controls.Add(lblStationToggle);
                    this.Controls.Add(pnlCollection);
                    //this.Refresh();
                    _horizontalIter++;
                }
                _panelIter += pnlCollection.Width;
                // Add controls on screen                
                this.Controls.Add(pnlCollection);                
            }
        }

cf6205925ea2f7fdd48887bb408bfa17
Here it is I made a mistake during upload.

Thanks for the suggestions.

My problem is that only the first panel is printed on screen. I want the whole lot (about 15) to be printed.

Output attached

Do you mean you only got one square printed or do you get a printout like your attachment?

No the output is the same as in the print out. The desired output should be all collections (panels) printed on screen

Miauw! Think I found it! Example line 40 of your original code:
Change lblCollectionHeader.Location = new Point(pnlCollection.Location.X + 70, 5); int
lblCollectionHeader.Location = new Point(0 + 70, 5);( now it are local coordinates of the Panel, not the Form)
Do the same for the rest of the Labels.