Support Forums

Using variable in a control reference variable

Archived support forum for customers who once purchased a PrimalForms product license. This forum is locked.
User avatar
kryten
Posts: 44
Joined: Sat Jan 03, 2009 11:10 pm

Using variable in a control reference variable

Postby kryten » Mon Nov 05, 2012 8:40 am

I should probably know this by now, but been wrestling with it for a while on and off without a clear answer so would appreciate any suggestions.

Suppose I have a form with many controls, but at the beginning a lot of them are hidden and only become visible if the user interactive choices go that way.

With this application I need to take the user input, do a bit of crunching and get a result. The result gives me an integer where the integer is the number of (lets say) a Picturebox control that I now want to make visible. My problem is that I can't seem to find an easy way to reference that Picturebox control using the name of the variable that contains my integer.

Eg:-

Suppose I have three Picturebox controls. Each of them is .Visible = 0

Now I do:-

$i = get-random (1,2,3) # $i is the number of the Picturebox I want to make .Visible = 1

$picturebox$i.Visible = 1
($picturebox)$i.Visible = 1
$($picturebox$i).Visible = 1
$picturebox$($i).Visible = 1

Even $a = $picturebox & "$i"

All crash out.

See what I mean?

Would appreciate any suggestions.

Regards,
Stuart
User avatar
kryten
Posts: 44
Joined: Sat Jan 03, 2009 11:10 pm

Using variable in a control reference variable

Postby kryten » Mon Nov 05, 2012 8:46 am

Should mention that this _can_ be done with New-Variable but its a bit onerous in a large script. Is there any other way?
User avatar
davidc
Posts: 4354
Joined: Thu Aug 18, 2011 4:56 am

Using variable in a control reference variable

Postby davidc » Mon Nov 05, 2012 9:43 am

There are various ways you can do this. One way of doing this is to create an array that references the picture boxes in the Load event:

$script:pictureBoxes = $pictureBox1, $pictureBox2, etc.

Then reference the array to make them visible.

$script:pictureBoxes[$i].Visible = $true

David
David
SAPIEN Technologies, Inc.

Return to “PrimalForms”

Who is online

Users browsing this forum: No registered users and 1 guest