I know that richtextboxes can detect links (like http://www.yahoo.com) but is there a way for me to add links to it that looks like text but its a link? Like where you can choose the label of the link? For example instead of it appearing as http://www.yahoo.com it appears as Click here to go to yahoo
edit: forgot, im using windows forms
edit: is there something thats better to use (as in easier to format)?
Here you can find an example of adding a link in rich Textbox by linkLabel:
And here is the handler:
The standard RichTextBox control (assuming you are using Windows Forms) exposes a rather limited set of features, so unfortunately you will need to do some Win32 interop to achieve that (along the lines of SendMessage(), CFM_LINK, EM_SETCHARFORMAT etc.).
You can find more information on how to do that in this answer here on SO.
Of course it is possible by invoking some WIN32 functionality into your control, but if you are looking for some standard ways, check this post out: Create hyperlink in TextBox control
There are some discussions about different ways of integration.
greetings
Update 1: The best thing is to follow this method: http://msdn.microsoft.com/en-us/library/f591a55w.aspx
because the RichText box controls provides some functionality to "DetectUrls". Then you can handle the clicked links very easy:
and you can simple create your own RichTextBox contorl by extending the base class - there you can override the methods you need, for example the DetectUrls.
I found a way which may not be the most elegant, but it's just a few lines of code and does the job. Namely, the idea is to simulate hyperlink appearance by means of font changes, and simulate hyperlink behavior by detecting what the mouse pointer is on.
The code:
To improve on the code, one could create an elegant way to map multiple "hot text" strings to their own linked URLs (a
Dictionary<K, V>
maybe). An additional improvement would be to subclassRichTextBox
to encapsulate the functionality that's in the code above.