You are on page 1of 14

Multiple Form Applications

Week 12

Instructor: Jawad Shaukat


Outline
1. Single Document Interface
2. Multiple Document Interface
Single Document Interface
 A single-document interface, SDI, is an application primarily made of a form
equipped with a menu. An example is Notepad.
 In some cases, an SDI can also have a toolbar and/or a status bar. Here is an
example from Microsoft Works Spreadsheet:

 All these features are left to the programmer to add and


configure.
 Although Notepad is text-based, an SDI can be any type of
application, text, graphics, spreadsheet, anything. Therefore,
to create an SDI, start from a normal form, add a menu to it,
and configure it to do what is required.
 To create a document using an SDI, the user launches the
application. The SDI then presents a rectangular window with
one frame and the inside is the document the user will use. In
most cases, the application itself creates the document. The
user can work on it and do whatever the application allows. To
create another document of the same type, the user must open
another instance of the application.
Multiple Document Interface
 A multiple-document interface, MDI, is an application that primarily has a
form and a menu. Some, if not most MDIs also have one or more toolbars
and/or a status bar. Here is an example
 Like a normal application, to use an MDI, the user must launch it. In some
cases, when the application starts, it is empty; that is, no document is
created and the title bar displays a caption, usually the name of the
application.
 Usually, there are steps the user must follow to create a document. In some
other cases, when the application is launched, it automatically creates a
document. A document resides inside the parent frame of the application.
That is, a child document can use only the area reserved for it. The child
document has its own system icon, its own title bar, and its system buttons
(Minimize, Maximize/Restore, and Close).
 To use the whole area, the user can maximize the child document. When this
is done, the child merges its title bar with the parent's. The new caption of
the title bar becomes made of the text of the parent, followed by -, and
followed by the caption the child window was using. The system buttons of
the child document display under those of the parent frame:
Multiple Document Interface.

 Once a document has been created, the user can use


it. Normally, the application must give the user the ability to
create other documents while still using the application. If
many documents have been created, all of them are confined
in the frame of the application:
 The user can maximize the child forms. If so, the document
that was in front occupies the whole area devoted to
child documents. The other child forms stay in the back but
become invisible.
MDI
 In some applications, the child windows can be of different types, such as
chart windows and spreadsheet windows.
 In that case, the menu bar can change as MDI child windows of different types
are activated.
 Standard Windows forms may exist as either MDI parents or MDI children.
 MDI children may only be displayed within the client area of their parent and
may not be dragged outside that client area.
 MDI children always remain on top of their parent form.
 Any standard Windows form can be converted to an MDI parent by setting its
IsMdiContainer property to True.
 By convention, MDI parent forms have a dark gray client area.
 Because MDI parents are intended solely as containers for other forms, they
should not include controls.
Creating an MDI Application

 You start an MDI application with a normal form. You can create a Windows Forms
Application using Microsoft Visual Studio or derive a class from Form. 
 The primary form of an MDI application is referred to as the parent or MDI container.
It provides the frame inside of which the documents will reside. To provide this
functionality, the Form class is equipped with a Boolean property
named IsMdiContainer. Therefore, after creating the first form of your application,
to indicate that it acts as the main frame, set this property to true. You can do this
in the Properties window if you are visually creating your application, or
programmatically.

1) Open your application. In main form set the property of ‘isMDIContainer=true’


 this.IsMdiContainer = true;

 Do not add any control on this form. You can add menu strip if you want as per your
application requirements.
Create an MDI Application
Adding a second form.
 Use the Add Windows Form… selection from the Project menu.
 A new form class is added to the project.
 In parent form constructor. Write the following code to open the child form.

Form2 frmChild = new Form2();


frmChild.MdiParent = this;
frmChild.Show();

 Set the MdiParent property of the child instance to the parent, before
Showing the child form.
Launching a second form.
 An instance of the second form must be created within the original
application form.
 Once the form object is created, it may be displayed by running its Show()
method.
 Multiple instances of the second form can be created and displayed.
MDI Child form

 Forms destined to become children of an MDI parent should be instantiated


within the MDI parent class.
 Any standard Windows form can become an MDI child.
Hiding and showing the parent form.

Hiding the parent form.


 Run the Hide() method of the original form to remove it from view prior to showing
the second form.
 Now the second form commands the application and can not lose focus unless it is
closed.
 Closing the second form leaves the original form running, but not visible.
Showing the parent form.
 We could run the Show() method of the original form from the second form—but only if
the second form is aware of the first form.
 If we created a new instance of the first form within the second form it would be
different from the original.
 One way to solve the problem is to pass the identity of the original form to the second
form as a constructor argument.
Hiding and showing the parent form.
private void frmButton_Click (object sender, EventArgs e)
{
SecondForm s = new SecondForm(this);
this.Hide();
s.Show();
}

Form parentForm;
public SecondForm (Form p)
{
parentForm = p;
InitializeComponent();
}
private void SecondForm_FormClosing (object sender,
FormClosingEventArgs e)
{
parentForm.Show();
}
Passing data between forms.
Form parentForm;
String message;
public SecondForm (Form p, String s)
{ Child
parentForm = p; Form
message = s;
InitializeComponent();
}

private void SecondForm_FormClosing (object sender,


FormClosingEventArgs e)
{ Child
parentForm.Show(); Form
}
private void SecondForm_Load (object sender, EventArgs e)
{
msgLabel.Text = message;
}
Passing data between forms.
Parent
Form

private void frmButton_Click (object sender, EventArgs e)


{
}
SecondForm s = new SecondForm(this, "Hello");
s.Show();
Example.
Create a login page to access
dashboard.
Example in VS Project

You might also like