Quantcast
Channel: DevExpress Support Center (Examples)
Viewing all articles
Browse latest Browse all 7205

OBSOLETE: How to implement a hyperlink functionality in the LabelControl

$
0
0

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.

Added By: Oleg Khozyainov at: 7/18/2012 8:18:14 AM    

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.

Added By: Stefan Koell at: 1/22/2013 3:58:01 AM    

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 PM    

In 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 PM    

12.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:

Added By: Joseph N. Musser II at: 5/16/2013 9:20:24 AM    I implemented a more comprehensive version using this as a guide.

1) It handles cases where two StringBlocks have the exact same text or do not match the link text. In cases like &lt;link=command1&gt;Test &lt;b&gt;bold&lt;/b&gt;&lt;/link&gt; the current code fails because no block has the text &quot;Test &lt;b&gt;bold&lt;/b&gt;&quot;; 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 &lt;link&gt;Test&lt;/link&gt; or &lt;link=command string&gt;Test&lt;/link&gt; 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 &#39;&gt;&#39; character if you escape it: &lt;link=this &gt;&gt; works&gt; gives &quot;this &gt; works&quot;.)

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 &gt; etc. Ahem.

Added By: Matt Jacobi at: 9/25/2013 11:22:18 AM    

This example breaks down if the hyperlink wraps (is broken across two lines). GetLink fails to match up with a known link.


Viewing all articles
Browse latest Browse all 7205

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>