Table of Contents

12.2.7 Form - Form in Form

The question “Is it possible to embed a form in another form (FormInForm), the selection of which can be changed at will?” can generally be answered YES, although the detailed answers vary:

B1

In this chapter, you will be presented with projects in which answers 2 and 3 are implemented in practice. The development of your own control elements is described in → Chapter 12.2.8. The corresponding projects are also presented.

12.2.7.1 Project 1

In all projects presented, including those in chapter 12.2.8, the following procedure is implemented:

In order to focus attention on inserting a form into a (main) form, the functionality of the (main) form is spartan. It is limited to making the background colour of the inserted form visible via a SwitchButton and a button with which the (main) form can be closed. The inserted form displays either a text in a TextArea or a picture in a PictureBox. Switching is done with the 'Switch View' button on the inserted form. An inserted form has no title bar with the window title and the usual buttons because it is not a top-level window. Therefore, the inserted form has a menu to show here that the inserted form is its own event observer and therefore you can manage its events - such as _Close() - in the source code of the class itself.


Figure 12.2.7.1.1: Main window with inserted window in action.


Figure 12.2.7.1.2: Main window with selected window (left)


Figure 12.2.7.1.3: Main window with outlined but empty container (panel).

In connection with the development of projects where forms are inserted into a (main) form, this experience could be gained:

From the source code of the class for the inserted form FEmbedded, only this excerpt is interesting for the constructor:

Public Sub _new(iColor As Integer)
  TextArea1.Background = iColor
End ' _new(...)

This is how the form FEmbedded is inserted into the form FMain:

Private hFEmbedded As FEmbedded
 
Public Sub Form_Open()
  FMain.Center()
' Weitere Initialisierungen ...
  SwitchButton1.Value = False
  hFEmbedded = New FEmbedded(&HE1EAF6, panEmbedder)
End ' Form_Open()

Comment:

hFEmbedded = New FEmbedded(&HE1EAF6, panEmbedder)

First the argument for the colour is noted (obligatory parameter) and only then the argument for the parent container (optional parameter).Note that you do not have to open the FEmbedded window! It will open automatically when it is inserted into the container (panel) on the (main) form FMain.

12.2.7.2 Project 2

In the second project you will learn how to insert two different forms into a (main) form and also how to remove them from their container on the (main) form.


Figure 12.2.7.2.1: Three top-level windows


Figure 12.2.7.2.2: (Main) window with window 1 inserted.


Figure 12.2.7.2.3: (Main) window with window 1 and 2 inserted.


Figure 12.2.7.2.4: (Main) window with the two windows 1 and 2 removed.

For a test in the project trial, you can display the number of top-level windows, their titles and window heights, and the names of the control elements on the (main) form:

FMain
Number of open windows = 3

1. Window: Caption      = MAIN FORM
1. Window: Window height = 301
------------------------------------------
2. Window: Caption      = FORM 1
2. Window: Window height = 126
------------------------------------------
3. Window: Caption      = Form 2
3. Window: Window height = 126
------------------------------------------
panContainer
HBox1

The insertion of the two windows in project 2 is done by one of the two procedures in the event handler Form_Open() of FMain, respectively:

Public Sub Form_Open()
' When this window is closed, all project windows are closed automatically
  Application.MainWindow = FMain
  ...
' Insert Form1 and Form2 in FMain - Set Button Texts
  btnEMUNForm1_Click()
  btnEMUNForm2_Click()
End
 
Public Sub btnEMUNForm1_Click()
  If btnEMUNForm1.Value Then
     Form1.Reparent(panContainer, 0, 0) ' Insert
     btnEMUNForm1.Text = ("Unembed F1")
  Else
     Form1.Reparent(Null) ' Remove
     btnEMUNForm1.Text = ("Embed F1")
  Endif
End
 
Public Sub btnEMUNForm2_Click()
  If btnEMUNForm2.Value Then
     Form2.Reparent(panContainer, 0, 0) ' Insert
     btnEMUNForm2.Text = ("Unembed F2")
  Else
     Form2.Reparent(Null) ' Remove
     btnEMUNForm2.Text = ("Embed F2")
  Endif
End

Download