Building a Text Editor, Part IV
By Eric Smith
In this final installment of the text editor
project, we're going to be adding these commonly used features:
 | Find, Find Again
|
 | Cut, Copy, Paste |
This version also includes the features from parts 1-3.
The easiest feature to add is the cut, copy, and paste features. These
features all revolve around the built-in Clipboard object. This object is an
interface to the clipboard used within other Windows applications. This object
is able to store text and objects of any sort. We're going to be using the
object for holding text for this application.
The first step is to add the Edit menu, which typically includes these items: mnuEdit &Edit
mnuEditCut Cu&t Ctrl+X
mnuEditCopy &Copy Ctrl+C
mnuEditPaste &Paste Ctrl+V The code behind each of these menu choices is shown here:Private Sub mnuEditCopy_Click()
Clipboard.SetText Data.SelText
End Sub
Private Sub mnuEditCut_Click()
Clipboard.SetText Data.SelText
Data.SelText = ""
End Sub
Private Sub mnuEditPaste_Click()
Data.SelText = Clipboard.GetText
End Sub
The SelText property of the text box control refers to the text that is
selected. This is a built-in feature of this and the other controls that include
text entry. The SetText method of the Clipboard object stores the text into the
internal clipboard in Windows. The best part is that if you go into Microsoft
Word, you can paste in text from your application since the clipboard is the
same.
For the Cut menu choice, we store the text in the clipboard the same way, but
once we've put it on the clipboard, we need to remove the text from the text box
control. Setting the SelText property to an empty string removes the selected
text from the text box control.
The last menu choice, Paste, uses the GetText method of the Clipboard object
to retrieve whatever text was put there. The SelText property of the text box
control can be used to put the text into the box. This property will work even
if no text is highlighted. If no text is highlighted, the text will be put
wherever the cursor is.
If you want to expand this code, you could use the ActiveControl property of
the Form object to extend these features to more than just one control. As long
as the control has a SelText property, you'll be able to use this code with that
type of control. The TypeOf operator and the TypeName function can help you with
this code.
The other features we're going to implement are the Find and Find Next
features. When the user first does a search, the search will start at the first
character of the text box. If the text is found once, the Find Next feature will
look for the same piece of text starting where the last one was found. Once the
text is not found again, the search is restarted by prompting the user for a new
piece of text. The code for these two routines is shown here: Private Sub mnuSearchFind_Click()
Dim Pos As Long
Search = InputBox("Enter the text to find.", "Find Text")
If Search = "" Then Exit Sub
Pos = InStr(1, Data.Text, Search, vbTextCompare)
If Pos > 0 Then
Data.SelStart = Pos - 1
Data.SelLength = Len(Search)
Else
Search = ""
MsgBox "Search text was not found.", vbExclamation
End If
End Sub
Private Sub mnuSearchFindAgain_Click()
Dim Pos As Long
If Search = "" Then Call mnuSearchFind_Click
Pos = InStr(Data.SelStart + Data.SelLength, _
Data.Text, Search, vbTextCompare)
If Pos > 0 Then
Data.SelStart = Pos - 1
Data.SelLength = Len(Search)
Else
Search = ""
MsgBox "Search text was not found.", vbExclamation
End If
End Sub
You should also declare the Search variable as a String up in the
declarations section of the form. This will allow the data to be shared between the
two subroutines. |