RichTextBox

  • Thread starter Thread starter Patrick Noll
  • Start date Start date
P

Patrick Noll

hi folks,

i need to integrate links into an RichTextBox.
so, i know the feature that you can DetectUrls when they written like
http://www.pseudolink.de, but that is not what i need.

i want to have text in this box and a single word should be the link.
Ex.: When Profile "Profile" is ready
^^^^^^^^^
Profile should be able to be clicked and fires the event LinkClicked
If you can't imagine exactly what i mean, you can see this functionality
in Outlook Express when you define Message rules.

Extras > Nachrichtenregel > Email (German version)

has anybody an idea how it could work?

patrick
 
Patrick Noll said:
hi folks,

I think I found a way to do this.

Basically, .NET's richtextbox just wraps the Win32 richedit control,
so you can for example send EM_SETCHARFORMAT messages to the control
(using P/invoke) to achieve effects the RichTextBox itself does not
offer (or more precisely: does not expose).

So all you have to do is to set the CFE_LINK attribute for the text
you want to appear (and react) like a link, and you're done.

If you're unsure how to do this, you can take a look at my MSDN
article on printing RichTextBox contents. It also has a (short)
section on how to achieve advanced effects using this method (in the
article I show how to just change the font size of the selection
without modifying the other properties like font style or type).
The URL is
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnwinforms/html/wnf_RichTextBox.asp
(beware URL splitting).

You could very easily add a function "SetSelectionLink" to the
RichTextBoxEx class like this one (along the lines of the existing
SetSelectionBold() function):

public bool SetSelectionLink(bool link)
{
return SetSelectionStyle(CFM_LINK, link ? CFE_LINK : 0);
}

If you use this function to make part of your text a link this part
correctly shows the hand cursor when you move over it and fires the
LinkClicked() event.
And you are not limited to real URL's.

One little caveat, though:
MSDN states that you should NOT use DetectUrls when using CFE_LINK on
your own, so you should set this property to false for your
RichTextBox (true is the default).

Hope this helps,

Martin Müller
 
Back
Top