C# novice here. Creating a little app that will monitor whether there's sufficient space on a backup drive to accommodate scheduled backups. After local drive discovery a window will be shown which lists found drives with associated details per the depicted "mock-up":
NOTE: mock-up detail controls are Visible = false;.
The objective is to dynamically build the details at run-time based on discovered disks enabling the user to select the drives to monitor, whether the monitored drive is a backup destination or source and enter a n historical backup compression factor. When run I'm only getting the first defined control:
Following is the code:
private readonly static List<Drives4DB> FoundDrives = ConnectionConfig.Connects.GetDrives();
private void SetupMonitoring_Load(Object sender, EventArgs e)
{
int drvs = FoundDrives.Count;
CheckBox[] drvChkBx;
Label[] nameLBL;
Label[] labelLBL;
ComboBox[] roleCBX;
Label[] typeLBL;
Label[] sizeLBL;
Label[] freeLBL;
Label[] usedLBL;
MaskedTextBox[] factor;
drvChkBx = new CheckBox[drvs];
nameLBL = new Label[drvs];
labelLBL = new Label[drvs];
// etc.
int drvChkBxStart_X = 22;
int drvChkBxStart_Y = 142;
int nameLBLstart_X = 42;
int nameLBLstart_Y = 144;
int labelLBLstart_X = 60;
int labelLBLstart_Y = 144;
// etc.
Console.WriteLine($"\n\n \"{drvs}\" internal drives found\n");
int vertSpace = 0;
for (int i = 0; i < drvs; i++)
{
foreach (var drv in FoundDrives)
{
if (drv.id_DrvBase == i + 1)
{
drvChkBx[i] = new CheckBox();
drvChkBx[i].Name = $"{drvChkBx}{i}";
nameLBL[i] = new Label();
nameLBL[i].Name = $"{nameLBL}{i}";
nameLBL[i].Text = $"{drv.name}";
// etc.
}
}
}
for (int i = 0; i < drvs; i++)
{
drvChkBx[i].Enabled = true;
drvChkBx[i].Visible = true;
drvChkBx[i].Location = new Point(drvChkBxStart_X, drvChkBxStart_Y + vertSpace);
nameLBL[i].Enabled = true;
nameLBL[i].Visible = true;
nameLBL[i].Location = new Point(nameLBLstart_X, nameLBLstart_Y + vertSpace);
labelLBL[i].Enabled = true;
labelLBL[i].Visible = true;
labelLBL[i].Location = new Point(labelLBLstart_X, labelLBLstart_Y + vertSpace);
// labelLBL[i].Size = new Size(125, 20);
this.Controls.Add(drvChkBx[i]);
this.Controls.Add(nameLBL[i]);
this.Controls.Add(labelLBL[i]);
vertSpace += 25;
}
}
NOTE: The example code found breaks the creation of the controls and display definition into two separate "for" loops but I don't understand why. I've tried this both as presented and using a single "for" with the same result.
All expected data is available (output from WriteLine):
"8" internal drives found
C:\ 00 NVMe_fresh_OS
D:\ 02 Data
E:\ 03 Games
F:\ 04 Internal Backups
G:\ 07 Virtual PCs
H:\ 05 NVMe Wrkg Space
J:\ 01 SATA_upgrade_OS
K:\ 06 SATA Wrkg Space
Checking control array content via debug/breakpoint verifies the arrays have the correct elements.
Why is this only picking up/showing the first control of a row?[
Thanx in advance for yur help.