devxlogo

Building a Text Editor, Part IV

Building a Text Editor, Part IV

n this final installment of the text editor project, we’re going to be adding these commonly used features:

  • Find, Find Again
  • Cut, Copy, Paste

As usual, the final code is available for download by clicking here. 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		&EditmnuEditCut		Cu&t		Ctrl+XmnuEditCopy		&Copy		Ctrl+CmnuEditPaste		&Paste		Ctrl+V

The code behind each of these menu choices is shown here:

Private Sub mnuEditCopy_Click()   Clipboard.SetText txtData.SelTextEnd SubPrivate Sub mnuEditCut_Click()   Clipboard.SetText txtData.SelText   txtData.SelText = ""End SubPrivate Sub mnuEditPaste_Click()   txtData.SelText = Clipboard.GetTextEnd 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 lngPos As Long      m_strSearch = InputBox("Enter the text to find.", "Find Text")   If m_strSearch = "" Then Exit Sub   lngPos = InStr(1, txtData.Text, m_strSearch, vbTextCompare)   If lngPos > 0 Then      txtData.SelStart = lngPos - 1      txtData.SelLength = Len(m_strSearch)   Else      m_strSearch = ""      MsgBox "Search text was not found.", vbExclamation   End IfEnd SubPrivate Sub mnuSearchFindAgain_Click()   Dim lngPos As Long      If m_strSearch = "" Then Call mnuSearchFind_Click      lngPos = InStr(txtData.SelStart + txtData.SelLength, _      txtData.Text, m_strSearch, vbTextCompare)   If lngPos > 0 Then      txtData.SelStart = lngPos - 1      txtData.SelLength = Len(m_strSearch)   Else      m_strSearch = ""      MsgBox "Search text was not found.", vbExclamation   End If   End Sub

You should also declare the m_strSearch variable as a String up in the declarations section of the form. This will let the data be shared between the two subroutines.

devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist