Solved

Embeding a form within a form

Posted on 2002-07-09
11
201 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 18

Expert Comment

by:bobbit31
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
"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
Comment Utility
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

771 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

13 Experts available now in Live!

Get 1:1 Help Now