Skype:TextControlSupport
Orders:877-462-4772
TX Text Control - word processing components.
What is this?Syndicate this content

Text Replacement in TX Text Control ActiveX

This source code snippet requires TX Text Control ActiveX
Author:TX Text Control Support Department
Language:Visual Basic
Version:1.1
Released:March 20, 2001
Last modified:January 11, 2008
Requirements:TX Text Control ActiveX with Visual Basic
Download code:text_replacement.zip Download [4.57 KB, ZIP]

This sample application originally appeared in the TX Text Control Newsletter - why not sign up today and receive sample code like this every week, directly to your Inbox.

Why type the same phrases again and again? Why mistype? Why not let the TX Text Control do repetitive and boring tasks for you, while you take a well-deserved break?

The sample application this week, allows a user to create a file of frequently used phrases, paragraphs, e-mail addresses, clipboard entries, scripts, etc. and invoke them in any application just by typing a short abbreviation (text shortcut).

For example, if the user writes 'br', the word is replaced by the predefined substitute 'Best Regards,'.

What's more, it's dead easy to implement with TX Text Control in just a few lines of code.

Want to know how it is done? Read on...

In the sample application you will be able to choose 'Shortcuts' from the 'Edit' menu to edit the defined trigger, add new ones or delete existing ones.

The keywords used as triggers are placed in a simple string array (szTriggers). The related texts are also stored in a string array (szTexts). The two arrays are expanded dynamically, when a keyword is added (or shrunk when a trigger is deleted).

At this point you could also imagine a small database for the keywords. Using a database would make it possible to keep the edited triggers after the program has been closed which is not possible in the example program.

To check the entered text for keywords we implemented the function CheckLastWord. This function is called when a Return or Space character has been entered.

  1. Private Sub CheckLastWord()
  2.  
  3. Dim lLastWord As Long
  4. Dim lLastReturn As Long
  5. Dim lLastSpace As Long
  6. Dim lCurrentPosAs Long
  7.  
  8. lCurrentPos= TXTextControl1.SelStart
  9.  
  10. ' Prevent refresh of the TX window
  11. TXTextControl1.LockWindowUpdate = True
  12.  
  13. ' Check which delimiter was the last (Return or Space)
  14. lLastSpace = TXTextControl1.Find(" ", lCurrentPos- 1, 17)
  15. lLastReturn = TXTextControl1.Find(vbLf, lCurrentPos- 1, 17)
  16.  
  17. ' Set lLastWord to the position of the last delimiter
  18. If (lLastSpace > lLastReturn) Then
  19. lLastWord = lLastSpace
  20. Else
  21. lLastWord = lLastReturn
  22. End If
  23.  
  24. ' If lLastWord is -1 the beginning of the text has been reached
  25. ' This case has to be handled different
  26. If lLastWord = -1 Then
  27. TXTextControl1.SelStart = 0
  28. TXTextControl1.SelLength = nCurrentPos
  29. Else
  30. TXTextControl1.SelStart = lLastWord + 1
  31. TXTextControl1.SelLength = lCurrentPos- lLastWord - 1
  32. End If
  33.  
  34. ' Now that the last word is selected check if the word is a trigger
  35. For i = 0 To nTriggerCnt - 1
  36. If (TXTextControl1.SelText = szTriggers(i)) Then
  37. TXTextControl1.SelText = szTexts(i)
  38. GoTo Done
  39. End If
  40. Next i
  41.  
  42. ' Clean up
  43. TXTextControl1.SelStart = nCurrentPos
  44. Done:
  45. TXTextControl1.SelLength = 0
  46. TXTextControl1.LockWindowUpdate = False
  47.  
  48. End Sub

First of all the function saves the current input position to a variable. This is necessary to restore the current input position after we are done.

Afterwards, we use the new TX function LockWindowUpdate to prevent the user seeing that text is selected. If you do no have TX Text Control v8, just comment out that line. :-)

Then the starting position of the last word entered is ascertained. This is done by looking for the last Space or Return in the text.

After that lLastWord holds the input position of the first character of the last word. If lLastWord is -1 the entered word is the first word in the text. This case has to be treated different.

Finally, the last word is selected and compared with the list of triggers. If a match is found, the selected text is replaced.

At the end of the function the input position is set back and the refreshing of the TX window is permitted again.

top

Top 10 Bestselling Product Award 2007Top 25 Publisher Product Award 2007Top 10 Bestselling Product Award 2007 in JapanTop 25 Bestselling Product Award 2006Top 25 Bestselling Publisher Award 2006Reader's Choice Award, dot.net magazin, 3rd place