Discussion:
Memo/Richedit with Hyperlink capabilities, web aware
(too old to reply)
Sven Parnol
2012-04-02 16:56:57 UTC
Permalink
My client asked if I could add hyperlink capability to TDBMemo control
which is connected to their own customer database. So the idea being
that they could freely add text and www-hyperlinks, the Control would
recognize Hyperlinks and when clicked, open them in Default Browser.

My first impression was that that could be done. But not easily. Dealing
with plain text and old TDBMemo controls, I thought it would be like
starting to add hyperlink capability to 16-bit Notepad application.

These were the links I found first:
http://www.scalabium.com/faq/dct0146.htm
http://delphi.about.com/od/vclwriteenhance/l/aa051804a.htm

The About.com.tip is a bit tricky and complicated. The Scalabium sample
almost does what is needed. But as with About.com tip, also their
TRichEdit control needs to be placed directly on the Form. If you place
TRichEdit control for instance on TPanel, the hyperlinks just stop working.

At this phase it started to look a bit complicated to get TRichEdit to
react on any Control.

Finally I found a tip telling that some hypertext capability was also
maybe in Rxlib Library. Bingo, there they were!
This useful RxLib library has been installed on my machine for almost 10
years, but I had not noticed this earlier. Both TRichEdit and database
aware TDBRichEdit replacement, with built in web/hypertext capability:
TRxRichEdit
TRxDBRichEdit

procedure TForm1.RxDBRichEdit1URLClick(Sender: TObject;
begin
ShellExecute(Handle, 'open', PChar(URLText), 0, 0, SW_SHOWNORMAL);
end;

The nice thing is that the text does not need to be saved as RichText
format. These controls recognize links like http://www.google.com or
mailto:***@microsoft.com straight from plain text. Or from Memo
type plain text Database field. So I just need to replace old TDbMemo
control with TRxDBRichEdit controls, and I get nicely looking, modern
hyperlink capable functionality, that works also with all the existing data.

Here's one more enchancement, that I made myself. These controls are
even a bit too eager to react on mouse clicks over www-links. If you
would just like to edit the Memo text, and try to place cursor inside
hyperlink area the control reacts right away and opens the link.

To avoid this, I use this code:

procedure TForm1.RxDBRichEdit1URLClick(Sender: TObject;
begin
FURLText := URLText; // Save the clicked www-link.
end;

procedure TEdCustForm.RxRichEd1DblClick(Sender: TObject);
// This way Web links open only when you double click the link.
begin
if FURLText <> '' then
begin
ShellExecute(Handle, 'open', PChar(URLText), 0, 0, SW_SHOWNORMAL);
FURLText:='';
end;
end;

---
That's it. The final code is not rocket science. Yet it took me almost
two days to find all this. And to get the hyperlink clicks and Memo
editing to work without hassle.

I wrote this article because the working solution was so simple after
all. Yet the hyperlink capability this brings to TDbMemo fields looks
very nice. Hopefully this saves someone else that two days I had to take.

Some tip pay back for the Delphi community:)
-SP
P E Schoen
2012-05-27 00:38:05 UTC
Permalink
Thanks for the information. I don't need the hyperlink capability, but I
would like some info on the differences between TMemo and TRichEdit. I was
using a memo to display text from a serial port, and I had problems. Another
serial port terminal demo used a TRichEdit, and I thought perhaps it was
better for some reason. But I tried both, using the new ComDrv32 component,
and they seemed to work identically well. The help documentation describes
the properties and methods in common, but the advantages or drawbacks to one
or the other are not clear.

Thanks,

Paul
Jamie
2012-05-27 01:13:23 UTC
Permalink
Post by P E Schoen
Thanks for the information. I don't need the hyperlink capability, but I
would like some info on the differences between TMemo and TRichEdit. I
was using a memo to display text from a serial port, and I had problems.
Another serial port terminal demo used a TRichEdit, and I thought
perhaps it was better for some reason. But I tried both, using the new
ComDrv32 component, and they seemed to work identically well. The help
documentation describes the properties and methods in common, but the
advantages or drawbacks to one or the other are not clear.
Thanks,
Paul
Hmm

There should be no difference between the two however, you must to
care in not using any RTF (rich text format) combinations from your data
in a Richedit otherwise, you'll get some unexpected results, unless of
course, that is the results you expect!

Jamie

Loading...