A Method to our Madness

We will now begin working on the final version of our word processing project. In this version, we will use the menus and dialogs that traditional Windows use. Before we begin, lets review some terminology.

Visual Basic programs consist of many objects that have specific functions. These functions are executed as the result of some event like a mouse click. That is why Visual Basic is considered an Object Oriented Programming Language (OOPS) and also as an event driven programming language.

The objects in Visual Basic programs like buttons, text boxes, labels and so forth have certain physical characteristics called properties. We have see that you can set the properties of an object in the properties box at design time or by sending the object a message at run time. For example, to sent a message to a text box to change its font bold property to true, you send the message

txtWord.FontBold = True

Objects also have things that they can do. They behaviors, called methods, can be used to change the object. For example, the many object have a Move method that can actually re-locate the object on the form. For example,

txtWord.Move 0, 0

Moves the text box to the upper left hand portion of the form.

Our new Word Processing program will use some methods.

The Text Box

The first problem we will solve regards the text box. When the program begins, the text box should completely fill the form. This can be accomplished in the design phase by simply sizing it with the mouse, but a better approach is to have the program size the text box. There are several properties of the form and text box that we will use- plus one text box method. When the form loads, we want to send the following messages to the text box:

  1. Move to the upper left corner of the form.
  2. Make your height equal the height of the form
  3. Make your width the width of the form

Form Properties

ScaleHeight = the height of the form
ScaleWidth = the width of the form

Text Box Properties

Height = the height of the text box

Width = the width of the text box

Text Box Move Method

Move x,y = move to the coordinate (x,y)

Thus:

            txtWord.Move 0,0

moves the text box to position (0,0)

Therefore, the three messages to the text box (above) can be restated as:

  1. txtWord.Move 0,0
  2. txtWord.Height = frmWord.ScaleHeight
  3. txtWord.Width = frmWord.Width

 

This code is added to the Form_Load( ) event.

Try this to make sure that it works properly.

Now that it is working properly, run the program and resize the form. Notice that the text box does not resize itself! (Why should it? We didn't "tell" it to.) To fix this problem, add the code to match the text box height and width to the form to the Form_Resize event. To do this:

bullet double-click on the form
bullet in the right hand drop-down menu, select Resize

Now we are ready to add the menu.

Adding Menus

To add a menu, use Visual Basic's build-in Menu Editor. It can be found under the Tools menu. (Note: the form must be active or this item is not enabled.)

You will now see the VB Menu Editor

Notice that as you create new menu items, you must give them both a name and a caption- just like any other object. All menu items begin with the prefix mnu. As is traditional, the first main menu will be the File menu. Type in the caption &File and the name mnuFile and press OK. Run your project and you will see the menu item, which, of course, does nothing yet! Now we will add the rest of the file menu (we are "designing the interface" here!) The first sub-menu under File is New. Go back to the Menu Editor (Tools-Menu Editor) and add &New as a caption and mnuNew as a name. Since New is a sub-menu, it must be "demoted" a level. Clicking the right arrow at the middle left of the Menu Editor does this. When correct, the Menu Editor looks like this:

You should now be able to add the following sub-menus.

Caption

Name

&Open

mnuOpen

&Save

mnuSave

Save &As

mnuSaveAs

Now we want to add a separator bar to the menu. Windows uses these bars to visually group menu items. To add a separator bar, give it the caption "-" (the dash- no quotes) and a name (use mnuFileSep1).

bullet Add a menu item for printing (caption: &Print, name: mnuPrint).
bullet Add another separator bar (caption: -, name: mnuFileSep2).
bullet Finally add the last of the File commands Exit, (caption: &Exit, name: mnuExit).

Now add the other menu items. Don't forget to use the left and right arrows in the Menu editor to "promote" and demote as necessary.        

The other menus are:

Caption

Name

&Edit

mnuEdit

            Cu&t (Ctrl+X)
            &Copy  (Ctrl+C)
            &Paste (Ctrl+V)
            &Delete
            -
            Select &All
            Time/&Date
            -
            Set &Font

            mnuCut
            mnuCopy
          mnuPaste
            mnuDelete
            mnuEditSep1
            mnuSelectAll
            mnuTimeDate
            mnuEditSep2
            mnuFont

&Search
           &Find
            Find &Again
            Find/&Replace

mnuSearch
            mnuFind
            mnuFindAgain
            mnuFindReplace

&Help
            &About DB's Word

mnuHelp
            mnuAbout

Now we are ready to begin the serious work of coding!

First, lets provide messages for each menu item that indicates they are "in progress." Place a message box behind each menu item.

MsgBox "This feature is in progress",,"Message"

Now we can program the Exit button. Obviously, this is easy:

Unload Me

However, this is not how we expect a word processor to work, is it? When you choose "Exit" from a program, it seems to "know" weather you have saved your work or not. If you haven't, a message box should appear and ask if you would like to save your work before exiting. How can we accomplish this?

Suppose that we had a Boolean variable called Saved that is originally set to True (meaning that it is true that there is no work to be saved). It can be changed to False when something is typed in the text box. So--

In General Declarations

Private Saved As Integer 'Booleans are integers- 0 or 1

In Form_Load

Saved = True 'this is legal in VB

In txtWord_Change( )

Saved = False

In mnuEnd

if (Saved) Then

   Unload Me

Else

   'Save

End If

In mnuSave

reset Save to False

Similarly, the New command should do more than simply clear the screen.

txtWord.Text = ""

It should also check to see if the work has been saved and, if it has not, ask the user if they are sure they want to erase the work. Use a similar approach to Save to program this menu item.

Homework

Create the Word Processing .2 interface

Place messages behind each item to indicate "in progress"

Program New and Exit