Link to home
Start Free TrialLog in
Avatar of ndroo
ndroo

asked on

Embeding a form within a form

I would like to create a form which can 'contain' another form in VB.Net. For example, I have a main form, and would like to create a rectange in it (eg. panel) which I and open another form in. Well, it probably acts like a MDI parent, just that my child forms will be 'trapped' in a fixed position (and I can dynamically) change the child form to display. Any idea?
ASKER CERTIFIED SOLUTION
Avatar of bobbit31
bobbit31
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Are these forms pre-created?  If not, maybe you can simply use a frame or picturebox.

I recently did something like this in VB6, but used frames (it was originally MDI.) I then set up some simple drag-drop code so the frames could be dragged by the user to any desired location.
I wrote a class that allows you to attach one form to another form either between forms in the same application, or even forms from other applications, provided you can get the window handle.

The technique is this.  Let's say that you main form has a picturebox in the location where you want the child form to appear (let's call the picturebox the parent window).  You pass the window handle (hwnd) of the picturebox to this class and the class Hooks the messages that get sent to the picturebox (a picturebox is also a type of window).  Then, you load a form, either from the same application or a different application, and pass it's window handle to the class.  The class then sizes and positions the child window in the exact location of it's parent, and moves it on top.  

Anytime the parent window gets a message such as move or resize, the class intercepts and processes the message by moving or resizing the child window accordingly.

If, in your main form, you have code for resizing the picturebox whenever the main form gets resized, then the child window should resize or move along with the parent window (picturebox).  

It works pretty well, but it was mainly designed to attach windows that are displayed by DLLs inside of some parent window in the main EXE.  It may be overkill for what you need.
Avatar of ndroo
ndroo

ASKER

Thanks for your prompt comments. I'll try it out and let u guys know the result. Appreciate it. Thanks.
Avatar of ndroo

ASKER

bobbit31, I can't use the hWnd property for both the form and frame. Does VB.Net support it? Or has it been changed to something else?

mdougan, any sample code? I just need something simple.
you can do it using api:

Private Declare Function SetParent Lib "user32" (ByVal hWndChild As Long, ByVal hWndNewParent As Long) As Long
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long

Private Sub Command1_Click()
    Dim form1Hwnd As Long
    Dim form2Hwnd As Long
    Dim frameHwnd As Long
   
    Form2.Show
    Form2.Left = 0
    Form2.Top = 0
   
    form1Hwnd = FindWindow(vbNullString, "Form1")
    form2Hwnd = FindWindow(vbNullString, "Form2")
    frameHwnd = FindWindowEx(form1Hwnd, 0, "ThunderFrame", "Frame1")
   
    SetParent form2Hwnd, frameHwnd
End Sub
Well, simple it's not.  I do have a sample project, but it would be better if you posted an e-mail address where I could send the sample project.
Avatar of ndroo

ASKER

bobbit31 : thanx for your example. but what is "ThunderFrame" and "Frame1"? What should I replace them with?

 frameHwnd = FindWindowEx(form1Hwnd, 0, "ThunderFrame", "Frame1")
Avatar of ndroo

ASKER

mdougan, I will try out bobbit31's sample first. if i still have problem, then i probably post my email address. thanks and appreciate your help.
"ThunderFrame" is the class name for visual basic frame
"Frame1" is your frame name

so if you name your frame frameParent, it would look like this:
frameHwnd = FindWindowEx(form1Hwnd, 0, "ThunderFrame", "frameParent")
Avatar of DanRollins
Hi ndroo,
It appears that you have forgotten this question. I will ask Community Support to close it unless you finalize it within 7 days. I will ask a Community Support Moderator to:

    Accept bobbit31's comment(s) as an answer.

ndroo, if you think your question was not answered at all or if you need help, just post a new comment here; Community Support will help you.  DO NOT accept this comment as an answer.

EXPERTS: If you disagree with that recommendation, please post an explanatory comment.
==========
DanRollins -- EE database cleanup volunteer