Adding a Menu

Visual Basic User's Guide > Creating a Simple Word Processor

In this chapter, you will add a menu to the text processor that enables you to call the Text Control's built-in dialog boxes.

Use the Visual Basic Menu Design Window to create a Format menu with the items Character... and Paragraph.... Name the items mnuFomat_Character and mnuFormat_Paragraph. (Please refer to the Visual Basic documentation, if you need help with creating menus).

Add the following code to the Click procedures of the menu items:

[Visual Basic 6]
Private Sub mnuFormat_Character_Click()
   TXTextControl1.FontDialog
End Sub
Private Sub mnuFormat_Paragraph_Click()
   TXTextControl1.ParagraphDialog
End Sub

Start the program again. You should be able to use the menu items to call the Font and Paragraph dialog boxes.

Now for the Edit menu. Again use the Menu Design Window and create an Edit menu that contains items for Cut, Copy, and Paste. The code for these menu items is:

[Visual Basic 6]
Private Sub mnuEdit_Cut_Click()
   TXTextControl1.Clip 1
End Sub
Private Sub mnuEdit_Copy_Click()
   TXTextControl1.Clip 2
End Sub
Private Sub mnuEdit_Paste_Click()
   TXTextControl1.Clip 3
End Sub

Having added these menu items, you can exchange formatted text with other word processors via the clipboard.

The last menu for now shall be a simple file menu. Create a File menu including the items Load... and Save As.... Place a common dialog box icon on the form and enter the following code, which will call the common dialog box to get a file name from the user, and will then load respectively save the selected file:

[Visual Basic 6]
Private Sub mnuFile_Load_Click()
   On Error Resume Next
   ' Create an "Open File" dialog box
   CommonDialog1.Filter = "TX Demo (*.tx)|*.tx"
   CommonDialog1.DialogTitle = "Open"
   CommonDialog1.Flags = cdlOFNFileMustExist Or _
      cdlOFNHideReadOnly
   CommonDialog1.CancelError = True
   CommonDialog1.ShowOpen
   If Err Then Exit Sub
   ' Pass the filename to the Text Control
   TXTextControl1.Load CommonDialog1.filename, 0
End Sub
Private Sub mnuFile_SaveAs_Click()
   On Error Resume Next
   ' Create a "Save File" dialog box
   CommonDialog1.Filter = "TX Demo (*.tx)|*.tx"
   CommonDialog1.DialogTitle = "Save As"
   CommonDialog1.Flags = cdlOFNOverwritePrompt Or _
      cdlOFNHideReadOnly
   CommonDialog1.CancelError = True
   CommonDialog1.ShowSave
   If Err Then Exit Sub
   ' Open the selected file
   TXTextControl1.Save CommonDialog1.filename, 0
End Sub