Login | Register   
RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX

By submitting your information, you agree that devx.com may send you DevX offers via email, phone and text message, as well as email offers about other products and services that DevX believes may be of interest to you. DevX will process your information in accordance with the Quinstreet Privacy Policy.


Building a Text Editor, Part IV

Part 4 of a four-part series on building a text editor in VB. Discusses the Find, Find Again, Cut, Copy, and Paste functions.




Application Security Testing: An Integral Part of DevOps

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 &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 txtData.SelText End Sub Private Sub mnuEditCut_Click() Clipboard.SetText txtData.SelText txtData.SelText = "" End Sub Private Sub mnuEditPaste_Click() txtData.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 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 If End Sub Private 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.

Eric Smith has been a senior consultant with Andersen Consulting, specializing in client/server development using Oracle and Visual Basic, and was senior content developer (Visual Basic) at inquiry.com before it was purchased by Fawcette Technical Publications. He joined the Information Strategies team in September 1997 and is now an independent consultant. He has written or contributed to "Visual Basic 6 Bible" (IDG Books Worldwide, 1998), "Using Visual Basic 6" (Que), and "Inside VBScript with ActiveX" (New Riders Press, 1997). He is certified in Visual Basic 5.0 and Windows Architecture (1 and 2). Reach him here.
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.
Thanks for your registration, follow us on our social networks to keep up-to-date