Reputation: 703
How can I list all the controls in my form where (name)-property begins with something spesific and then use it in a foreach?
I have multiple groupboxes
under multiple tabpages
where the name
starts with gbs
(Example: gbs1, gbs2, gbs3 ++). Now I want to disable all the groupeboxes that begins with gbs*
at say a click of a botton without having to list all the groupboxes manually.
I've tried to look it up, but I can't find good documentation on it. I might be searching for the wrong words...
I'm guessing this is a start, and it's as far as I've come, but I'm not sure where to go from here or if I'm way off;
$list = @($MainForm.Controls.Find -like 'gbs*')
foreach ($item in $list){$item.enabled = $false} #Just a example of what I'm thinking
Purpose: I am creating a .exe form that is going to automate active directory. The form contains a bunch of settings, and all of them is within these groupboxes. I’m going to have a ‘edit-mode’, so that when it’s active - the groupboxes are enabled. When not, disabled.
Solution, thanks to help from @Clijsters :
#Get all the tabpages
$script:tabpages = $tabTasks.Controls | Where-Object { $_.Name -like "tp*" } | select Name, Controls
$script:tabpages += $tabSettings.Controls | Where-Object { $_.Name -like "tp*" } | select Name, Controls
#Get groupboxes in tabpages
$script:groupboxes = [System.Object]$tabpages.Controls
#Disable
$groupboxes | Where-Object { $_.Name -like "gbs*" } | ForEach-Object { $_.Enabled = $true }
Upvotes: 1
Views: 5267
Reputation: 4256
For finding Items in a list, Where-Object
is your CmdLet of choice!
In your case something like
$list = $MainForm.Controls | Where-Object {$_.Name -like "gbs*"}
will work fine. You can store the result in a variable (like shown above) and use it in foreach
or pipe the resulting list directly to ForEach-Object
and process it there like below:
$MainForm.Controls | Where-Object {$_.Name -like "gbs*"} | ForEach-Object {Do Something}
Upvotes: 3