Solved

Embeding a form within a form

Posted on 2002-07-09
11
204 Views
Last Modified: 2010-05-02
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?
0
Comment
Question by:ndroo
  • 4
  • 3
  • 2
  • +2
11 Comments
 
LVL 18

Accepted Solution

by:
bobbit31 earned 200 total points
ID: 7141062
Private Declare Function SetParent Lib "user32" (ByVal hWndChild As Long, ByVal hWndNewParent As Long) As Long

Private Sub Command1_Click()
    Form2.Show
    Form2.Left = 0
    Form2.Top = 0
    SetParent Form2.hWnd, Frame1.hWnd
End Sub

0
 
LVL 22

Expert Comment

by:rspahitz
ID: 7141149
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.
0
 
LVL 18

Expert Comment

by:mdougan
ID: 7141177
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.
0
 

Author Comment

by:ndroo
ID: 7142285
Thanks for your prompt comments. I'll try it out and let u guys know the result. Appreciate it. Thanks.
0
 

Author Comment

by:ndroo
ID: 7142401
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.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 18

Expert Comment

by:bobbit31
ID: 7143671
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
0
 
LVL 18

Expert Comment

by:mdougan
ID: 7144424
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.
0
 

Author Comment

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

 frameHwnd = FindWindowEx(form1Hwnd, 0, "ThunderFrame", "Frame1")
0
 

Author Comment

by:ndroo
ID: 7145582
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.
0
 
LVL 18

Expert Comment

by:bobbit31
ID: 7146343
"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")
0
 
LVL 49

Expert Comment

by:DanRollins
ID: 8002245
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
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now