Assigning an image to a control
I am hoping someone responds quicker then I can think this through or find the info to it on the net.
I have a panel that contains ONLY pictureboxes. I need to use the index of Controls and i want to assign an image directly to the Control not the container. How would i do this.
For
example: Panel.controls(0).Image = MyImage. I know this is wrong but i can think of the right way this should be done.
thanks to anyone that can provide a quick response.
Re: Assigning an image to a control
Perhaps something like this totally untested code to loop through the picture boxes?
vb.net Code:
' Set my images.
For i As Integer = 0 To Me.Panel1.Controls.Count - 1
' Test for the correct index in here.
DirectCast(Me.Panel1.Controls(i), PictureBox).Image = ' Some image.
Next i
Re: Assigning an image to a control
I got it to work with CType. I wonder what we be most sound Ctype or Direct Cast. Code is very similiar to what you used. Does CType cause more code to be executed?
With Ctype(MyPanel.Controls(RandomNumber))
.Image= MyImage
.Location = New point(...,...)
End With
Re: Assigning an image to a control
CType is less efficient than DirectCast because it does type-checking and will perform a conversion if necessary and possible. If you know that no conversion is necessary then there's no point allowing for one.
Having said all that, I would simply create an array of PictureBoxes and use that, rather than the much less efficient method of going via the Controls collection and casting each time.
Re: Assigning an image to a control
Thanks. I guess thats all the info i would need on that.