Setting form object properties via a forloop

This forum can be browsed by the general public. Posting is limited to current SAPIEN license holders with active maintenance and does not offer a response time guarantee.
Forum rules
DO NOT POST LICENSE NUMBERS, ACTIVATION KEYS OR ANY OTHER LICENSING INFORMATION IN THIS FORUM.
Only the original author and our tech personnel can reply to a topic that is created in this forum. If you find a topic that relates to an issue you are having, please create a new topic and reference the other in your post.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 11 years and 9 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
StupidSexyFlanders
Posts: 107
Last visit: Thu Apr 29, 2021 8:47 am

Setting form object properties via a forloop

Post by StupidSexyFlanders »

Not sure if my logic is wrong, or if this is even the proper way to do this. In any regard, it's not working...

I have an array like this:
$names = "$fee","$fii","$foe","$fum"

Then, I create a series of form objects (e.g. labels) with the same names:

$fee = New-Object 'System.Windows.Forms.Label'
$fii = New-Object 'System.Windows.Forms.Label'
and so on....

Then, I try using a forloop:

for($n=0;$n -lt $names.count;$n++)
{
if($something -eq $true)
{
$names[$n].BackColor = 'red'
}
else
{
$names[$n].BackColor = 'green'
}
}

Not sure if this can even be done. Whenever I try, it (predictably) says "$names[$n] does not contain a property called 'BackColor'".

My actual array is pretty big and I am trying to avoid having to manually create a bunch of if statements for each item.mbaker2012-07-09 14:19:40
Mike
User avatar
davidc
Posts: 5913
Last visit: Mon Jul 08, 2019 8:55 am
Been upvoted: 2 times

Setting form object properties via a forloop

Post by davidc »

The problem is the way you are declaring the array: $names = "$fee","$fii","$foe","$fum" In this case you are creating a list of strings instead of labels. Try this instead: $name = $fee, $fii, $foe, $fum David
David
SAPIEN Technologies, Inc.
StupidSexyFlanders
Posts: 107
Last visit: Thu Apr 29, 2021 8:47 am

Setting form object properties via a forloop

Post by StupidSexyFlanders »

But $fee equals new-object. When I go through the loop, calling $fee, won't it just try to create another new object and throw an error? I am going to try it and see....
Mike
User avatar
davidc
Posts: 5913
Last visit: Mon Jul 08, 2019 8:55 am
Been upvoted: 2 times

Setting form object properties via a forloop

Post by davidc »

When you put the variable in double quotes it becomes a string. David
David
SAPIEN Technologies, Inc.
StupidSexyFlanders
Posts: 107
Last visit: Thu Apr 29, 2021 8:47 am

Setting form object properties via a forloop

Post by StupidSexyFlanders »

Really? I thought that only applied to single-quotes. In any regard, still no dice. I changed my code to read this way:

$names = $fee,$fii,$foe,$fum
$fee = New-Object 'System.Windows.Forms.Label'
$fii = New-Object 'System.Windows.Forms.Label'
(and so on...)

for($n=0;$n -lt $names.count;$n++)
{
if($something -eq $true)
{
$names[$n].BackColor = 'red'
}
else
{
$names[$n].BackColor = 'green'
}
}

Still getting:
"Property 'BackColor' cannot be found on this object; make sure it exists and is settable."
Mike
User avatar
davidc
Posts: 5913
Last visit: Mon Jul 08, 2019 8:55 am
Been upvoted: 2 times

Setting form object properties via a forloop

Post by davidc »

Create the names array after creating the Labels objects otherwise you will have an array of empty (null) values. Just for future reference, ScriptingAnswers forums designated for scripting questions like this. David
David
SAPIEN Technologies, Inc.
StupidSexyFlanders
Posts: 107
Last visit: Thu Apr 29, 2021 8:47 am

Setting form object properties via a forloop

Post by StupidSexyFlanders »

That appeared to be the problem. Working like a champ now.
Another rookie mistake FTW!

Thanks!

I will take this type of thing to the other forum in the future. :)mbaker2012-07-09 16:24:03
Mike
This topic is 11 years and 9 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.