Search code examples
c#winformscursorlinklabel

C# - Fix linklabel hand-cursor


I have two link labels in my windows forms program which links to my website. I got rid of the underlines and the ugly blue colour and tried to fix them up a little bit. But the biggest problem still remains and It's just so disturbing for me, I don't know why.

The hand cursor when you hover over them is that old Windows 98 hand/link cursor. Is there any way to change it to the system cursor? I've checked some other links about this problem, but I couldn't get it to work so I decided to ask here.

Here's my code to get rid of the underline btw: linkLabel1.LinkBehavior = System.Windows.Forms.LinkBehavior.NeverUnderline;


Solution

  • Unfortunately the LinkLabel class is hard-coded to use Cursors.Hand as the hover cursor.

    However, you can work around it by adding a class like this to your project:

    public class MyLinkLabel : LinkLabel
    {
        protected override void OnMouseEnter(EventArgs e)
        {
            base.OnMouseEnter(e);
            OverrideCursor = Cursors.Cross;
        }
    
        protected override void OnMouseLeave(EventArgs e)
        {
            base.OnMouseLeave(e);
            OverrideCursor = null;
        }
    
        protected override void OnMouseMove(MouseEventArgs e)
        {
            base.OnMouseMove(e);
            OverrideCursor = Cursors.Cross;
        }
    }
    

    and using that instead of LinkLabel on your form. (This sets the cursor to a cross for testing purposes, but you can change it to whatever you want.)

    I should say that the real LinkLabel code has much more complex logic to do with changing the cursor according to whether or not the link is enabled, but you might not care about that.