• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 291
  • Last Modified:

Text Formatting

Assuming I have a TMemo Control on a form how could i highlite (i.e. make bold) every instance of a word.  Kind of like a very cheap and easy syntax highlighting thingamy.  

Many Thanks In Advance.

James
0
phuzzy
Asked:
phuzzy
  • 9
  • 4
  • 2
  • +2
1 Solution
 
ginsonicCommented:
You wish to bold a specified word into memo lines ?
0
 
Wim ten BrinkSelf-employed developerCommented:
Use a TRichEdit control, not a TMemo control A TMemo control doesn't support any highlighting, only selecting of part of the text between two positions.
0
 
cubudCommented:
Use richedit
then use SelStart, SelLength and SelAttributes properties.

Look at the richedit demo in the DEMOS directory.
0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

 
classmateCommented:
Solution 1:
Use capitals for highlighting

Solution 2:
Use a TRichEdit instead.


In either way you can use the StringReplace function to do what you want, but with TRichEdit controls it's a bit more complicated. Have to use the Lines.SaveToStream/LoadFromStream methods and make a string point to the data you want to change. Next, you need to know RichEdit formatting.

Classmate.
0
 
ginsonicCommented:
You can use ColorMemo from http://www.torry.net/memos.htm .
Is freeware , but no source codes .
0
 
ginsonicCommented:
Same address for ESColorMemo v.1.0 . Freeware with source code . Tested and work perfect .
0
 
ginsonicCommented:
And many anothers alike freware memos (TGSMemo & TGSMemoHTMLCode v.1.0 ..etc)
0
 
classmateCommented:
Solution 1:

Memo.Text :=
  StringReplace (
    Memo.text, 'myword', 'MYWORD',
    [rfReplaceAll, rfIgnoreCase]
  );


Solution 2:

procedure TForm1.ButtonHighLightClick(Sender: TObject);
const
  boldon = '\b ';
  boldoff = '\b0 ';
var
  Replacement : string;
  RStream : TMemoryStream;
begin
  RStream := TMemoryStream.Create;
  Richedit1.Lines.SaveToStream(RStream);

  SetLength (Replacement, RStream.size);
  StrCopy (PChar(Replacement), RStream.memory);

  replacement := StringReplace (replacement, 'myword', boldon + 'myword' + boldoff, [rfReplaceAll, rfIgnoreCase]);

  RStream.Size := Length(Replacement);
  StrCopy (RStream.Memory, PChar(Replacement));

  RStream.Seek(0, soFromBeginning);
  RichEdit1.Lines.LoadFromStream(RStream);

  RStream.Free;
end;


classmate.
0
 
ginsonicCommented:
Whay to use RichEdit when the question is about TMemo ?
And is possible to do that with Memo . I just tested couple ( freware with source code ) components from Torry .
0
 
classmateCommented:
Out of curiousity, do theese components have one simple method of highligthing the desired words?

0
 
classmateCommented:
(this freeware code, i meant :-) )
0
 
cubudCommented:
My favourite component is SynEdit

http://sourceforge.net/projects/synedit/

You can specify you own rules for highlighting, and it comes with some pre-defined rules (Pasca, Java, etc).

http://www.HowToDoThings.com
0
 
ginsonicCommented:
In ESColorMemo v.1.0 you can choice how many words you wish and assign for each own color .
Have a problem . If select couple words and then deselect, the text is paint with default color . But this is very easy to fix . Just add a new Refresh message into component .
0
 
ginsonicCommented:
Else wait 2-3 weeks to release my transparent/own transparence memo :) Will have this features , too .
0
 
phuzzyAuthor Commented:
hello everyone... sorry about the time delay....

ginsonic : how do you use that ESColorMemo control... i dont understand the TcolorItem/s stuff... any chance you could supply me with a sample section of code????

Cant find any documentation on it either.  But it seems very good.
0
 
ginsonicCommented:
What kind of code ? Can be more specified ?
0
 
phuzzyAuthor Commented:
no in fact it's not important any more i figured out how it wrks but it's not brilliant... but it will do.

Thanks

James
0
 
ginsonicCommented:
See you ... on net :)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

  • 9
  • 4
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now