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

Search & replace with 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

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:

Visual Basic

  1. Const SEARCHUP = 1
  2. Const MATCHCAS = 4
  3. Const NOHILITE = 8
  4. Const NOMSGBOX = 16
  5.  
  6. Private Sub Command1_Click()
  7.  
  8. Dim pos As Long
  9. Dim oldSt, oldLen As Integer
  10.  
  11. TXTextControl1.FormatSelection = True
  12. oldSt = TXTextControl1.SelStart
  13. oldLen = TXTextControl1.SelLength
  14.  
  15. pos = TXTextControl1.Find("TX", 0, NOMSGBOX)
  16. While (pos <> -1)
  17. TXTextControl1.ForeColor = vbRed
  18. TXTextControl1.SelText = "New Text"
  19. pos = TXTextControl1.Find("TX", pos + 1, NOMSGBOX)
  20. Wend
  21.  
  22. TXTextControl1.SelStart = oldSt
  23. TXTextControl1.SelLength = oldLen
  24.  
  25. TXTextControl1.SetFocus
  26. TXTextControl1.Refresh
  27.  
  28. End Sub

Delphi

  1. Const SEARCHUP = 1;
  2. MATCHCAS = 4;
  3. NOHILITE = 8;
  4. NOMSGBOX = 16;
  5.  
  6. procedure TForm1.Button1Click(Sender: TObject);
  7.  
  8. var
  9. pos : LongInt;
  10. oldSt, oldLen : Integer;
  11.  
  12. begin
  13. TXTextControl1.FormatSelection := True;
  14. oldSt := TXTextControl1.SelStart;
  15. oldLen := TXTextControl1.SelLength;
  16.  
  17. pos := TXTextControl1.Find('TX', 0, NOMSGBOX);
  18. While (pos <> -1) do begin
  19. TXTextControl1.ForeColor := clRed;
  20. TXTextControl1.SelText := 'New Text';
  21. pos := TXTextControl1.Find('TX', pos + 1, NOMSGBOX);
  22. end;
  23.  
  24. TXTextControl1.SelStart := oldSt;
  25. TXTextControl1.SelLength := oldLen;
  26.  
  27. TXTextControl1.SetFocus;
  28. TXTextControl1.Refresh;
  29. 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!

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