1

Is there a way to determine if the mouse is within the LinkArea of a LinkLabel control in C#?

Any help would be greatly appreciated.

William Troup
  • 12,739
  • 21
  • 70
  • 98

3 Answers3

1

You can use Mouse Enter Event

linkLabel1.MouseEnter += new EventHandler(linkLabel1_MouseEnter);

private void linkLabel1_MouseEnter(object sender, EventArgs e)
{
    MessageBox.Show("Mouse is within link area");
}
  • can we use mouse hover also? – Sudhakar Tillapudi Dec 03 '13 at 18:24
  • @SudhakarTillapudi As much as I know. It will have a different effect from mouse enter. In mouse hover. The mouse has to be over the control for some seconds but mouse enter is immediately –  Dec 03 '13 at 18:31
  • This is not correct. This will tell you if the mouse is within the control boundaries, but not whether it is actually over the link itself. An example is if you have a `LinkLabel` control docked to the top or bottom of a large container, and align it to the left or right so that the control covers the entire container width but the link within does not. If you move your mouse cursor into the blank area of the control besides the text, the `MouseEnter` event will still fire. – Knowledge Cube Apr 19 '17 at 18:03
0

This cannot be done. The MouseEnter event suggested in the other answer at the time of this writing will fire any time the mouse enters the control area, regardless of whether it is actually over link text or not.

If your LinkLabel control's boundaries are small relative to its content, then the MouseEnter event may work well enough. But in the case where (for example) you want your link to change color when the mouse hovers over the link text and there is a lot of area within the control around the text, then this approach will not work.

On a somewhat unrelated note, this also prevents you from detecting which link within the LinkLabel is currently being hovered over if you have multiple, as mentioned here.

One more detail in case anybody is wondering: the LinkLabel.LinkArea property is not what you are looking for, either. That only determines which characters within the LinkLabel are actually part of a link, and not the actual area they occupy onscreen.

To wrap up, the only way you may be able to get the functionality you are looking for is to implement your own custom control which behaves similarly to the Label or LinkLabel control, but adds the methods and/or properties that you need.

Community
  • 1
  • 1
Knowledge Cube
  • 990
  • 12
  • 35
0

Since the original LinkLabel has a protected function PointInLink this is not hard to do:

using System;
using System.Windows.Forms;

namespace MyControlNameSpace
{
    /// <summary>
    /// Data for a HoveredLinkChanged-Handler.
    /// </summary>
    public class HoveredLinkChangedEventArgs : EventArgs
    {
        private readonly LinkLabel.Link m_Link;

        /// <summary>
        /// Creates data for a HoveredLinkChanged-Handler
        /// </summary>
        /// <param name="link">the Link, with the mouse pointer over it</param>
        public HoveredLinkChangedEventArgs(LinkLabel.Link link)
        {
            m_Link = link;
        }

        /// <summary>
        /// Returns the hovered Link
        /// </summary>
        public LinkLabel.Link HoveredLink
        {
            get { return m_Link; }
        }
    }

    /// <summary>
    /// The structure of a HoveredLinkChanged-Handler
    /// </summary>
    public delegate void HoveredLinkChangedEventHandler(
              object sender, HoveredLinkChangedEventArgs e);

    /// <summary>
    /// Adds to LinkLabel the possiblity to react on changes
    /// of the hovered Link (e.g. to alter a TooltipText).
    /// </summary>
    public class LinkLabelEx : LinkLabel
    {
        private Link m_HoveredLink;

        /// <summary>
        /// Occurs, when another Link is hovered.
        /// </summary>
        public event HoveredLinkChangedEventHandler HoveredLinkChanged;

        /// <summary>
        /// Raises the HoveredLinkChanged event
        /// </summary>
        /// <param name="hoveredLink">the hovered Link</param>
        protected virtual void OnHoveredLinkChanged(Link hoveredLink)
        {
            if (HoveredLinkChanged != null)
                HoveredLinkChanged(this,
                    new HoveredLinkChangedEventArgs(hoveredLink));
        }

        /// <summary>
        /// Raises the Control.OnMouseMove(MouseEventArgs) event.
        /// </summary>
        /// <param name="e">a MouseEventArgs containing the event data</param>
        protected override void OnMouseMove(MouseEventArgs e)
        {
            base.OnMouseMove(e);
            Link currentLink = PointInLink(e.X, e.Y);
            if (Equals(currentLink, m_HoveredLink)) return;
            m_HoveredLink = currentLink;
            OnHoveredLinkChanged(m_HoveredLink);
        }
    }
}

Then there are only two things left to do:

Add an eventhandler to your LinkLabelEx, e.g.:

linkLabelEx1.HoveredLinkChanged += linkLabelEx1_HoveredLinkChanged;

Set the tooltiptext according to the link given in the eventArgs, e.g.:

void linkLabelEx1_HoveredLinkChanged(object sender, HoveredLinkChangedEventArgs e)
{
    string ttt = e.Link == null
        ? string.Empty
        : e.Link.Description;
    toolTip1.SetToolTip((Control)sender, ttt);
}