Link to home
Start Free TrialLog in
Avatar of Marco Gasi
Marco GasiFlag for Spain

asked on

Loading large file in a TRichEdit

Hi all this question is related this one: https://www.experts-exchange.com/questions/28407456/Loading-large-text-files.html?anchorAnswerId=39986818#a39986818

I thought the problem was the StringList but the bottleneck is TRichEdit. The file is very fastly loaded in the StringList but it takes a long time to be loaded in TRichEdit and when it is loaded it freezes the program. I use TRichEdit because I have a piece of code which allows me to use a different font color for certain words.
In my case, I highlight all non existent paths to allow the user to edit them accordingly to his system.

How can solve this problem?
Thanks in advance
Marco
ASKER CERTIFIED SOLUTION
Avatar of aikimark
aikimark
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Marco Gasi

ASKER

Okay, aikimark, Listbox is very fast! But I still have an issue with the OwnerDraw event. Here's my code:

procedure TfrmIncorrectPaths.lbxRegFileDrawItem(Control: TWinControl;
  Index: Integer; Rect: TRect; State: TOwnerDrawState);
var
  myColor: TColor;
  myBrush: TBrush;
  rx: TPerlRegEx;
  I: Integer;
begin
  myBrush := TBrush.Create;
  try
    rx := TPerlRegEx.Create;
    try
      with (Control as TListBox).Canvas do
      begin
          rx.Options := [preCaseLess,preExtended];
          rx.RegEx := '\b[a-z]:\\                    # Drive'#10 +
                      '(?:[^\\/:*?"<>|\r\n]+\\)*     # Folder'#10 +
                      '[^\\/:*?"<>|\r\n]*            # File';
          rx.Subject := lbxRegFile.Items[Index];
          if rx.Match then
          begin
            if (not DirectoryExists(ExtractFilePath(rx.MatchedText))) then
              myColor := clYellow
            else
              myColor := clWhite;
          end;
        myBrush.Style := bsSolid;
        myBrush.Color := myColor;
        Winapi.Windows.FillRect(handle, Rect, myBrush.Handle) ;
        Brush.Style := bsClear;
        TextOut(Rect.Left, Rect.Top,
                 (Control as TListBox).Items[Index]) ;
      end;
    finally
      rx.Free;
    end;
  finally
    MyBrush.Free;
  end;
end;

Open in new window


It correctly draws a yellow background for non existent directories (it highlights the whole item instead of the directory string only but I can accept it), but all the rest is black! It's sure because I don't understand brush tecniques: can you help me?
The only difference I notice between your code and the article's code is
Winapi.Windows.FillRect()
vs.
Windows.FillRect()

What version of Delphi are you using?
XE3. If I only use Windows.FillRect I get 'Undeclared identifier: Windows' error when I try to compile.
Solved. The working code is the following:

procedure TfrmIncorrectPaths.lbxRegFileDrawItem(Control: TWinControl;
  Index: Integer; Rect: TRect; State: TOwnerDrawState);
var
  myColor: TColor;
  myBrush: TBrush;
  rx: TPerlRegEx;
  I: Integer;
begin
  myBrush := TBrush.Create;
  try
    rx := TPerlRegEx.Create;
    try
      rx.Options := [preCaseLess,preExtended];
      rx.RegEx := '\b[a-z]:\\                    # Drive'#10 +
                  '(?:[^\\/:*?"<>|\r\n]+\\)*     # Folder'#10 +
                  '[^\\/:*?"<>|\r\n]*            # File';
      rx.Subject := lbxRegFile.Items[Index];
      if rx.Match then
      begin
        if (not DirectoryExists(ExtractFilePath(rx.MatchedText))) then
          myColor := clYellow
        else
          myColor := clWhite;
      end
      else
        myColor := clWhite;
      with (Control as TListBox).Canvas do
      begin
        myBrush.Style := bsSolid;
        myBrush.Color := myColor;
        Winapi.Windows.FillRect(handle, Rect, myBrush.Handle) ;
        Brush.Style := bsClear;
        TextOut(Rect.Left, Rect.Top,
                 (Control as TListBox).Items[Index]) ;
      end;
    finally
      rx.Free;
    end;
  finally
    MyBrush.Free;
  end;
end;

Open in new window


Thanks
Thank you so mutch!
You're welcome.

Ah.  I see the problem now as well.  Thanks for posting the fixed/correct code. I missed the default white color being lost.  Maybe it is only a style difference, but you might try the following structure to see if it is faster than the else clause.
      myColor := clWhite;
      if rx.Match then
        if (not DirectoryExists(ExtractFilePath(rx.MatchedText))) then
          myColor := clYellow;

Open in new window

Note: I edited my comment, tweaking the code for simplicity.
Okay, I'll try it. Yhank you for this further help. Please, if you can take a look at the other quesiton: https://www.experts-exchange.com/questions/28408505/Regex-issue-maybe.html
Cheers