Board index » delphi » Determine BOLD words in a TRichText

Determine BOLD words in a TRichText

I have tried for a week to determine a way to find out  which words,
within a TRichEdit, have been set to bold.   I have tried to use
SelStart and SelLength in conjuction with SelAttributes to determine if
the selected word was = fsBold but this only seemed to work for line one
of the TRichEdit.   Can anyone offer some direction on this?    Thanks
in Advance.
 

Re:Determine BOLD words in a TRichText


Quote
> I have tried for a week to determine a way to find out  which words,
> within a TRichEdit, have been set to bold.   I have tried to use
> SelStart and SelLength in conjuction with SelAttributes to determine if
> the selected word was = fsBold but this only seemed to work for line one
> of the TRichEdit.   Can anyone offer some direction on this?    

Certainly. Here is an example:

unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    RichEdit1: TRichEdit;
    ListBox1: TListBox;
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
var
  S: String;
  wordstart, wordend: Integer;
begin
  listbox1.clear;
  S:= richedit1.text;
  // doing the word search in a string is orders of magnitude faster than
  // doing it in the rich edit control directly.
  wordstart := 0;
  Repeat
    // find start of next word
    repeat
      Inc(wordstart);
    until (wordstart > Length(S)) or IsCharAlpha( S[wordstart] );
    if wordstart <= Length(S) then begin
      // find end of word
      wordend := wordstart;
      repeat
        Inc(wordend);
      until (wordend > Length(S)) or not IsCharAlpha( S[wordend] );

      // we have a word, select it in the rich edit
      with richedit1 do begin
        selstart := wordstart-1; // character index is 0 based!
        sellength := wordend-wordstart;
        // check the attributes
        If (fsBold In SelAttributes.Style) and
           (caBold In SelAttributes.ConsistentAttributes)
        Then
          // we have a winna, add it to the listbox
          listbox1.items.add( Copy( S, wordstart, wordend-wordstart));
      end;
      wordstart := wordend;
    end;
  until wordstart >= Length(S);
end;

end.

Note that it will find word that are bold but also can have other
attributes, e.g. bold + italic, bold+underline. If you want to limit this
to bold-only change the test to [fsBold] = SelAttributes.Style.

Peter Below (TeamB)  100113.1...@compuserve.com)
No e-mail responses, please, unless explicitely requested!

Re:Determine BOLD words in a TRichText


Thanks Peter, it works great but I am now wondering why when I  read each line
of the RichEdit and found/selected each word that my algorithm only corrected
selected the highlighed words from the 1st line of the RichEdit.
Quote
"Peter Below (TeamB)" wrote:
> > I have tried for a week to determine a way to find out  which words,
> > within a TRichEdit, have been set to bold.   I have tried to use
> > SelStart and SelLength in conjuction with SelAttributes to determine if
> > the selected word was = fsBold but this only seemed to work for line one
> > of the TRichEdit.   Can anyone offer some direction on this?

> Certainly. Here is an example:

> unit Unit1;

> interface

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

> type
>   TForm1 = class(TForm)
>     RichEdit1: TRichEdit;
>     ListBox1: TListBox;
>     Button1: TButton;
>     procedure Button1Click(Sender: TObject);
>   private
>     { Private declarations }
>   public
>     { Public declarations }
>   end;

> var
>   Form1: TForm1;

> implementation

> {$R *.DFM}

> procedure TForm1.Button1Click(Sender: TObject);
> var
>   S: String;
>   wordstart, wordend: Integer;
> begin
>   listbox1.clear;
>   S:= richedit1.text;
>   // doing the word search in a string is orders of magnitude faster than
>   // doing it in the rich edit control directly.
>   wordstart := 0;
>   Repeat
>     // find start of next word
>     repeat
>       Inc(wordstart);
>     until (wordstart > Length(S)) or IsCharAlpha( S[wordstart] );
>     if wordstart <= Length(S) then begin
>       // find end of word
>       wordend := wordstart;
>       repeat
>         Inc(wordend);
>       until (wordend > Length(S)) or not IsCharAlpha( S[wordend] );

>       // we have a word, select it in the rich edit
>       with richedit1 do begin
>         selstart := wordstart-1; // character index is 0 based!
>         sellength := wordend-wordstart;
>         // check the attributes
>         If (fsBold In SelAttributes.Style) and
>            (caBold In SelAttributes.ConsistentAttributes)
>         Then
>           // we have a winna, add it to the listbox
>           listbox1.items.add( Copy( S, wordstart, wordend-wordstart));
>       end;
>       wordstart := wordend;
>     end;
>   until wordstart >= Length(S);
> end;

> end.

> Note that it will find word that are bold but also can have other
> attributes, e.g. bold + italic, bold+underline. If you want to limit this
> to bold-only change the test to [fsBold] = SelAttributes.Style.

> Peter Below (TeamB)  100113.1...@compuserve.com)
> No e-mail responses, please, unless explicitely requested!

Re:Determine BOLD words in a TRichText


Quote
In article <3712A0D0.E497A...@pacbell.net>, Cary T. Kellems wrote:
> Thanks Peter, it works great but I am now wondering why when I  read each line
> of the RichEdit and found/selected each word that my algorithm only corrected
> selected the highlighed words from the 1st line of the RichEdit.

I suppose you had an error in your algorithm somewhere. You never showed the
code you used so i have no idea what you did wrong, of course.

Peter Below (TeamB)  100113.1...@compuserve.com)
No e-mail responses, please, unless explicitely requested!

Other Threads