2. Adding a menu to the project

In this chapter, we will add a menu to the text processor to enable you to call some of the built-in dialog boxes.

Drag a Main Menu control on your form and create a File menu with the items Load... and Save....

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

[C#]
private void mnuFile_OpenFile_Click(object sender, System.EventArgs e)
{
    textControl1.Load(TXTextControl.StreamType.RichTextFormat | TXTextControl.StreamType.HTMLFormat);
}

private void mnuFile_SaveFileAs_Click(object sender, System.EventArgs e)
{
    textControl1.Save(TXTextControl.StreamType.RichTextFormat | TXTextControl.StreamType.HTMLFormat);
}
[Visual Basic]
Private Sub mnuFile_OpenFile_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles mnuFile_OpenFile.Click
    TextControl1.Load(TXTextControl.StreamType.RichTextFormat + TXTextControl.StreamType.HTMLFormat)
End Sub

Private Sub mnuFile_SaveFileAs_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles mnuFile_SaveFileAs.Click
    TextControl1.Save(TXTextControl.StreamType.RichTextFormat + TXTextControl.StreamType.HTMLFormat)
End Sub

The Load and Save methods will automatically open a dialog box for the user to select a filename and file format, and then load, respectively save, the selected file.

Now for the Edit menu. Create an Edit menu containing items for Cut, Copy and Paste. The code for these menu items is:

[C#]
private void mnuCut_Click(object sender, System.EventArgs e)
{
    textControl1.Cut();
}

private void mnuCopy_Click(object sender, System.EventArgs e)
{
    textControl1.Copy();
}

private void mnuPaste_Click(object sender, System.EventArgs e)
{
    textControl1.Paste();
}
[Visual Basic]
Private Sub mnuEdit_Cut_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles mnuEdit_Cut.Click
    TextControl1.Cut()
End Sub

Private Sub mnuEdit_Copy_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles mnuEdit_Copy.Click
    TextControl1.Copy()
End Sub

Private Sub mnuEdit_Paste_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles mnuEdit_Paste.Click
    TextControl1.Paste()
End Sub

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

Now, create a Format menu with the items Character... and Paragraph.... Name the items mnuFomat_Character and mnuFormat_Paragraph and add the following code to their procedures.

[C#]
private void mnuFormat_Character_Click(object sender, System.EventArgs e)
{
    textControl1.FontDialog();
}

private void mnuFormat_Paragraph_Click(object sender, System.EventArgs e)
{
    textControl1.ParagraphFormatDialog();
}
[Visual Basic]
Private Sub mnuFormat_Character_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles mnuFormat_Character.Click
    TextControl1.FontDialog()
End Sub

Private Sub mnuFormat_Paragraph_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles mnuFormat_Paragraph.Click
    TextControl1.ParagraphFormatDialog()
End Sub

<< Previous step