Home

Microsoft Visual Basic - SDI and MDI Applications

 

SDI and MDI Fundamentals

 

Introduction

A single-document interface, SDI, is an application primarily made of a form equipped with a menu. An example is Notepad:

Notepad

In some cases, an SDI can also have a toolbar and/or a status bar. Here is an example from Microsoft Works Spreadsheet:

Microsoft Works

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 you want.

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.

ApplicationPractical Learning: Introducing MDI Applications

  1. Start Microsoft Visual Studio
  2. To create a new application, on the main menu, click File -> New Project...
  3. In the middle list, click Windows Forms Application
  4. Change the Name to Notice2 and click OK
  5. In the Solution Explorer, right-click Form1.vb and click Rename
  6. Type Notice.vb and press Enter
  7. From the Dialogs section of the Toolbox, click the OpenFileDialog button OpenFileDialog and click the form
  8. In the Properties window, click (Name) and type DlgFileOpen
  9. From the Menus & Toolbars section of the Toolbox, click the MenuStrip button OpenFileDialog and click the form
  10. In the Properties window, change its (Name) to MnuMain
  11. On the form, right-click the menu strip and click Insert Standard Items
  12. In the File group, click Type Here, type Close and press Enter
  13. On the form, click the Close menu item and, in the Properties window, double-click Enabled to set it to False
  14. Move that new menu item and position it under Open
  15. Under the form, click MnuMain. In the Properties window, click Items and click its browse button
  16. Change the names of the items as follows:

    Top Menu Item DropDownItems
    Old Name New Name Old Name New Name
    FileToolStripMenuItem MnuFile    
        NewToolStripMenuItem MnuFileNew
        OpenToolStripMenuItem MnuFileOpen
        CloseToolStripMenuItem MnuFileClose
          Separator
        SaveToolStripMenuItem MnuFileSave
        SaveAsToolStripMenuItem MnuFileSaveAs
          Separator
        PrintToolStripMenuItem MnuFilePrint
        PrintPreviewToolStripMenuItem MnuFilePrintPreview
          Separator
        ExitToolStripMenuItem MnuFileExit
    EditToolStripMenuItem MnuEdit    
        UndoToolStripMenuItem MnuEditUndo
        RedoToolStripMenuItem MnuEditRedo
          Separator 
        CutToolStripMenuItem MnuEditCut
        CopyToolStripMenuItem MnuEditCopy
        PasteToolStripMenuItem MnuEditPaste 
          Separator 
        SelectAllToolStripMenuItem MnuEditSelectAll
    ToolsToolStripMenuItem MnuTools    
        CustomizeToolStripMenuItem MnuToolsCustomize
        OptionsToolStripMenuItem MnuToolsOptions
    HelpToolStripMenuItem MnuHelp    
        ContentsToolStripMenuItem MnuHelpContents
        IndexToolStripMenuItem MnuHelpIndex
        SearchToolStripMenuItem MnuHelpSearch
          Separator
        AboutToolStripMenuItem MnuHelpAbout
     
    Items Collection Editor
  17. Click OK and OK
  18. From the Menus & Toolbars section of the Toolbox, click ToolStrip Tool Strip and click the form
  19. While the tool strip is still selected, in the Properties window, change its Name to TbrStandard
  20. On the form, right-click the tool strip and click Insert Standard Items
  21. Under the form, click tbrStandard. Under the Properties window, click Edit Items
  22. Change the names of the items as follows:
     
    Old Name New Name
    NewToolStripButton TbrStandardNew
    OpenToolStripButton TbrStandardOpen
    SaveToolStripButton TbrStandardSave
    PrintToolStripButton TbrStandardPrint
    Separator  
    CutToolStripButton TbrStandardCut
    CopyToolStripButton TbrStandardCopy
    PasteToolStripButton TbrStandardPaste
    Separator  
    HelpToolStripButton TbrStandardHelp
  23. Click OK
  24. From the Menus & Toolbars section of the Toolbox, click StatusStrip Status Strip and click the form
  25. While the tool strip is still selected, in the Properties window, change its Name to StbApplicationStatus
  26. Set its AutoSize to False
  27. Click the + button of Size and change the Width to 28
  28. On the form, to create a section, click the button that displays
  29. While the new label is still selected, in the Properties window, change the following characteristics:
    (Name): PnlMessage
    AutoSize: False
    BorderSides: All
    BorderStyle: Sunken
    DisplayStyle: None
    Size -> Width: 200
  30. On the form, click the new button on the right side of the previous label
  31. While the new label is still selected, in the Properties window, change the following characteristics:
    (Name): PnlStatus
    AutoSize: False
    BorderSides: All
    BorderStyle: Raised
    DisplayStyle: None
    Size -> Width: 150
  32. On the form, click the new button on the right side of the previous label
  33. While the new label is still selected, in the Properties window, change the following characteristics:
    (Name): PnlZoom
    BorderSides: All
    BorderStyle: SunkenOuter
    DisplayStyle: None
    Spring: True

