Results 1 to 9 of 9

Thread: How do you create an image box with code?

  1. #1

    Thread Starter
    Junior Member
    Join Date
    May 2005
    Posts
    31

    How do you create an image box with code?

    Hello, I'm new to these forums. My name is Josh, and my alias is obviously "The Pinball Wizard", after the The Who song. Anyway, I was wondering if there was a way to create an image box and place it somewhere with code. Kind of like the "ctrl-D" duplicate function of most windows programs. Also, since this is the first year I've been taking VB, and my teacher doesn't know a lot of the more advanced things, please elaborate on some of the more advanced code (if there is any), thanks.

  2. #2
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: How do you create an image box with code?

    Welcome to VBF, Josh!

    Place command button onto your form and try to replcate this sample:
    VB Code:
    1. Private Sub Command1_Click()
    2. Dim img As Control
    3.  
    4.     Set img = Controls.Add("VB.Image", "imgTemp")
    5.     img.Move 300, 300
    6.     img.Picture = LoadPicture("c:\temp\some_image.bmp")
    7.     img.Visible = True
    8.  
    9. End Sub
    As per advanced coding technics - I would suggest to take one step at the time - it'll come to you when you're ready for it.

  3. #3
    Next Of Kin baja_yu's Avatar
    Join Date
    Aug 2002
    Location
    /dev/root
    Posts
    5,989

    Re: How do you create an image box with code?

    Using Control Arrays. You can do it with any control. Set it's index property to zero (0) at design time.

    then at runtime do something like this:

    VB Code:
    1. Load imgImage(1)
    2. imgImage(1).Visible = True
    3. imgImage(1).Left = 100
    4. imgImage(1).Top = 100

  4. #4

    Thread Starter
    Junior Member
    Join Date
    May 2005
    Posts
    31

    Re: How do you create an image box with code?

    I understand (for the most part) both of those messages, thanks a lot! I have a few questsions for RhinoBull though.

    What is "Control"?
    What is the .Add function?
    What is the .Move function?

  5. #5
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: How do you create an image box with code?

    1.
    Every button, textbox, label, picturebox and others are CONTROLS.
    Also every control is an object, so Dim img As Control is a declaration of an object variable of a Control type.

    2.
    Add is method of Controls collection that allows adding new instance of some control at run time.

    3.
    Move is method of a specified control that positions ("moves") object to a specified location: in my sample img.Move 300, 300 is the sample as img.Left = 300 and img.Top = 300

  6. #6

    Thread Starter
    Junior Member
    Join Date
    May 2005
    Posts
    31

    Re: How do you create an image box with code?

    Thanks for all those explanations, I'll keep those in mind. Your code didn't work, though. It came up with an error saying "Object Required", then it pointed to the line:

    set img = control.add ("VB_image", "img_temp")

  7. #7
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: How do you create an image box with code?

    What you have IS NOT what I gave you, so take a look one more time and try to replicate it.

  8. #8

    Thread Starter
    Junior Member
    Join Date
    May 2005
    Posts
    31

    Re: How do you create an image box with code?

    Oh, sorry about that, it works now. Thanks a lot! I need this code for my end of the year school project, so you really helped.

  9. #9
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: How do you create an image box with code?

    That sounds better.

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