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.

Share the Post:
Heading photo, Metadata.

What is Metadata?

What is metadata? Well, It’s an odd concept to wrap your head around. Metadata is essentially the secondary layer of data that tracks details about the “regular” data. The regular

XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several

data fivetran pricing

Fivetran Pricing Explained

One of the biggest trends of the 21st century is the massive surge in analytics. Analytics is the process of utilizing data to drive future decision-making. With so much of

kubernetes logging

Kubernetes Logging: What You Need to Know

Kubernetes from Google is one of the most popular open-source and free container management solutions made to make managing and deploying applications easier. It has a solid architecture that makes