I am trying to dynamically add checkboxes to a panel depending on if a file exists. My code is:

While d1 <= d2
                If File.Exists("C:\bnb\" & m1 & "-" & d1 & "-" & y1) = False Then
                    rm1CheckBoxes(rm1I) = New CheckBox()
                    rm2CheckBoxes(rm2I) = New CheckBox()
                    rm1CheckBoxes(rm1I).Location = New Point(20, rm1y)
                    rm2CheckBoxes(rm2I).Location = New Point(60, rm2y)
                    rm1y = rm1y + 20
                    rm2y = rm2y + 20
                    MessageBox.Show(pnlResults.Controls.Count())
                    pnlResults.Controls.Add(rm1CheckBoxes(rm1I))
                    pnlResults.Controls.Add(rm2CheckBoxes(rm2I))
                    rm1I = rm1I + 1
                    rm2I = rm2I + 1
                    d1 = d1 + 1
                End If
            End While

But only the first pnlResults.Controls.Add() actually shows up in my form. If I reverse the two calls or comment out the first one, then I get x boxes at 60,rm2y, as is I get x boxes at 20,rm1y. I tried changing the second point to a constant 60,60, and that one checkbox did appear. I am just completely lost as to why only half of my checkboxes show up. I tried putting an Application.DoEvents call at the end of the if, but that didn't fix the problem. Any help would be greatly appreciated.

Dani AI

Generated

The behavior described (only one of two dynamically created CheckBoxes appearing, and reversing the Add calls swapping which set is visible) most commonly points to layout/positioning or control-reuse issues rather than File.Exists itself. 's off-screen suggestion and 's filename reminder are sensible first checks; the following checklist and quick diagnostics will help pin the actual cause.

  • Confirm the runtime values used for placement. Log or message the X/Y values used for each new CheckBox each iteration; identical locations will cause overlapping and make one set appear to "disappear."
  • Inspect the panel's scroll/clip state and the controls' Visible and Parent properties; a control outside panel bounds or with Visible=False will not show.
  • Ensure each CheckBox instance is actually a distinct object and that arrays/lists are not accidentally aliasing the same object or being overwritten. If On Error Resume Next is in effect, errors (index out of range, etc.) can be silently skipped.
  • After the loop, enumerate the panel's Controls collection and print each control's Type, Name, Location, Size, and Visible to confirm what was added and where.

Quick diagnostic snippet (run after additions):

For Each c As Control In pnlResults.Controls
    Debug.WriteLine(String.Format("{0} Name={1} Loc={2} Size={3} Visible={4}",
        c.GetType().Name, c.Name, c.Location, c.Size, c.Visible))
Next

Practical fixes: give each CheckBox a unique Name/Text so they are easy to identify in the debug output; call BringToFront on controls that must be visible above others; enable AutoScroll on the panel if controls extend past its client area. For simpler layout and fewer position bugs, consider a FlowLayoutPanel (it arranges added controls automatically) — see the FlowLayoutPanel docs for usage details (FlowLayoutPanel control).

If the diagnostic log shows the second set never being created (no entries), focus on the array/list allocation and any error handling that might be hiding exceptions.

Recommended Answers

All 2 Replies

I dont know what the values are that you are passing. Are you sure that the control is not being added off the sides of the form? Run your control count after the loop to see.

Check the filenames.

While d1 <= d2
        If File.Exists("....") = False Then
            ........
        End If
  d1 = d1 + 1
  End While
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.