Page 1 of 1

One Click to rule them all?

Posted: Tue Oct 09, 2012 12:11 pm
by StupidSexyFlanders
As soon as my form loads it immediately checks the users current domain. If it's not an approved domain, all the form controls are disabled. Currently, I am doing an IF($ENV:USERDOMAIN -ne 'mydomain') and them manually enabling/disabling all the controls based on the results.

Is there a command that I can invoke that can set *all* the controls (or at least all the buttons) to 'Enabled = $false' ? I was thinking of adding all my control names to an array, and them doing a foreach...but i don't know if that's the best way (or even possible).

Thanks

One Click to rule them all?

Posted: Wed Oct 10, 2012 4:28 am
by davidc
First you must forge a function in the depths of Mount Doom that will iterate through all the controls and disable/enable them:

function Enable-Controls
{
param([bool]$Enabled, [System.Windows.Forms.Form]$Form)
foreach($control in $Form.Controls)
{
$control.Enabled = $Enabled
}
}

After you have forged this one function to disable them all you must wield it as thus:

Enable-Controls $false $form1

Note: Disabling the container control and it will disable all its children. For example, disabling a Groupbox will disable all the control contained within. You do not want to disable the form itself because the all windows system buttons such as the close X button will not work.

David

[PS. With a title like that you are just asking for a response like this ;-)]