Friday, December 20, 2013

MDI forms in .net using C# and Visual Studio 2010 Express

There are two kinds of forms:
SDI (Single Document Interface)
MDI (Multiple Document Interface)

We are interested in MDI forms. MDI forms allow us to contain children form between.

Steps:

As 1st step, we create a new project (Windows application).

In main form, set property IsMdiContainer to True (See example)


main form, click to enlarge

Now we create our child form and it works as a form template to create multiple children form. This example tries to simulate a simple text editor and with MDI support.

We add a second form to our project.

We add a panel to our form (this panel will contain a "Save" button) and it is located at bottom. Set Dock Property to Bottom.

Set Dock property to Bottom
Now we add a 2nd panel to our form (this panel will contain a text box). Set this panel´s property Dock to Fill.


In 2nd panel set Dock property to Fill


We add a textBox inside 2dn panel and set it these properties:
Name = txtBoxContent
Multiline = True
Dock = Fill





Now we can add a button to save current working for our text editor. This button will be placed at bottom panel. And we have a form like this:

This child form works as a template form

It is time to create a menu to handle our children forms (from main form).

Notice that option "Lista ->" does not have any sub-element, this due we will create all our children windows in run time.


.


Time to code!!!

To create new children windows, we will handle click event from "New windows" menu.

We create a new object (FrmChildBase) and set its property MdiParent.

Code to create new children forms

To order our children forms we must handle click event from menus: Horizontal, Vertical and Cascada.
There is a function MdiLayout, see code:

In case we need to know which mdi form is active we use property ActiveMdiChild, this property returns a reference to the active form. This is how it works:

Note: we can have a reference to the active control with ActiveControl property.

As a last example, we will list all children forms in run time for our menu. To achieve this, we will handle MouseEnter event in our menu. To add all children form to our menu we need to clean previous items and in a foreach loop get all forms and add them to menu. MdiChildren returns an array of  mdi forms.


Based on: 
http://lambdabox.blogspot.com/2012/01/manejo-de-formularios-mdi-con-c-y.html



No comments:

Post a Comment