 |
BorlandTalk.com Borland discussion newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Chris L. Guest
|
Posted: Fri Sep 02, 2005 9:54 pm Post subject: TCustomRichEdit font help |
|
|
Hi All,
I need to search and replace text in this control and print it afterwards.
The font property of a TCustomRichEdit ( I use wwDBRichEditMSWord1 in
IP4000) is set to 'TimesNewRoman" at design time. I cut and paste a block of
formated text into the control at runtime. Then run
btPrintSingle.Click;
procedure TForm1.ReplaceAllClick(Sender:TObject);
begin
with wwDBRichEditMSWord1 do
begin
Readonly := False;
Lines.beginupdate;
for i:= 0 to DataModule2.AdsCustomer.FieldCount-1 do
begin
with DataModule2.AdsCustomer do
if DataModule2.AdsCustomer.Fields[i].AsString <> ''
then
Text :=
StringReplace(Text,'<'+Fields[i].FieldName+'>',
FieldByName(Fields[i].FieldName).AsString,
[rfReplaceAll,rfIgnoreCase])
else
// Replace with Blank
Text :=
StringReplace(Text,'<'+Fields[i].FieldName+'>',
'',[rfReplaceAll,rfIgnoreCase]);
end;
Lines.endupdate;
end;
end;
procedure TForm1.btPrintSingleClick(Sender: TObject);
begin
DocPrinted := False; // initialize
ShowMessage('Font in this document is ' +
wwDBRichEditMSWord1.Font.Name);
if PrinterSetupDialog1.Execute then
begin
try
Screen.Cursor := crHourglass;
if not Replaced then
btReplaceAll.Click
else
ShowMessage('Your document has already been replaced. '
+ 'Please make sure you are using a template or an '
+ 'unmodified copy of the document');
wwDBRichEditMSWord1.Print('Single Mailing'); // send doc to
Print
DocPrinted := True;
finally
Screen.Cursor := crDefault;
if Replaced then
btUndo.Click;
end;
end;
end;
The resulting text printed out in all 'TimesNewRoman' instead of the
original format.
If I took out the
StringReplace(Text,Oldpattern,NewPattern,[rfReplaceAll,rfIgnoreCase]); then
it printed OK.
I have to use wwDBRichEditMSWord and I must have the search and replace
ability. RichEdit.FindText behaves the same as StringReplace. I want
something simple and do not want to code my own seach and replace routine.
Any suggetion will be greatly appreciated.
Thanks.
Chris
|
|
| Back to top |
|
 |
Bruce Roberts Guest
|
Posted: Sun Sep 04, 2005 2:06 am Post subject: Re: TCustomRichEdit font help |
|
|
"Chris L." <christopher.leongc (AT) verizon (DOT) net> wrote
| Quote: | Hi All,
I need to search and replace text in this control and print it afterwards.
The font property of a TCustomRichEdit ( I use wwDBRichEditMSWord1 in
IP4000) is set to 'TimesNewRoman" at design time. I cut and paste a block
of
formated text into the control at runtime. Then run
btPrintSingle.Click;
procedure TForm1.ReplaceAllClick(Sender:TObject);
begin
with wwDBRichEditMSWord1 do
begin
Readonly := False;
Lines.beginupdate;
for i:= 0 to DataModule2.AdsCustomer.FieldCount-1 do
begin
with DataModule2.AdsCustomer do
if DataModule2.AdsCustomer.Fields[i].AsString <> ''
then
Text :=
StringReplace(Text,'<'+Fields[i].FieldName+'>',
FieldByName(Fields[i].FieldName).AsString,
[rfReplaceAll,rfIgnoreCase])
else
// Replace with Blank
Text :=
StringReplace(Text,'<'+Fields[i].FieldName+'>',
'',[rfReplaceAll,rfIgnoreCase]);
end;
Lines.endupdate;
end;
end;
The resulting text printed out in all 'TimesNewRoman' instead of the
original format.
If I took out the
StringReplace(Text,Oldpattern,NewPattern,[rfReplaceAll,rfIgnoreCase]);
then
it printed OK.
I have to use wwDBRichEditMSWord and I must have the search and replace
ability. RichEdit.FindText behaves the same as StringReplace. I want
something simple and do not want to code my own seach and replace routine.
|
First off, don't use the With statement. While it makes no difference to the
problem you are facing, it will lead, eventually, to serious maintenance
issues.
The difficulty you are having is that you are replacing all of the text in
the control with the new text. If the font face, size, etc. are uniform, you
could simply set the Font properties to those indicated by the control's
SelAttributes property after setting SelLength to 1 and SelStart to zero. An
alternative is to use the control's Find method and manually replace each
occurrence of the text, see SelStart, SelLength, and SelText.
|
|
| Back to top |
|
 |
