Results 1 to 5 of 5

Thread: Assigning an image to a control

  1. #1

    Thread Starter
    Member
    Join Date
    Jun 2007
    Posts
    42

    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.

  2. #2
    Registered User nmadd's Avatar
    Join Date
    Jun 2007
    Location
    U.S.A.
    Posts
    1,676

    Re: Assigning an image to a control

    Perhaps something like this totally untested code to loop through the picture boxes?

    vb.net Code:
    1. ' Set my images.
    2.         For i As Integer = 0 To Me.Panel1.Controls.Count - 1
    3.             ' Test for the correct index in here.
    4.             DirectCast(Me.Panel1.Controls(i), PictureBox).Image = ' Some image.
    5.         Next i
    Last edited by nmadd; Jul 11th, 2007 at 10:03 PM. Reason: my first code didn't make any sense.

  3. #3

    Thread Starter
    Member
    Join Date
    Jun 2007
    Posts
    42

    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

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Member
    Join Date
    Jun 2007
    Posts
    42

    Resolved 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
  •  



Click Here to Expand Forum to Full Width