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

WEBINAR:

On-Demand

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:

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:

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.