| Skype: | TextControlSupport | |
| Orders: | 877-462-4772 |

| 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 |
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.
We still get many emails from customers who don't know how to do a customized search through the contents of a TX Text Control. They say: "The built-in dialog is rather nice, but it's not really what I need!"
As of version 6.0 there is a method available that makes Search&Replace routines a piece of cake!
In the following example, the string "TX" is replaced with "New Text" and the inserted text is marked red. Read all about it:
Const SEARCHUP = 1 Const MATCHCAS = 4 Const NOHILITE = 8 Const NOMSGBOX = 16 Private Sub Command1_Click() Dim pos As Long Dim oldSt, oldLen As Integer TXTextControl1.FormatSelection = True oldSt = TXTextControl1.SelStart oldLen = TXTextControl1.SelLength pos = TXTextControl1.Find("TX", 0, NOMSGBOX) While (pos <> -1) TXTextControl1.ForeColor = vbRed TXTextControl1.SelText = "New Text" pos = TXTextControl1.Find("TX", pos + 1, NOMSGBOX) Wend TXTextControl1.SelStart = oldSt TXTextControl1.SelLength = oldLen TXTextControl1.SetFocus TXTextControl1.Refresh End Sub
Const SEARCHUP = 1; MATCHCAS = 4; NOHILITE = 8; NOMSGBOX = 16; procedure TForm1.Button1Click(Sender: TObject); var pos : LongInt; oldSt, oldLen : Integer; begin TXTextControl1.FormatSelection := True; oldSt := TXTextControl1.SelStart; oldLen := TXTextControl1.SelLength; pos := TXTextControl1.Find('TX', 0, NOMSGBOX); While (pos <> -1) do begin TXTextControl1.ForeColor := clRed; TXTextControl1.SelText := 'New Text'; pos := TXTextControl1.Find('TX', pos + 1, NOMSGBOX); end; TXTextControl1.SelStart := oldSt; TXTextControl1.SelLength := oldLen; TXTextControl1.SetFocus; TXTextControl1.Refresh; end;
The first parameter of the Find method is the string that should be searched for. The second parameter is the input position from where the search should start. The last parameter is the sum of several switches. Refer to the manual to find out more about these switches. If you want to find all occurrences of a string in the document, you have to start the search again from the point where the last occurrence has been found until the word is no more found. That's how the code above works. More from us next week!