troubleshooting Question

Color words in richedit (500 points)

Avatar of rincewind666
rincewind666 asked on
DelphiEditors IDEs
13 Comments1 Solution802 ViewsLast Modified:
Whenever a certain sequence of characters appears in a richedit, I want to color it red.  The richedit contains text such as:
$db = "yes";
$prenum='no';
$prefix = 'ABC';

I am using the following code which turns every "$" in the text red.  I just want to turn all text in the same line red if it has a $, = and either a ' or " in it such as above.  Note that sometimes there  is a space before/after = and sometimes there isn't. Ignore any other times $ appears.

I am using Delphi 6.

I am giving 500 points as this is urgent.  Many thanks for your help.

CODE:

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ComCtrls;

type
  TForm1 = class(TForm)
    RichEdit1: TRichEdit;
    procedure FormShow(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure colorword(ARichedit : TRichedit; AWord : String; AColor : TColor; whole:Boolean; aChar:String);          //  baby123
var
  foundAt,
  oldSelStart,
  oldSelLength,
  first,last : Integer;
begin

  oldSelStart := ARichedit.SelStart;
  oldSelLength := ARichedit.SelLength;
  try
    foundAt := -1;
    repeat
      if whole then begin
        foundAt := ARichEdit.FindText(AWord,foundAt+1,length(ARichEdit.Text),[stWholeWord]);
        if foundAt < 0 then break;
        ARichedit.SelStart := foundAt;
        ARichEdit.SelLength := length(AWord);
        ARichEdit.SelAttributes.Color := AColor;
        ARichEdit.SelAttributes.Style := [fsBold];
      end
      else begin
        foundAt := ARichEdit.FindText(AWord,foundAt+1,length(ARichEdit.Text),[]);
        if foundAt < 0 then break;
        first:=foundAt;
        while (ARichEdit.Text[first] in ['a'..'z']) or (ARichEdit.Text[first] in ['A'..'Z']) do begin
           Dec(first);
        end;
        ARichedit.SelStart := first;
        last:=foundAt+length(AWord)+1;
        while (ARichEdit.Text[last] in ['a'..'z']) or (ARichEdit.Text[last] in ['A'..'Z']) or
              (ARichEdit.Text[last]=aChar) do begin
           Inc(last);
        end;
        ARichedit.SelLength := last-first;
        ARichEdit.SelAttributes.Color := AColor;
        ARichEdit.SelAttributes.Style := [fsBold];
      end;
    until foundAt = -1;
  finally
    ARichedit.SelStart := oldSelStart;
    ARichedit.SelLength := oldSelLength;
  end;
end;

procedure TForm1.FormShow(Sender: TObject);
begin
colorword(RichEdit1,'$', clRed, false, '');
end;

end.

FORM:

object Form1: TForm1
  Left = 237
  Top = 316
  Width = 652
  Height = 215
  Caption = 'Form1'
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -13
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  OldCreateOrder = False
  Position = poScreenCenter
  OnShow = FormShow
  PixelsPerInch = 120
  TextHeight = 16
  object RichEdit1: TRichEdit
    Left = 0
    Top = 0
    Width = 644
    Height = 105
    Align = alTop
    Lines.Strings = (
      'session_start(); '

        'if ($_POST["vercode"] != $_SESSION["vercode"] OR $_SESSION["verc' +
        'ode"]=='''')  {'
      '     echo  ''<strong>Incorrect verification code.</strong><br>'';'
      '} else {'
      '$email = $_POST[''email''] ;'
      '$name = $_POST[''sendername''] ;'
      '$subject = $_POST[''subject''] ;'
      '$message = $_POST[''mesbody''] ;'
      '$db = "yes";'
      '$prenum=''no'';'
      '$prefix = ''ABC'';'
      '$count_my_page = ("hitcounter.txt");'
      '$hits = file($count_my_page);'
      '$hits[0] ++;'
      '$fp = fopen($count_my_page , "w");'
      'fputs($fp , "$hits[0]");'
      'fclose($fp);'
      '//sets number for subject'
      '$subnum = $hits[0];')
    ScrollBars = ssVertical
    TabOrder = 0
  end
end
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 13 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 13 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros