So I have a PowerShell tool that uses WinForms GUI.
The GUI has one main flowlayoutpanel that in turn has roughly dozen flowlayout panels as child-items. These flowlayoutpanels contain groupboxes that in turn contain other controls.
These groupboxes are by default disabled and not visible. Enabled and visible states are controlled by other parameters as well as user selections.
The code that determines which objects should be enabled and visible:
ForEach ($Entry in $EnableCommonObjects) {
[string]$Enabled = '$'+$Entry+'.Enabled = $false'
[string]$Visible = '$'+$Entry+'.Visible = $false'
Invoke-Expression "$Enabled"
Invoke-Expression "$Visible"
}
ForEach ($Entry in $EnableUncommonObjects) {
If ($tsbEnableUncommonObjects.Checked -eq $true) {
[string]$Enabled = '$'+$Entry+'.Enabled = $true'
[string]$Visible = '$'+$Entry+'.Visible = $true'
} Else {
[string]$Enabled = '$'+$Entry+'.Enabled = $false'
[string]$Visible = '$'+$Entry+'.Visible = $false'
}
Invoke-Expression "$Enabled"
Invoke-Expression "$Visible"
}
There are quite a few code blocks like that. The latter code block checks whether toolstrip button (tsbEnableUncommonObjects) is checked, if it is then the code also makes uncommon objects visible to the user. This works like a charm.
To further organize the layout I wanted to use flowbreaks in most but not all of the panels. Unfortunately now panels that have no visible content and have flowbreaks enabled leave a gap between two visible panels. The gap seems to be constant no matter how many panels with invisible content is between two panels with visible content.
All the panels already had minimum size set to 0, no margins or padding. These leave no gaps between panels, unless I use flowbreaks. I suppose this to be expected behavior when using flowbreaks.
Well no big deal I thought, I'll just make any panels themselves invisible that have zero visible groupboxes. Now this is where things go awry.
I decided to build a convenient loop to check visibility state of each groupbox.
ForEach ($FlowLayoutPanel in $flpMain.Controls) {
[bool]$VisibleControls = $false
ForEach ($Control in $FlowLayoutPanel.Controls) {
If ($Control.Visible) {
$VisibleControls = $true
}
}
If ($VisibleControls) {
$FlowLayoutPanel.Visible = $true
} Else {
$FlowLayoutPanel.Visible = $false
}
}
The loop works, it finds all panels and all groupboxes within the panels, It is also successful at checking each groupbox's visible state.
The problem is that the visible states are not correct. This loop runs after the code that determines which common and uncommon groupboxes are visible and which are not. Yet the loop finds visible states that match only common groupboxes. It is like user never clicked the toolstrip button that enables uncommon groupboxes too.
The panels that contained both common and uncommon objects are visible and both object types are shown. But the panels that contained only uncommon objects are still invisible. If I comment out this flowlayoutpanel code block, everything is back to normal.
Then I made a little test. I outputted visible state of one of the uncommon groupboxes to a text file while I had the flowlayoutpanel loop commented out. As expected, false and true states matched what user selects in the toolstrip button.
However, when I uncommented the flowlayoutpanel loop and repeated the test, the groupbox's visible state never changed from false. I'm baffled, what the heck is happening here?