Sunday, 23 February 2014

The MDI NotePad Application


The MDI NotePad sample application is a simple text editor similar to the NotePad application included with Microsoft Windows. The MDI NotePad application, however, uses a multiple-document interface (MDI). At run time, when the user requests a new document (implemented with the New command on the application's File menu), the application creates a new instance of the child form. This allows the user to create as many child forms, or documents, as necessary.
To create a document-centered application in Visual Basic, you need at least two forms — an MDI form and a child form. At design time, you create an MDI form to contain the application and a single child form to serve as a template for the application's document.
To create your own MDI NotePad application
  1. From the File menu, choose New Project.
  2. From the Project menu, choose Add MDI Form to create the container form.
    The project should now contain an MDI form (MDIForm1) and a standard form (Form1).
  3. Create a text box (Text1) on Form1.
  4. Set properties for the two forms and the text box as follows.
    Object Property Setting
    MDIForm1 Caption MDI NotePad
    Form1 Caption
    MDIChild
    Untitled
    True
    Text1 MultiLine
    Text
    Left
    Top
    True
    (Empty)
    0
    0

  5. Using the Menu Editor (from the Tools menu), create a File menu for MDIForm1.
    Caption Name Indented
    &File mnuFile No
    &New mnuFileNew Yes

  6. Add the following code to the mnuFileNew_Click procedure:
    Private Sub mnuFileNew_Click ()
        ' Create a new instance of Form1, called NewDoc.
        Dim NewDoc As New Form1
        ' Display the new form.
        NewDoc.Show
    End Sub
    
    This procedure creates and then displays a new instance (or copy) of Form1, called NewDoc. Each time the user chooses New from the File menu, an exact duplicate (instance) of Form1 is created, including all the controls and code that it contains.
  7. Add the following code to the Form_Resize procedure for Form1:
    Private Sub Form_Resize ()
        ' Expand text box to fill the current child form.
        Text1.Height = ScaleHeight
        Text1.Width = ScaleWidth
    End Sub
    
    The code for the Form_Resize event procedure, like all the code in Form1, is shared by each instance of Form1. When several copies of a form are displayed, each form recognizes its own events. When an event occurs, the code for that event procedure is called. Because the same code is shared by each instance, you might wonder how to reference the form that has called the code — especially since each instance has the same name (Form1). This is discussed in "Working with MDI Forms and Child Forms," later in this chapter.
  8. Press F5 to run the application.

Creating an MDI Application

Use the following procedure to create an MDI form and its child forms.
To create an MDI application
  1. Create an MDI form.
    From the Project menu, choose Add MDI Form.
    Note   An application can have only one MDI form. If a project already has an MDI form, the Add MDI Form command on the Project menu is unavailable.
  2. Create the application's child forms.
    To create an MDI child form, create a new form (or open an existing one) and set its MDIChild property to True.

Run-Time Features of MDI Forms


At run time, an MDI form and all of its child forms take on special characteristics:
  • All child forms are displayed within the MDI form's workspace. The user can move and size child forms like any other form; however, they are restricted to this workspace.
  • When a child form is minimized, its icon appears on the MDI form instead of the taskbar. When the MDI form is minimized, the MDI form and all of its child forms are represented by a single icon. When the MDI form is restored, the MDI form and all the child forms are displayed in the same state they were in before being minimized.
  • When a child form is maximized, its caption is combined with the caption of the MDI form and is displayed in the MDI form's title bar (see Figure 6.6).
  • By setting the AutoShowChildren property, you can display child forms automatically when forms are loaded (True), or load child forms as hidden (False).
  • The active child form's menus (if any) are displayed on the MDI form's menu bar, not on the child form.
    Figure 6.6   A child form caption combined with the caption of an MDI form

MDI Child Forms at Design Time


At design time, child forms are not restricted to the area inside the MDI form. You can add controls, set properties, write code, and design the features of child forms just as you would with any other Visual Basic form.
You can determine whether a form is an MDI child by looking at its MDIChild property, or by examining the Project Explorer. If the form's MDIChild property is set to True, it is a child form. Visual Basic displays special icons in the Project Explorer for the MDI and MDI child forms, as shown in Figure 6.5.
Figure 6.5   Icons in the Project Explorer identify MDI child, standard, and MDI forms

Multiple-Document Interface (MDI) Applications


The multiple-document interface (MDI) allows you to create an application that maintains multiple forms within a single container form. Applications such as Microsoft Excel and Microsoft Word for Windows have multiple-document interfaces.
An MDI application allows the user to display multiple documents at the same time, with each document displayed in its own window. Documents or child windows are contained in a parent window, which provides a workspace for all the child windows in the application. For example, Microsoft Excel allows you to create and display multiple-document windows of different types. Each individual window is confined to the area of the Excel parent window. When you minimize Excel, all of the document windows are minimized as well; only the parent window's icon appears in the task bar.
A child form is an ordinary form that has its MDIChild property set to True. Your application can include many MDI child forms of similar or different types.
At run time, child forms are displayed within the workspace of the MDI parent form (the area inside the form's borders and below the title and menu bars). When a child form is minimized, its icon appears within the workspace of the MDI form instead of on the taskbar, as shown in Figure1

Figure 1   Child forms displayed within the workspace of the MDI form

Date and Time Functions


CDate([expr]): Converts a valid date and time expression to a date variable

Example:
' declare a date variable.
Dim curDate as Date
' set our date variable
curDate = CDate("May 20, 2011")
' curDate contains the date using VB's internal storage formats for date/time.
curDate = CDate("2:45:00 PM")
' curDate now contains the time with today's date.
curDate = CDate("May 20, 2011 2:45:00 PM")
' curDate now contains the date and time specified.

IsDate([expr]): Returns a Boolean value that indicates if the evaulated expression can be converted to a date.

Example:
' declare variables.
Dim myDate as Boolean
' set our date variable
myDate = IsDate("May 20, 2011")
' myDate = True
myDate = IsDate(#05/20/2011#)
' myDate = True
myDate = IsDate("5/20/2011")
' myDate = True
myDate = IsDate("52/20/2011")
' myDate = False
myDate = IsDate("Hello World!")
' myDate = False

FormatDateTime([date], [format]) : Returns date or time in a specified format
[format] can take the following values:
  • 0 = vbGeneralDate - Defualt, Returns date: mm/dd/yy and time if specified: hh:mm:ss am/pm.
  • 1 = vbLongDate - Returns date: weekday, monthname, year
  • 2 = vbShortDate - Returns date: mm/dd/yy
  • 3 = vbLongTime - Returns time: hh:mm:ss am/pm
  • 4 = vbShortTime - Returns time: hh:mm
Some may choose to use the Format() function instead because you can be more specific with the format you want

Now(): Returns the current system date and time

Example:
' declare a date variable.
Dim curDate as Date
' set our date variable
curDate = Now()
' VB will automatically convert from date to string.
MsgBox(curDate)
' Produces a message box with the current date and time.

String Functions


CStr([expr]) :Converts a variable expression to a string

Example:
' declare a date variable.
Dim curDate as Date
' set our date variable to the current date/time.
curDate = Now()
' MsgBox expects a string so we need to convert our date.
MsgBox(CStr(curDate))
' Produces a message box with the current date and time.

LCase([string])
UCase([string]) :Converts a string to lower case (or upper case with UCase() function.)

Example:
' declare variables.
Dim txt as Date
txt = "This is a beautiful day!"
txt = UCase(txt)
' txt = "THIS IS A BEAUTIFUL DAY!".
txt = LCase(txt)
' txt = "this is a beautiful day!".

Left([string], [length])
Right([string], [length]) : Returns a specific number of characters from the left side (right side with the Right() Function) of a string

Example:
' declare variables.
Dim txt as Date
txt = "This is a beautiful day!"
txt = Left(txt, 6)
' txt = "This i".
txt = Right(txt, 2)
' txt = " i".

Len([string])):Returns the number of characters in a string

Example:
' declare variables.
Dim txt as String
Dim cnt as Integer
txt = "This is a beautiful day!"
cnt = Len(txt)
' cnt = 24
cnt = Len("This is a beautiful day!")
' cnt = 24

 Mid([string], [start], [length])):Returns a specified number of characters from a string

Example:
' declare variables.
Dim txt as String
Dim txt2 as Integer
txt = "This is a beautiful day!"
txt2 = Mid(txt, 1, 1)
' txt2 = "T"
txt2 = Mid(txt, 3, 10)
' txt2 = "is is a be"


Trim([string])
LTrim([string])
RTrim([string])
Remove spaces from both sides of a string. (LTrim = left side only, RTrim = right side only)

Example:
' declare variables.
Dim fname as Date
Dim txt as Date
fname = " Tim "
txt = "Hello" & fname & "and welcome"
' txt = "Hello Tim and welcome".
txt = "Hello" & Trim(fname) & "and welcome"
' txt = "HelloTimand welcome".

InStr([start], [string1], [string2], [compare])
InStrRev([start], [string1], [string2], [compare]):
Returns the position of the first (or last with InStrRev) occurance of one string within another.
[compare] can have a value of 0 = vbBinaryCompare or 1 = vbTextCompare

Example:
' declare a string variable and integer variable.
Dim txt as String
Dim pos as Integer
txt = "This is a beautiful day!"
pos = InStr(0, txt, "beautiful")
' pos = 10.

FindReplace(sSearchString$, sFindWhat$, sReplaceWith$):
This function is not a built in function to VB however can be very useful in your VB applications. A simple search and replace function. Pass sSearchString$ ByRef.