Solved

Loading large file in a TRichEdit

Posted on 2014-04-08
9
371 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 31

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 31

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 31

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 31

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 31

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Delphi 2010 Export to pdf 2 285
proper way to parse text with delphi 7 100
Working with hours 3 46
Create a path if not exists 7 69
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
A company’s greatest vulnerability is their email. CEO fraud, ransomware and spear phishing attacks are the no1 threat to a company’s security. Cybercrime is responsible for the largest loss of money to companies today with losses projected to r…

919 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

21 Experts available now in Live!

Get 1:1 Help Now