This feature is available out-of-the-box starting from version 13.1. See HTML Text Formatting
For earlier versions:
This example illustrates how to create a custom LabelControl so that it can display hyperlinks. For this, enable the LabelControl.AllowHtmlString property and use the tags to add a hyperlink to a text.
Question Comments
Added By: Oleg Khozyainov at: 7/18/2012 2:35:12 AM
The sample does not work with 11.2 and 12.1.
I get Null reference exception in GetLinkText method when I move mouse pointer over the hyperlink.
Here is how I changed it, so it works again now:
GetLinkText method used reflection to call StringInfo private properties.
Now, these properties are public.
I changed GetLinkText code to:
List<StringBlock> blocks = ViewInfo.StringInfo.Blocks;
List<Rectangle> blocksBounds = ViewInfo.StringInfo.BlocksBounds;
and it works fine now.
This doesn't work very well when used with images or links with mailto: syntax. Not sure why. Anyway, I really wish that DevExpress includes that functionality in their suite with full support.
Added By: Glen Harvy at: 2/3/2013 4:25:28 PMIn Version 12.2.6 the GetLinkText method throws an error:
The property or indexer 'DevExpress.XtraEditors.ViewInfo.BaseControlViewInfo.StringInfo' cannot be used in this context because the get accessor is inaccessible
Added By: Glen Harvy at: 2/3/2013 4:46:26 PM12.2.6 After virtually copying and pasting this code I now get
System.NullReferenceException was unhandled
HResult=-2147467261
Message=Object reference not set to an instance of an object.
Source=MyCourts
StackTrace:
at MyCourts.LinkLabelControl.GetLinkText(MouseEventArgs e) in c:\Users\Administrator\Documents\My Projects\MyCourts\LinkLabelControl.cs:line 121
at MyCourts.LinkLabelControl.GetLink(MouseEventArgs e) in c:\Users\Administrator\Documents\My Projects\MyCourts\LinkLabelControl.cs:line 110
at MyCourts.LinkLabelControl.SetCursor(MouseEventArgs e) in c:\Users\Administrator\Documents\My Projects\MyCourts\LinkLabelControl.cs:line 102
at MyCourts.LinkLabelControl.OnMouseMove(MouseEventArgs e) in c:\Users\Administrator\Documents\My Projects\MyCourts\LinkLabelControl.cs:line 97
at System.Windows.Forms.Control.WmMouseMove(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at DevExpress.Utils.Controls.ControlBase.WndProc(Message& m)
at DevExpress.XtraEditors.BaseControl.WndProc(Message& msg)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Form.ShowDialog(IWin32Window owner)
at DevExpress.XtraSplashScreen.ThreadManagerBase.ThreadEntryPoint(Object skinName)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart(Object obj)
InnerException:
1) It handles cases where two StringBlocks have the exact same text or do not match the link text. In cases like <link=command1>Test <b>bold</b></link> the current code fails because no block has the text "Test <b>bold</b>"; there are actually two blocks for that link. The strategy I used is impossible to confuse.
2) It has color properties and redraws nicely for hover and pressed states.
3) It allows <link>Test</link> or <link=command string>Test</link> and provides a LinkClicked event with the index of the link and the command string, if specified. This is better for programmability (and safer!) than Process.Start.
(The command string can contain the '>' character if you escape it: <link=this >> works> gives "this > works".)
It is thirty lines longer and the code is better performance-wise as well. If this interests you, I may be able to submit it.Added By: Joseph N. Musser II at: 5/16/2013 9:22:13 AM
The comment system replaced my angle brackets with > etc. Ahem.
Added By: Matt Jacobi at: 9/25/2013 11:22:18 AMThis example breaks down if the hyperlink wraps (is broken across two lines). GetLink fails to match up with a known link.
Added By: Simon Hewitt at: 9/25/2014 3:34:13 AMDon't use Cursor.Current in SetCursor - it flickers like crazy,
Use "Cursor = " instead
Hi,
OK. Thank you for your note. We will update our example accordingly.
I would like to note that this feature is available out of the box starting from version 13.1. If you are using version 13.1 or higher, you do not need to use this example.