Using variable in a control reference variable

Archived support forum for customers who once purchased a PrimalForms product license. This forum is locked.
This topic is 11 years and 4 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.
User avatar
kryten
Posts: 45
Last visit: Fri Dec 25, 2020 7:46 am

Using variable in a control reference variable

Post by kryten »

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: 45
Last visit: Fri Dec 25, 2020 7:46 am

Using variable in a control reference variable

Post by kryten »

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: 5913
Last visit: Mon Jul 08, 2019 8:55 am
Been upvoted: 2 times

Using variable in a control reference variable

Post by davidc »

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.
This topic is 11 years and 4 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.