Q. How do I make Form2 a child of Form1?
A. Use MDI
Q. And if I don't want to use MDI?
A. In VB you can't unless you use a MDI Parent/Child.
OR
Q. How do I make a sub form like in access?
A. Use MDI
...
Well... that is not quite true...
To play with this example start a new standard exe project, you will see Form1 displayed by default. Add a command button and a picture box to form1 (Command1, Picture1) and add the following code...
[tt]
Option Explicit
Private Const HWND_TOP = 0
Private Const SWP_NOMOVE = &H2
Private Const SWP_NOOWNERZORDER = &H200
Private Declare Function SetWindowPos Lib "user32" (ByVal hwnd As Long, ByVal hWndInsertAfter As Long, ByVal x As Long, ByVal y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As Long
Public IsForm2Visible As Boolean
Private Sub Form_Load()
Picture1.ScaleMode = vbPixels
End Sub
Private Sub Form_Resize()
Dim RetValue As Long
Command1.Left = 30
Command1.Top = 30
Picture1.Left = 30
Picture1.Top = 30 + (Command1.Height + Command1.Top)
Picture1.Width = Me.Width - 180
Picture1.Height = Me.Height - (450 + Picture1.Top)
If IsForm2Visible = True Then
RetValue = SetWindowPos(Form2.hwnd, HWND_TOP, 0, 0, Picture1.ScaleWidth, Picture1.ScaleHeight, SWP_NOMOVE + SWP_NOOWNERZORDER)
End If
End Sub
Private Sub Command1_Click()
Dim RetValue As Long
Load Form2
Form2.ShowMeAsChild Picture1.hwnd
RetValue = SetWindowPos(Form2.hwnd, HWND_TOP, 0, 0, Picture1.ScaleWidth, Picture1.ScaleHeight, SWP_NOMOVE + SWP_NOOWNERZORDER)
Form2.Show
End Sub
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
Dim F As Form
For Each F In Forms
Unload F
Next
End Sub
[/tt]
Now add a form (Form2). Change the following 2 properties of form2. Set the StartupPosition to Manual and optionally set the MaxButton to false (Optionally because when maximized the form will not resize correctly when the parent is resized.). Now add a command button to form2 (Command1) and add the following code to form2...
[tt]
Option Explicit
Private Declare Function SetParent Lib "user32" (ByVal hWndChild As Long, ByVal hWndNewParent As Long) As Long
Private Sub Form_Load()
Command1.Left = 100
Command1.Top = 100
End Sub
Public Sub ShowMeAsChild(ParentHwnd As Long)
Dim RetValue As Long
RetValue = SetParent(Me.hwnd, ParentHwnd)
Form1.IsForm2Visible = True
End Sub
Private Sub Form_Resize()
Me.Caption = "Resized to " & Me.Height & "," & Me.Width
End Sub
Private Sub Command1_Click()
Unload Me
Form1.IsForm2Visible = False
End Sub
[/tt]
Now for continuous forms like in access I will leave that up to the user to play with.
The above has been tested on Win 95 B and Win2k Pro SP2
Have fun and Good Luck