MDI Fundamentals

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:

Multiple-Document Interface

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:

MDI

Once a document has been created, the user can use it. Normalement, 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 Topic Applied:

Multiple Document Interface

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.

One of the differences between an SDI and an MDI is that, because the document and the application don't share a frame, the user can close the document and keep the application open.

Creating an MDI Application

As mentioned already, there is nothing magical with creating an SDI. You start with a form, add a menu to it, and specify what the application should allow a user to do with a document. As we will see, an MDI requires more steps.

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. Here is an example:

Imports System.Drawing
Imports System.Windows.Forms

Public Class Exercise
    Inherits System.Windows.Forms.Form

    Public Sub New()
        
    End Sub

    Public Shared Function Main() As Integer
        Application.Run(New Exercise)

        Return 0
    End Function

End Class

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. Here is an example:

Imports System.Drawing
Imports System.Windows.Forms

Public Class Exercise
    Inherits System.Windows.Forms.Form

    Public Sub New()
        Me.IsMdiContainer = True
    End Sub

    Public Shared Function Main() As Integer
        Application.Run(New Exercise)

        Return 0
    End Function

End Class

This would produce:

Multiple-Document Interface

The primary characteristic of an MDI is that it contains other forms. These forms must be created and made available to the parent. Each form can be created using a predefined form or you can programmatically create one by declaring an object of type Form. To allow you to specify that a form has a parent and will act as a child, the Form class is equipped with a property named MdiParent. This is a read-write property. The set accessor indicates what form acts as this one's parent. To provide this information, assign the main form this form's MdiParent. After doing this, you can display the form when you are ready, by calling its Show() method. Here is an example:

Imports System.Drawing
Imports System.Windows.Forms

Public Class Exercise
    Inherits System.Windows.Forms.Form

    Public Sub New()
        IsMdiContainer = True

        Dim frmChild As Form = New Form
        frmChild.MdiParent = Me

        frmChild.Show()
    End Sub

    Public Shared Function Main() As Integer
        Application.Run(New Exercise)

        Return 0
    End Function

End Class

This would produce:

Multiple-Document Interface

 
 
 

ApplicationTopic Applied: Creating an MDI Application

  1. Click the form to give it focus
  2. In the Properties window, change the following characteristics:
    IsMdiContainer: True
    StartPosition: CenterScreen
    Text: Notice
     
    Notice
  3. To add a new form, on the main menu, click Project -> Add Windows Form...
  4. In the middle list, make sure Windows Form is selected.
    Set the Name to SingleDocument
  5. Click Add
  6. From the Common Controls section of the Toolbox, click RichTextBox Rich Text Box and click the form
  7. In the Properties window, change the following characteristics:
    (Name): RtbNotice
    Dock: Fill
    Modifiers: Public
  8. Display the first form
  9. On the form, click File and double-click New
  10. Implement the event as follows:
    Public Class Notice
    
        Private Sub MnuFileNew_Click(sender As System.Object,
                                     e As System.EventArgs) Handles MnuFileNew.Click
            Dim document As SingleDocument = New SingleDocument
            document.Text = "Untitled"
    
            document.MdiParent = Me
            document.Show()
    
            MnuFileClose.Enabled = True
        End Sub
    End Class
  11. On the Object combo box, select MnuFileOpen
  12. In the Procedure combo box, select Click
  13. Implement the event as follows:
    Private Sub MnuFileOpen_Click(sender As Object,
                                  e As System.EventArgs) Handles MnuFileOpen.Click
        If dlgFileOpen.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
            For Each strFile As String In dlgFileOpen.FileNames
                Dim document As SingleDocument = New SingleDocument
    
                document.RtbNotice.LoadFile(strFile)
    
                document.MdiParent = Me
                document.Show()
            Next
        End If
    End Sub
  14. Return to the main form
  15. On its toolbar, click the first button (New)
  16. Click the Events button Events of the Properties window
  17. Click the Click field and, on the right side, select MnuFileNew_Click
  18. On the toolbar of the form, click the second button (Open)
  19. In the Events section of the Properties window, click the button of Click and select MnuFileOpen_Click

MDI Characteristics

 

Introduction to the Children of an MDI

When you create an MDI application, you must make sure you provide your users with the ability to create documents. In fact, probably one of your first assignments is to make sure the user can create as many documents as necessary. As the documents are created, you need a way to programmatically keep track of the child forms. For example, you can store the documents in a collection. To assist you with this, the Form class is equipped with a property named MdiChildren, which is a read-only array. Each element of the MdiChildren[] array is of type Form. Therefore, to access a child form of an MDI application, you can pass an index to this property.

The Layout of Child Forms

Based on the standards defined in the operating system, as child forms are created in an MDI application, they are positioned each on top of the previous one but below the next one. The arrangement uses a 3-D coordiniate system whose origin is on the lower-left corner of the parent's frame just under the title bar (or the menu, if any; or the toolbar, if any), with the Z axis moving from the monitor towards you:

