On Oct 11, 9:18 pm, Kuma <kumasa...@xxxxxx> wrote:
> I am creating a [system.windows.forms.form] object with the following.
> [Void]
> [System.Reflection.Assembly]::Loadwithpartialname("System.Windows.Forms")
> [Void]
> [System.Reflection.Assembly]::Loadwithpartialname("System.Drawing")
> $form = new-object system.windows.forms.form
> $form.size = new-object system.drawing.size(1280,1024)
>
> Then I need to add 10 labels all with the same properties except for
> text and location. I know I can just type them all out but I would
> rather use a for loop like so.
>
> for($i=0;$i -lt 10;$i++)
> {
> set-variable -name "label$i" -value (new-object
> system.windows.forms.label)
> }
>
> I can create the labels with this, I have checked the variables and
> they are indeed labels with all the appropriate properties, methods,
> etc. What I can't figure out is how to reference them in the same for
> loop like so.
>
> for($i=0;$i -lt 10;$i++)
> {
> set-variable -name "label$i" -value (new-object
> system.windows.forms.label)
> $label$i.size = new-object system.drawing.size(140,30)
> $label$i.set_backcolor('0')
> }
> I have tried every possible combination of "",' ', (), {}, and + that
> i can think of, but i can't get it to accept $label$i in any form as a
> variable. Any thoughts or am I doomed to typing them all out one at a
> time?
>
> Thanks,
>
> Kuma Kuma,
I would recommend using an array:
$labels = @()
for ( $i = 0; $i -lt 10; $i++ )
{
$label = New-Object System.Windows.Forms.Label
$label.Size = New-Object System.Drawing.Size( 140, 30 )
$label.BackColor = [System.Drawing.Color]::Orange
$labels += $label
}
After the loop, you can just subscript into the array to get the label
you want:
$labels[ 0 ].Text = "I'm a label!"
Good luck.
Jeff