Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Embeding a form within a form

Posted on 2002-07-09
11
210 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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 

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
 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

792 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