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.
If you want to know more about TX Text Control and databases, browse the archive of past TX Newsletters - we have covered that topic in great depth.
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.
Private Sub CheckLastWord()
Dim lLastWord As Long
Dim lLastReturn As Long
Dim lLastSpace As Long
Dim lCurrentPosAs Long
' Prevent refresh of the TX window
TXTextControl1.LockWindowUpdate = True
' Check which delimiter was the last (Return or Space)
lLastSpace = TXTextControl1.Find(" ", lCurrentPos- 1, 17)
lLastReturn = TXTextControl1.Find(vbLf, lCurrentPos- 1, 17)
' Set lLastWord to the position of the last delimiter
If (lLastSpace > lLastReturn) Then
lLastWord = lLastSpace
lLastWord = lLastReturn
' If lLastWord is -1 the beginning of the text has been reached
' This case has to be handled different
If lLastWord = -1 Then
TXTextControl1.SelStart = 0
TXTextControl1.SelLength = nCurrentPos
TXTextControl1.SelStart = lLastWord + 1
TXTextControl1.SelLength = lCurrentPos- lLastWord - 1
' Now that the last word is selected check if the word is a trigger
For i = 0 To nTriggerCnt - 1
If (TXTextControl1.SelText = szTriggers(i)) Then
TXTextControl1.SelText = szTexts(i)
' Clean up
TXTextControl1.SelStart = nCurrentPos
TXTextControl1.SelLength = 0
TXTextControl1.LockWindowUpdate = False
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.
The Newsletter Team