|
-
Jul 11th, 2007, 09:38 PM
#1
Thread Starter
Member
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.
-
Jul 11th, 2007, 09:51 PM
#2
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
Last edited by nmadd; Jul 11th, 2007 at 10:03 PM.
Reason: my first code didn't make any sense.
-
Jul 11th, 2007, 10:26 PM
#3
Thread Starter
Member
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
-
Jul 11th, 2007, 10:37 PM
#4
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.
-
Jul 11th, 2007, 11:48 PM
#5
Thread Starter
Member
Re: Assigning an image to a control
Thanks. I guess thats all the info i would need on that.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|