alanglloyd@aol.com Guest
|
Posted: Sun Sep 04, 2005 6:04 am Post subject: Re: TCustomRichEdit font help |
|
|
It seems that StringReplace() replaces only the text characters.
It appears that you would have to ...
use TRichEdit.FindText to find the next occurrence,
set SelLength to the length of your removed text,
set SelText to your replacement text,
extract the format of your replacement text,
set SelAttributes to that format.
repeat until finished.
Alan Lloyd
|
|
| Back to top |
|
 |
Chris L. Guest
|
Posted: Mon Sep 05, 2005 2:55 pm Post subject: Re: TCustomRichEdit font help |
|
|
Thank you all for your suggestions.
After some testing, here's what I found:
StringReplace() will replace the text and set the font to the font property
of the TRichEdit control. So you would lose the original font properties of
the block of text if it is different from that of the TRichEdit.
StringReplace() can replace all the occurences in on fell swoop.
With TRichEdit.FindText, I need to use a loop to find and replace all the
occurences of a string. It retains the original font property of the
replaced string in the text block and SelLength and SelText is not
necessary.
I'm very grateful for all your help.
Chris
<alanglloyd (AT) aol (DOT) com> wrote
| Quote: | It seems that StringReplace() replaces only the text characters.
It appears that you would have to ...
use TRichEdit.FindText to find the next occurrence,
set SelLength to the length of your removed text,
set SelText to your replacement text,
extract the format of your replacement text,
set SelAttributes to that format.
repeat until finished.
Alan Lloyd
|
|
|
| Back to top |
|
 |
Bruce Roberts Guest
|
Posted: Tue Sep 06, 2005 2:35 pm Post subject: Re: TCustomRichEdit font help |
|
|
"Chris L." <christopher.leongc (AT) verizon (DOT) net> wrote
| Quote: | After some testing, here's what I found:
StringReplace() will replace the text and set the font to the font
property
of the TRichEdit control. So you would lose the original font properties
of
the block of text if it is different from that of the TRichEdit.
StringReplace() can replace all the occurences in on fell swoop.
|
StringReplace sets no fonts! All the function does is replace text. The rich
edit itself is setting the font of newly assigned text. It occurs to me that
something like the following should work
var strm : tStringStream;
strm := tStringStream.Create;
try
richEdit.Lines.SaveToStream (strm);
strm.DataString := StringReplace (strm.DataString, oldPattern,
newPattern, . . .)
strm.Position := 0;
richEdit.Lines.LoadFromStream (strm);
finally
strm.Free;
end;
The reason this should work is that it preserves the formatting commands
embedded in the text.
|
|
| Back to top |
|
 |
Chris L. Guest
|
Posted: Wed Sep 07, 2005 10:24 pm Post subject: Re: TCustomRichEdit font help |
|
|
Hi Bruce,
That's a great idea.
However, strm.DataString is a read only property and I'm not able to execute
strm.DataString := StringReplace (strm.DataString, oldPattern,
newPattern, . . .)
strm.Position := 0;
richEdit.Lines.LoadFromStream (strm);
Some code example will really help.
Thanks.
Chris
"Bruce Roberts" <ber (AT) bounceitattcanada (DOT) xnet> wrote
| Quote: |
"Chris L." <christopher.leongc (AT) verizon (DOT) net> wrote in message
news:U%YSe.9283$aG.5412 (AT) trndny01 (DOT) ..
After some testing, here's what I found:
StringReplace() will replace the text and set the font to the font
property
of the TRichEdit control. So you would lose the original font properties
of
the block of text if it is different from that of the TRichEdit.
StringReplace() can replace all the occurences in on fell swoop.
StringReplace sets no fonts! All the function does is replace text. The
rich
edit itself is setting the font of newly assigned text. It occurs to me
that
something like the following should work
var strm : tStringStream;
strm := tStringStream.Create;
try
richEdit.Lines.SaveToStream (strm);
strm.DataString := StringReplace (strm.DataString, oldPattern,
newPattern, . . .)
strm.Position := 0;
richEdit.Lines.LoadFromStream (strm);
finally
strm.Free;
end;
The reason this should work is that it preserves the formatting commands
embedded in the text.
|
|
|
| Back to top |
|
 |
Bruce Roberts Guest
|
Posted: Thu Sep 08, 2005 3:06 pm Post subject: Re: TCustomRichEdit font help |
|
|
"Chris L." <christopher.leongc (AT) verizon (DOT) net> wrote
| Quote: | Hi Bruce,
That's a great idea.
However, strm.DataString is a read only property and I'm not able to
execute
strm.DataString := StringReplace (strm.DataString, oldPattern,
newPattern, . . .)
strm.Position := 0;
richEdit.Lines.LoadFromStream (strm);
Some code example will really help.
|
.. . .
s := StringReplace (strm.DataString, . . .)
strm.Size := 0;
strm.WriteString (s);
strm.Position := 0;
.. . .
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|