Solved

Loading large file in a TRichEdit

Posted on 2014-04-08
9
369 Views
Last Modified: 2014-04-09
Hi all this question is related this one: http://www.experts-exchange.com/Programming/Languages/Pascal/Delphi/Q_28407456.html#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
0
Comment
Question by:Marco Gasi
  • 5
  • 4
9 Comments
 
LVL 45

Accepted Solution

by:
aikimark earned 500 total points
ID: 39987055
I can offer this work-around.  Use a listbox control and use custom drawing to do your highlighting.  When the user clicks on a listbox item, you move that selected item into an edit control.  Update the listbox item after the user has finished editing.

a code example is in this article:
http://delphi.about.com/cs/adptips2002/a/bltip0602_4.htm
0
 
LVL 30

Author Comment

by:Marco Gasi
ID: 39987167
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?
0
 
LVL 45

Expert Comment

by:aikimark
ID: 39987339
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?
0
 
LVL 30

Author Comment

by:Marco Gasi
ID: 39987846
XE3. If I only use Windows.FillRect I get 'Undeclared identifier: Windows' error when I try to compile.
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 30

Author Comment

by:Marco Gasi
ID: 39987918
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
0
 
LVL 30

Author Closing Comment

by:Marco Gasi
ID: 39987919
Thank you so mutch!
0
 
LVL 45

Expert Comment

by:aikimark
ID: 39988447
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

0
 
LVL 45

Expert Comment

by:aikimark
ID: 39988471
Note: I edited my comment, tweaking the code for simplicity.
0
 
LVL 30

Author Comment

by:Marco Gasi
ID: 39988581
Okay, I'll try it. Yhank you for this further help. Please, if you can take a look at the other quesiton: http://www.experts-exchange.com/Programming/Languages/Pascal/Delphi/Q_28408505.html
Cheers
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now