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