With the RichTextBox control, you can create applications that incorporate an RTF text editor to allow end users to edit documents without starting up a second application. When you incorporate such a text editor into the application, you can provide a toolbar that gives the end user access to common operations such as opening a file and changing font characteristics. This simple scenario populates the Toolbar control with only one Button object. When the user clicks the button, the Open File dialog box is opened.
The following objects are used in this scenario:
To incorporate a toolbar into your application
At design time, populate the ImageList control with the images you will need to populate your Toolbar control. This is easily accomplished.
To populate an ImageList at design time
or
Also at design time, after you have populated the ImageList control, you must associate it with the Toolbar control.
To associate the ImageList control with the Toolbar control at design time
or
Once you have populated the ImageList control and associated it with the Toolbar control, you can begin to create the Button objects themselves. In this scenario, we will create two buttons, one with the Placeholder style, and a second with the Default style.
To add buttons to the Toolbar Control at design time
Because the Open File operation is commonly invoked from a menu bar, we must create a menu first.
To create a menu
Use the CommonDialog control to create an Open File dialog box. This can be done in the Form object's Load event, as shown below:
Private Sub Form_Load()
' Configure dlgOpenFile for opening and
' saving files.
With dlgOpenFile
.DefaultExt = ".rtf"
.Filter = "RTF file (*.RTF) | *.RTF"
End With
End Sub
In the present scenario, the Toolbar control's single button simply represents a common operation, an Open File function, that is also found on the menu bar. Thus, the code for the Open File operation should be placed in the mnuOpen object's Click event, as shown below:
Private Sub mnuOpen_Click()
' Declare a string variable to hold the file name.
' Then invoke the ShowOpen method to show
' the dialog box. Set the variable to the Filename
' property. Finally, load the RichTextBox control.
Dim strOpen As String
dlgOpenFile.ShowOpen
strOpen = dlgOpenFile.Filename
rtfData.LoadFile strOpen, rtfRTF
End Sub
When any Button object on the Toolbar control is clicked, the ButtonClick event occurs. To determine which Button object was clicked, use the Select Case statement with either the Key property or the Index property.
The example below uses the Key property of the Button object.
Private Sub tlbRTF_ButtonClick _
(ByVal Button As Button)
Select Case Button.Key
' User clicked "open file" button.
Case "openFile"
mnuOpen_Click ' Invoke the mnuOpen Click event
End Select
End Sub
You can now run the project and open an RTF file by clicking the button on the toolbar.