Results 1 to 13 of 13

Thread: how to use code to add a picture box to the form

  1. #1

    Thread Starter
    Member bamboosam's Avatar
    Join Date
    Feb 2001
    Posts
    61

    Smile how to use code to add a picture box to the form

    i have a frame contorl on my form, i want to use code to add a picture box in the frame, and can set the position of the picture box too.please help.
    _______________________
    VB 6 professional
    java2 SDK 1.3

  2. #2
    -= B u g S l a y e r =- peet's Avatar
    Join Date
    Aug 2000
    Posts
    9,629
    VB Code:
    1. Private Sub Command1_Click()
    2.     Dim ctlPB As Control
    3.     Set ctlPB = Controls.Add("VB.PictureBox", "PB1ox", Frame1)
    4.     ctlPB.Top = 160
    5.     ctlPB.Left = 60
    6.     ctlPB.Height = Frame1.Height - 220
    7.     ctlPB.Width = Frame1.Width - 200
    8.     ctlPB.Visible = True
    9. End Sub
    -= a peet post =-

  3. #3
    Frenzied Member moinkhan's Avatar
    Join Date
    Jun 2000
    Location
    Karachi, Pakistan
    Posts
    2,011
    Try This


    Private Declare Function SetParent Lib "user32" (ByVal hWndChild As Long, ByVal hWndNewParent As Long) As Long


    Private Sub Form_Load()
    Dim x As Control
    Set x = Controls.Add("VB.PictureBox", "Pic1", Frame1)
    x.Visible = True
    SetParent x.hWnd, Frame1.hWnd
    End Sub

  4. #4

    Thread Starter
    Member bamboosam's Avatar
    Join Date
    Feb 2001
    Posts
    61

    can you give me some explaination?

    thank you very much for you guys help,since i am not good at windows API , so i prefer the method which peet wrote.

    hi Peet:

    i hope you can give me some explaination,
    Set ctlPB = Controls.Add("VB.PictureBox", "PB1ox", Frame1)
    in this stament what is "PB1ox" mean?

    and in

    ctlPB.Height = Frame1.Height - 220
    ctlPB.Width = Frame1.Width - 200

    why height and width need to minus 220 and 200?

    thanks
    _______________________
    VB 6 professional
    java2 SDK 1.3

  5. #5
    -= B u g S l a y e r =- peet's Avatar
    Join Date
    Aug 2000
    Posts
    9,629
    VB Code:
    1. Private Sub Command1_Click()
    2.     Dim ctlPB As Control
    3.    
    4.     '"VB.PictureBox" = the type of control I want to add
    5.     '"PictureBox1" = the name of the control (I changed it :) )
    6.     'Frame1 = the container that I want the picture box placed in
    7.     Set ctlPB = Controls.Add("VB.PictureBox", "PictureBox1", Frame1)
    8.    
    9.     'place the picture box inside the Frame1, so that it does not
    10.     'cover the frame...
    11.     ctlPB.Top = 160
    12.     ctlPB.Left = 60
    13.     ctlPB.Height = Frame1.Height - 220
    14.     ctlPB.Width = Frame1.Width - 200
    15.    
    16.     'show the picturebox
    17.     ctlPB.Visible = True
    18. End Sub
    -= a peet post =-

  6. #6
    -= B u g S l a y e r =- peet's Avatar
    Join Date
    Aug 2000
    Posts
    9,629

    Re: can you give me some explaination?

    Originally posted by bamboosam
    ....

    ctlPB.Height = Frame1.Height - 220
    ctlPB.Width = Frame1.Width - 200

    why height and width need to minus 220 and 200?

    thanks
    doesnt have to minus those numbers, but if u want the picturebox not to overlap the frame that its in, u will have to do this.
    -= a peet post =-

  7. #7

    Thread Starter
    Member bamboosam's Avatar
    Join Date
    Feb 2001
    Posts
    61
    thanks for your explaination,now i understand very clearly.

    but now i have another problem

    i add this stamemt to the program " PictureBox1.Print ("hello") ", but it did not work

    Private Sub Command1_Click()
    Dim ctlPB As Control

    Set ctlPB = Controls.Add("VB.PictureBox", "PictureBox1", Frame1)
    ctlPB.Top = 160
    ctlPB.Left = 60
    ctlPB.Height = 500
    ctlPB.Width = 500
    ctlPB.Visible = True
    PictureBox1.Print ("hello")

    End Sub

    by the way, i also want to draw lines in the frame, and also need to set the position of the line, can u tell me how to do?

    thank you very much
    _______________________
    VB 6 professional
    java2 SDK 1.3

  8. #8
    -= B u g S l a y e r =- peet's Avatar
    Join Date
    Aug 2000
    Posts
    9,629
    VB Code:
    1. Option Explicit
    2.  
    3. Private ctlPB As Control
    4.  
    5. Private Sub Command1_Click()
    6.    
    7.     '"VB.PictureBox" = the type of control I want to add
    8.     '"PictureBox1" = the name of the control (I changed it :) )
    9.     'Frame1 = the container that I want the picture box placed in
    10.     Set ctlPB = Controls.Add("VB.PictureBox", "PictureBox1", Frame1)
    11.    
    12.     'place the picture box inside the Frame1, so that it does not
    13.     'cover the frame...
    14.     ctlPB.Top = 160
    15.     ctlPB.Left = 60
    16.     ctlPB.Height = Frame1.Height - 220
    17.     ctlPB.Width = Frame1.Width - 200
    18.    
    19.     'show the picturebox
    20.     ctlPB.Visible = True
    21. End Sub
    22.  
    23. Private Sub Command2_Click()
    24.     ctlPB.Print "Hello"
    25. End Sub
    -= a peet post =-

  9. #9
    Frenzied Member moinkhan's Avatar
    Join Date
    Jun 2000
    Location
    Karachi, Pakistan
    Posts
    2,011
    SEt the control's AutoRedraw Property to True

    ctlPB.AutoRedraw = True

  10. #10
    -= B u g S l a y e r =- peet's Avatar
    Join Date
    Aug 2000
    Posts
    9,629

    make a line in the frame from one corner to another

    VB Code:
    1. Private Sub Command3_Click()
    2.     Dim ctlLine1 As Control
    3.     Set ctlLine1 = Controls.Add("VB.Line", "Line1", Frame1)
    4.     ctlLine1.Visible = True
    5.     ctlLine1.X1 = 0
    6.     ctlLine1.X2 = Frame1.Width
    7.     ctlLine1.Y1 = 0
    8.     ctlLine1.Y2 = Frame1.Height
    9. End Sub
    -= a peet post =-

  11. #11

    Thread Starter
    Member bamboosam's Avatar
    Join Date
    Feb 2001
    Posts
    61
    hi peet:

    now i got another problem,now i want to remove all the controls
    which in the frame, but i can not find a way to do it,please help,thanks alot.
    _______________________
    VB 6 professional
    java2 SDK 1.3

  12. #12
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091
    VB Code:
    1. Dim c As Control
    2.  
    3.     For Each c In Controls
    4.         If c.Container Is Frame1
    5.             Controls.Remove c
    6.         End If
    7.     Next

    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  13. #13
    The picture isn't missing BuggyProgrammer's Avatar
    Join Date
    Oct 2000
    Location
    Vancouver, Canada
    Posts
    5,217
    instead of using the SetParent API you can just use
    set something.Container = Frame1
    Remember, if someone's post was not helpful, you can always rate their post negatively .

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