Setting LinkLabel cursor?

D

David Veeneman

I'm creating a UserControl that uses a LinkLabel. For reasons that I won't
bore everyone with, I don't want the LinkLable to show the default hand
cursor when the mouse enters the control. Should be a simple setting on the
LinkLabel.Cursor property, right? Not so simple.

I set the property, but still got the default 'hand' cursor. To debug, I
added the following event handler to my LinkLabel control:

private void linkLabel1_MouseEnter(object sender, EventArgs e)
{
linkLabel1.Cursor = Cursors.Arrow;
linkLabel1.Text = linkLabel1.Cursor.ToString();
}

When I run the app and enter the LinkLabel, it shows that its Cursor is
still set to Cursors.Default (the hand)! The Cursor property is not getting
set.

Any thoughts as to what's going on here? Thanks in advance

David Veeneman
Foresight Systems
 
D

DeveloperX

I'm creating a UserControl that uses a LinkLabel. For reasons that I won't
bore everyone with, I don't want the LinkLable to show the default hand
cursor when the mouse enters the control. Should be a simple setting on the
LinkLabel.Cursor property, right? Not so simple.

I set the property, but still got the default 'hand' cursor. To debug, I
added the following event handler to my LinkLabel control:

private void linkLabel1_MouseEnter(object sender, EventArgs e)
{
linkLabel1.Cursor = Cursors.Arrow;
linkLabel1.Text = linkLabel1.Cursor.ToString();

}

When I run the app and enter the LinkLabel, it shows that its Cursor is
still set to Cursors.Default (the hand)! The Cursor property is not getting
set.

Any thoughts as to what's going on here? Thanks in advance

David Veeneman
Foresight Systems

Yep, if you disassemble the Linklabel, you get the code at the end of
this reply, but there's hope :)

This works:

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace WindowsApplication8
{
public class Class1 : LinkLabel
{
Cursor _newCursor = Cursors.Arrow;
public Class1()
{

}
public Cursor NewOverrideCursor
{
set
{
_newCursor = value;
this.OverrideCursor = value;
}
get
{
return this.OverrideCursor;
}
}


protected override void OnMouseMove(MouseEventArgs e)
{

base.OnMouseMove (e);
this.OverrideCursor = _newCursor;
}
}
}









This is what Reflector told me the linklabel does on a mouse move
event.


protected override void OnMouseMove(MouseEventArgs e)
{
base.OnMouseMove(e);
if (base.Enabled)
{
LinkLabel.Link link1 = null;
foreach (LinkLabel.Link link2 in this.links)
{
if ((link2.State & LinkState.Hover) ==
LinkState.Hover)
{
link1 = link2;
break;
}
}
LinkLabel.Link link3 = this.PointInLink(e.X, e.Y);
if (link3 != link1)
{
if (link1 != null)
{
link1.State &= ~LinkState.Hover;
}
if (link3 != null)
{
link3.State |= LinkState.Hover;
if (link3.Enabled)
{
this.OverrideCursor = Cursors.Hand;
}
}
else
{
this.OverrideCursor = null;
}
if (this.hoverLinkFont != this.linkFont)
{
if (link1 != null)
{
this.InvalidateLink(link1);
}
if (link3 != null)
{
this.InvalidateLink(link3);
}
}
}
}
}
 
P

PokerMan

argh DevX beat me too it ;) but you could simplify it one step less. This
is essentially what dev did except i have assumed you will always want your
link to have an arrow so no need for the property to override the cursor.
Then you can just drop this one all over your forms and never need to touch
its properties for the cursor.

Great spot tho, i never realised that the cursor property of linklabel seems
to do f**k all.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace littleApp
{
public partial class customLink : LinkLabel
{
public customLink()
{
InitializeComponent();
this.OverrideCursor = Cursors.Arrow;
}

protected override void OnMouseMove(MouseEventArgs e)
{
base.OnMouseMove(e);
this.OverrideCursor = Cursors.Arrow;
}

}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top