Embedding forms inside a form in windows application

Embedding forms inside a form in windows application:

Problem Scenario:

Let me first give a brief introduction of the problem so that we can understand better about the context of the solution.

In one of my project, I need to wrap up an old application with the new features and functionalities. But the problem I faced is, in every form, customer wants two new sections to be added and those two sections will remain fixed for all the forms.

Solution:

The options I left with is either to edit each form and add those two section to each form or we can have a MDI form or a simple form and divide it into three sections.

Top and side left portion will be fixed and in bottom right portion, different forms will be loaded and unloaded as per the triggered events from other two sections. Also we need to make right bottom corner scrollable.

We adopt the second solution and there by came across the need for “Embedding a form inside a form”.

The goal is to achive an effect like we have with Master page scenario in asp 2.0. where we can have common fixed portion appearing for all pages. These are useful for many scenarios such as having side menu bar etc. To achieve the goal I chose MDI form having divided into sections by number of panels (in my case it’s three) and load the user controls for the portions which are going to be fixed. According to the triggered events on those user controls we can load unload forms on the third panel.

Started With:

We can embed form within another form or within a MDI form. Both the approach is basically same except few differences. But let understand the difference between adding a control and a form to another form. Control can be added at design time by drag drop or at run time using Me.controls.add() method. But in case of embedding a form within a form we have to do it in run time, but before that we need to have a container control like panel, as we can not just use Me.controls.add. In case of MDI form we can use Controls. Add () method but that wont server our purpose.

Steps to embed form within an MDI form

As discussed above let say we have a MDI form with name “frmMDI” which has three panels: – pnl1, pnl2 and pnl3.

To add user controls to the panels we can use

Pnl1.Controls.Add (instance of user control)

But to add form to panel let say pnl3 below is the steps need to follow

Dim sampleOne As New frmSampleOne

sampleOne.MdiParent = frmMDI

sampleOne.TopLevel = False

sampleOne.FormBorderStyle = Windows.Forms.FormBorderStyle.None

sampleOne.AutoScroll = True

sampleOne.Dock = DockStyle.Fill frmMDI.pnlFrm.Controls.Add(GeneticHx)

sampleOne.Show()

sampleOne.WindowState = FormWindowState.Normal

lets discuss the steps:

1. First need to instantiate the form

2. Set the Mdi parent to the frmMDI so that you can use “ActiveMdiChild” fi needed

3. We need to set the TopLevel property of the form to false. By default the TopLevel property of the form is true. This property indicates whether to display the form as a top-level window. A top-level form is a window that has no parent form, or whose parent form is the desktop window. Top-level windows are typically used as the main form in an application. So if we do not set the TopLevel property to false it will throw exception as the form is going to add under a container control like Panel or MDI form.

4. Set FormBorderStyle to none to remove the upper action bar.

5. Set the AutoScroll to true. This is needed to for scrolling to appear. Here we need to understand that scrolling appears on the form which is getting added not on the panel. Here the AutoScroll property for the form means that if the form exceeds the size of its parent control (here pnl3) scrolling appears.

6. Set the Dock property “fill” so that it can acquire the whole space under the panel

7. Add the form object into the panel by Controls.Add() method

8. Invoke the show () methode on the form object to make the form loaded.

9. Set the WindowState property to Normal. This very important for the scrolling to come. If we set the WindowState to maximum, scrolling will not come for form .

Also one point is worth mentionable here is that if you are adding a form and that form itself has one or more panels then make sure that the Dock property of those panel set to None. if not scrolling will not appear for that form.

As we have now finished with adding form under a panel now let’s see how we can load and unload forms and invoking events on those forms from out side panel. Let say as per our example on click of a button on pnl2 I need to close the form under pnl3 if any and load frmSampleTwo in that panel.

For the above mentioned situation first we need to access the form instance in the panel.

Dim sampleOne As frmSampleOne

Dim ctl As New Control

If frmMDI.pnl3.Controls.Find(“frmSampleOne”, True).Length > 0 Then

ctl = frmMDI.pnl3.Controls.Find((“frmSampleOne”, True)(0)

sampleOne = CType(ctl, Form)

Follow the steps as mentioned above for adding new form (frmSampleTwo) in pnl3

sampleOne.close()

End If

As the above code shows once we get the instance of the form under pnl3 we can invoke any events and methods having public modifier.

It’s always good to load the next form before closing the existing one to avoid flickering effect. That’s the reason we load frmSampleTwo before invoking close () method on sampleOne.

By applying above concept we can divide a form into sections and can load more than one form under each section.

Did you enjoy this post? Why not leave a comment below and continue the conversation, or subscribe to my feed and get articles like this delivered automatically to your feed reader.

Comments

No comments yet.

Leave a comment

(required)

(required)