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.
Printable View
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 Code:
Private Sub Command1_Click() Dim ctlPB As Control Set ctlPB = Controls.Add("VB.PictureBox", "PB1ox", Frame1) ctlPB.Top = 160 ctlPB.Left = 60 ctlPB.Height = Frame1.Height - 220 ctlPB.Width = Frame1.Width - 200 ctlPB.Visible = True End Sub
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
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 Code:
Private Sub Command1_Click() Dim ctlPB As Control '"VB.PictureBox" = the type of control I want to add '"PictureBox1" = the name of the control (I changed it :) ) 'Frame1 = the container that I want the picture box placed in Set ctlPB = Controls.Add("VB.PictureBox", "PictureBox1", Frame1) 'place the picture box inside the Frame1, so that it does not 'cover the frame... ctlPB.Top = 160 ctlPB.Left = 60 ctlPB.Height = Frame1.Height - 220 ctlPB.Width = Frame1.Width - 200 'show the picturebox ctlPB.Visible = True End Sub
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.Quote:
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
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 Code:
Option Explicit Private ctlPB As Control Private Sub Command1_Click() '"VB.PictureBox" = the type of control I want to add '"PictureBox1" = the name of the control (I changed it :) ) 'Frame1 = the container that I want the picture box placed in Set ctlPB = Controls.Add("VB.PictureBox", "PictureBox1", Frame1) 'place the picture box inside the Frame1, so that it does not 'cover the frame... ctlPB.Top = 160 ctlPB.Left = 60 ctlPB.Height = Frame1.Height - 220 ctlPB.Width = Frame1.Width - 200 'show the picturebox ctlPB.Visible = True End Sub Private Sub Command2_Click() ctlPB.Print "Hello" End Sub
SEt the control's AutoRedraw Property to True
ctlPB.AutoRedraw = True
VB Code:
Private Sub Command3_Click() Dim ctlLine1 As Control Set ctlLine1 = Controls.Add("VB.Line", "Line1", Frame1) ctlLine1.Visible = True ctlLine1.X1 = 0 ctlLine1.X2 = Frame1.Width ctlLine1.Y1 = 0 ctlLine1.Y2 = Frame1.Height End Sub
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 Code:
Dim c As Control For Each c In Controls If c.Container Is Frame1 Controls.Remove c End If Next
:)
instead of using the SetParent API you can just use
set something.Container = Frame1