Title: MDI Programming
1Chapter 13
2Objectives (1)
- Understand the characteristics of MDI
applications - Organize the procedures between forms in MDI
applications - Understand the role of standard forms in an MDI
application - Integrate menus in an MDI application and create
context menus - Understand the relationships between events in an
MDI application
3Objectives (2)
- Manage and organize the forms that make up an MDI
application - Configure the RichTextBox control to display
custom fonts and colors - Read and write files with the RichTextBox control
- Format a RichTextBox control instance
- Use zooming
- Detect links to Web pages
4Solution Organization
5Characteristics of MDI Programs
- Single-document interface applications
- Create one instance of a particular form
- Multiple-document interface (MDI) applications
- Create multiple instances of the same form
- Display those multiple form instances such that
they appear inside the region of another form - Excel is an MDI application, for example
6MDI Applications
- You can use three different types of forms in an
MDI application - An MDI parent form acts as the container for the
MDI child forms in the solution - An MDI child form always appears inside the
visible region of its MDI parent form - A project may have one or more MDI child forms
- Standard forms can be displayed anywhere on the
screen and are not contained by the MDI parent
form - Standard forms are typically displayed as dialog
boxes
7Characteristics of MDI Applications
- MDI child forms can not be extended beyond the
visible region of the containing MDI parent form - Iconified MDI child forms do not appear on the
task bar - The user positions the icon inside the visible
region of the MDI parent form - Menus of MDI parent and MDI child forms are
integrated
8Displaying an MDI Child Form
- Code to create MDI child form appears in MDI
parent form - Create the child form instance
- Dim frmCurrentChild As New frmMdiChild()
- Designate the form as a child form
- frmCurrentChild.MdiParent Me
- Display the child form
- frmCurrentChild.Show()
9Organizing Proceduresin MDI Applications (1)
- The following list describes the problems shared
by nearly all MDI applications - Procedures and data must often be shared by both
MDI parent forms and MDI child forms - An MDI parent form often needs to know which MDI
child form instance is active - From the MDI child form, you must often reference
members of the MDI parent form or the MDI parent
form itself
10Organizing Proceduresin MDI Applications (2)
- When organizing the procedures and variables in
an MDI application, consider the following rules - Locate the procedures that create instances of
the MDI child forms in the MDI parent form - In the MDI child form(s), create the procedures
responsible for managing each instance of the MDI
child form - To share a global reference to the MDI parent
form, declare a Friend or global variable in a
module file - When the MDI parent form loads, store a reference
to the MDI parent form in the Friend or global
variable
11Standard Forms in MDI Applications
- MDI applications can have standard forms
- Characteristics
- Standard forms typically do not have menus
- Standard forms are typically displayed as modal
dialog boxes
12Menus and MDI Applications
- Each MDI parent form and any MDI child form in a
solution can have its own menu system - MDI application menus have unique characteristics
- One menu bar appears below the title bar of the
MDI parent form - An MDI parent forms menu appears when no MDI
child forms are loaded - When an instance of an MDI child form is loaded
or has focus, the menu of the MDI parent form is
merged with the menu of the MDI child form - Standard forms in an MDI application can have an
associated menu but typically they do not
13Merging Menus
- Menus of MDI parent and MDI child forms are
merged based on the following property values - MergeOrder property
- Applies to a MenuItem and contains an Integer
value - Indicates the relative order in which two menus
or menu items will be merged - MergeType property
- Works in conjunction with the MergeOrder property
- Defines whether one menu will replace another
menu, whether one menu will be added to another
menu, or whether menu items will be merged
together
14The MergeType Property
- The MergeType property has a data type of
MenuMerge - Possible enumeration values
- Add - menu on the MDI child form will be added to
the menu of the MDI parent form - MergeItems - menu items on the MDI child form
menu will be merged with the menu items on the
MDI parent form menu - Remove - MDI parent form menu or menu item will
not appear in the merged menu - Replace - causes the menu of the MDI child form
to replace the menu appearing on the MDI parent
form
15Merging Menus (1)
16Merging Menus (2)
17Merging Menus (3)
18Using Menus to Select MDI Child Forms
- To select and display an MDI child form from a
menu - Set the MdiList property of a menu item to True
- This setting causes the menu item to display a
list of MDI child form instances - The caption appearing in the menu is the same as
the caption appearing in the title bar of the MDI
child form - The process is automatic - you need not write any
statements to display the MDI child forms
19Arranging MDI Child Forms
- The LayoutMdi method applies to the MDI parent
form - Use to configure display of child forms
- Four valid enumeration values
- ArrangeIcons - iconified MDI child windows are
arranged along the bottom of the MDI parent form - Cascade - visible MDI child windows are arranged
such that each MDI child window appears below
another MDI child window and is indented - TileHorizontal - divides the MDI child forms
horizontally to fill the visible region of the
MDI parent form - TileVertical - divides the MDI child forms
vertically to fill the visible region of the MDI
parent form
20Context Menus
- Context menus typically appear when the user
right-clicks on a control instance or the form
itself - To create a context menu
- Create an instance of the ContextMenu control
- Create the menu items in-place
- Associate the context menu with a form or control
instance by setting the ContextMenu property of
the desired form or control instance to the
context menu
21Context Menus (Illustration)
Context menu title cannot be changed
Menu items
22MDI Event Relationships
- Windows fires various events as the user
navigates from form to form in an MDI application - Events
- The Closing event fires for both the MDI parent
and MDI child forms - Fires first for all children and then the parent
- Event can be cancelled
- The Enter event fires for the MDI child form
getting focus - Use to update status information
- The Leave event fires just before an instance of
the MDI child form loses focus
23Order of Closing Events
24Managing MDI Child Forms
- There is no Forms collection to manage the loaded
MDI forms of an application - Create a class to enable the developer to add and
remove forms from the collection, and enumerate
the collection with a For Each loop
25Rich Text Files and the RichTextBox Control
- The RichTextBox control allows the user to edit
files having a standardized file format called
Rich Text Format (RTF) - Embedded inside RTF files are formatting
directives - The control instance interprets the directives to
perform a desired formatting task
26Rich Text File
27Formatted Text
28RichTextBox Control (Properties)
- The Boolean AcceptsTab property defines what
happens when the user presses the Tab key - If set to True, a tab character is inserted into
the control instance - The DetectUrls property causes the RichTextBox
control to recognize URLs - The Lines property returns an array of strings
- Array of strings contains one element for each
line appearing in the control instance - The MultiLine property has the same purpose as
the MultiLine property of the TextBox control - If set to True, the control instance will display
text on multiple lines
29RichTextBox Control (Properties)
- The RightMargin property sets the rightmost limit
for the text that appears on a line - The ScrollBars property defines whether scroll
bars appear in the region of the control instance - Property can be configured to display scroll bars
only when necessary or always - The SelectedRTF property contains the selected
text, including all rich text formatting
directives
30RichTextBox Control (Properties)
- The SelectedText property contains the selected
text - The SelectionColor property defines the color of
the selected text or insertion point - The SelectionFont property defines the font of
the selected text or insertion point - The SelectionStart property contains an Integer
and defines the position of the starting
character of the selection - The SelectionLength property contains the number
of characters in the selection
31RichTextBox Control (Methods)
- The Clear method removes all the text from the
control instance - The Copy method copies the selected text to the
Windows clipboard - The Cut method removes the selected text and
places it in the Windows clipboard - The Paste method copies the contents of the
Windows clipboard into an instance of the
RichTextBox control - The LoadFile method reads a file into the
RichTextBox control instance - The SaveFile method saves the contents of a
RichTextBox control instance to a file
32RichTextBox Control (Events)
- The LinkClicked event only fires when the
DetectUrls property is set to True - The event fires when the user clicks a link
- The TextChanged event fires when the user
modifies the contents of a RichTextBox control
instance - The SelectionChanged event fires when the user
selects different text or moves the insertion
point
33LoadFile and SaveFile Methods
- LoadFile and SaveFile methods read and write a
text file or rich text file, respectively - Overloads Public Sub LoadFile(path As String,
filetype As - RichTextBoxStreamType)
- Overloads Public Sub SaveFile(path As String,
filetype As - RichTextBoxStreamType)
- The path argument contains the folder and file
name of the file to open or save - The filetype enumeration defines how the LoadFile
or SaveFile methods will interpret the file
contents
34LoadFile and SaveFile (Example)
- Read and write the text file named Demo.rtf as a
rich text file - rtfText.LoadFile("C\Demo.rtf", _
- RichTextBoxStreamType.RichText)
- rtfText.SaveFile("C\Demo.rtf",_
- RichTextBoxStreamType.RichText)
35Formatting a Rich Text Box
- When formatting the contents of the rich text
box, two concepts are important - When no text is selected in the control instance,
the cursor position is referred to as the
insertion point - If the user applies formatting attributes such as
boldface, the characters typed by the user at the
insertion point will appear in boldface and have
that selected font - Selected text is called the selection
- Any formatting attributes chosen by the user will
be applied to the selection
36Working with the Selection
- The SelectionFont property returns the font of
the insertion point or selection - The SelectionFont property, in turn, has
properties named Bold, Italic, and Underline - These three properties can store the following
values - Nothing - the selected text contains characters
having different fonts - True - characters in the selection or the
character following the insertion point have the
selected font style applied - False - (default) neither the characters in the
selection nor the character following the
insertion point have the selected font style
applied
37The ColorDialog Control
- The ColorDialog control allows the user to select
a color from a standard dialog box - The ColorDialog control does not set the color of
an object, it merely returns a color - The ColorDialog control contains two sections
- The first section displays basic colors
- The second section allows the user to define
custom colors - Set properties to allow custom color selection
and creation
38The ColorDialog Control (Properties)
- Selected color is returned in the Color property
- The Boolean AllowFullOpen property allows the
user to select custom colors - The Boolean FullOpen property displays custom
colors section
39ColorDialog (Illustration)
Basic colors
Custom colors
40Setting Fonts
- The FontDialog control allows the user to select
fonts - To use the FontDialog control
- First set properties to define the options that
will appear to the user on the dialog box - Then display the control instance
- Selected font is returned in the Font property
41FontDialog Control (Properties)
- The Color property defines the font color
- The Font property defines the selected font
- The MinSize and MaxSize properties allow you to
specify the minimum and maximum user selectable
font sizes - The ShowApply property, if set to True, causes
the Font dialog box to display an Apply button - The ShowColor property (Boolean), if set to True,
allows the user to select colors in the Font
dialog box - The ShowEffects property (Boolean) defines
whether or not the user can select font effects,
such as boldface and underlining
42FontDialog Control (Events and Methods)
- Events
- The Apply event fires when the user clicks the
Apply button - The Apply button only appears if the ShowApply
property is set to True - Methods
- The ShowDialog method displays the dialog box
43FontDialog Control (Illustration)
44Setting Individual Font Attributes
- Setting individual font attributes requires a bit
flag - Determine whether the attribute is set and add
the value to the bit flag - Convert bit flag to FontStyle using CType
45Setting Font Attributes (Illustration)
46Zooming
- The RichTextBox control supports capabilities to
zoom in and out of a documents contents - The ZoomFactor property allows you to change the
magnification of the text displayed in a
RichTextBox control instance - A ZoomFactor of 1 displays the text in normal
magnification - A setting of 1.5 displays the text as 150 of
normal
47Detecting Links to Web Pages
- By setting the DetectUrls property to True, VB
.NET will automatically detect when the user
types a URL into the RichTextBox control instance - You must also create a LinkClicked event handler
that will execute when the user clicks on the URL - To start another program, such as Internet
Explorer, you call the Start method of the
Process class of the System.Diagnostics namespace
48Start Method of the Process Class
- The overloaded Start method starts a new process,
such as Internet Explorer - Overloads Public Shared Function Start(filename
As String) As Process - Overloads Public Shared Function Start(filename
As String, arguments As String) As Process - The filename argument contains the path and the
file name of the program to start - The arguments argument contains any command-line
arguments that should be passed to the program
49Start Method Example
- Start Internet Explorer
- System.Diagnostics.Process.Start("Iexplore", _
- "www.course.com")