Layout

The operating system allows the user to choose among different arrangements. For example, you can position the documents as vertical columns, as horizontal rows, or as tiles. To support this, the Form class is equipped with a method named LayoutMdi. Its syntax is:

Public Sub LayoutMdi(value As MdiLayout)

The LayoutMdi() method takes an argument that is a member of the MdiLayout enumeration. The members of this enumeration are Cascade, TileHorizontal, TileVertical, and ArrangeIcons.

ApplicationTopic Applied: Laying Out the Documents

  1. Make sure the Notice form is displaying.
    Under the form, click MnuMain
  2. In the Properties window, click Items and click its ellipsis button
  3. In the Select Item And Add To List Below, make sure MenuItem is selected and click Add
  4. Change its Text to &Window
  5. Change the (Name) to MnuWindow
  6. Use the up button up to move and position it just above MnuHelp
     
    Items Collection Editor
  7. While MnuWindow is selected, in the right list, click DropDownItems and click its button
  8. Use the Add button to create items with the following characteristics:
     
    Text (Name)
    &Arrange MnuWindowArrange
    &Cascade MnuWindowCascade
    Tile &Horizontal MnuWindowTileHorizontal
    Tile &Vertical MnuWindowTileVertical
  9. Click OK
  10. Click OK
  11. On the form, click Window and double-click Arrange
  12. Implement the event as follows:
    Private Sub MnuWindowArrange_Click(sender As System.Object,
                               e As System.EventArgs) Handles MnuWindowArrange.Click
        LayoutMdi(MdiLayout.ArrangeIcons)
    End Sub
  13. In the Object combo box, select MnuWindowCascade
  14. In the Procedure combo box, select Click
  15. Implement the event as follows:
    Private Sub MnuWindowCascade_Click(sender As Object,
                              e As System.EventArgs) Handles MnuWindowCascade.Click
            LayoutMdi(MdiLayout.Cascade)
    End Sub
  16. In the Object combo box, select MnuWindowTileHorizontal
  17. In the Procedure combo box, select Click
  18. Implement the event as follows:
    Private Sub MnuWindowTileHorizontal_Click(sender As Object,
                        e As System.EventArgs) Handles MnuWindowTileHorizontal.Click
            LayoutMdi(MdiLayout.TileHorizontal)
    End Sub
  19. In the Object combo box, select MnuWindowTileVertical
  20. In the Procedure combo box, select Click
  21. Implement the event as follows:
    Private Sub MnuWindowTileVertical_Click(sender As Object,
                         e As System.EventArgs) Handles MnuWindowTileVertical.Click
            LayoutMdi(MdiLayout.TileVertical)
    End Sub

Activating a Child

In most MDI applications, a user can create as many documents as necessary. This also means that the application can hold many child forms. To access a child form, the user can click its title bar. You can also provide options on a menu item named Window that would display a list of open documents.

When a child window is activated, it fires an event named MdiChildActivate:

Public Event MdiChildActivate As EventHandler

The MdiChildActivate event is of type EventArgs.

The document that is currently active has a brighter title bar. To identify this document, the form has a property named ActiveMdiChild. This read-only property allows you to know what document is the current active one. This property is of type Form, which means its produces a Form object. When you enquire about this property, if its value is null, it means there is no current active document.

If the value of the ActiveMdiChild property is not null, a document is active and you can use it. If you want to access the objects (Windows controls) that are positioned on a child form, remember that the child form is somehow foreign. Therefore, you should set the Modifiers property of its hosted controls approppriately. For example, if you want the parent frame to access a control on the child frame, set the Modifiers of that control to Public.

ApplicationTopic Applied: Getting the Active Document

  1. In the Object combo box, select MnuFileClose
  2. In the Procedure combo box, select Click
  3. Implement the event as follows:
    Private Sub MnuFileClose_Click(sender As Object,
                                 e As System.EventArgs) Handles MnuFileClose.Click
            Dim document As SingleDocument = CType(ActiveMdiChild, SingleDocument)
    
            If Not document Is Nothing Then
                document.Close()
            End If
    
            If MdiChildren.Length = 0 Then
                MnuFileClose.Enabled = False
            End If
    End Sub
  4. Return to the form

Arranging the Minimized Children

In an MDI application, if the user doesn't want to display a document but doesn't want to close it, he can minimise the window. In the same way, the user can minimize as many child forms as necessary. When a child form has been minimized, it shows a button in the lower part of the parent. The buttons of the other minimized child forms are usually positioned next to each other:

Minimized Buttons

The user can move the buttons at will:

Minimized Buttons

A user can also close the child form using the Close button of its minimized button. At one time the minimized buttons may display as a "mess". To let you rearrange them, call the LayoutMdi method of the Form class and pass the argument as ArrangeIcons. When you do this, the application will visit all buttons, if any, that represent minimized child documents, and position them from the left to the right, adjacently.

 
 
   
 

Home Copyright © 2010-2016, FunctionX Home