Home > Net >  How to change cursor on disabled buttons in C#
How to change cursor on disabled buttons in C#

Time:04-01

I just started working on C# with Visual Studio & Windows Forms Applications. I was trying to create a Calculator and I was wondering if I could change the cursor type on a button which is disabled, I can't figure out how to do it, please help me thank you!

Edit: here's the code I tried to do, it only works if the button's enabled...

private void txt_current_operation_MouseHover(object sender,EventArgs e) {

            txt_current_operation.Cursor=Cursors.Hand;
        }

CodePudding user response:

Hack Answer

Assuming the Button is contained by the Form, you can handle the MouseMove event of the Form and change the cursor from there:

private void Form1_MouseMove(object sender, MouseEventArgs e)
{
    Rectangle rc = txt_current_operation.RectangleToScreen(txt_current_operation.ClientRectangle);
    this.Cursor = rc.Contains(Cursor.Position) ? Cursors.Hand : Cursors.Default;
}

If the Button was contained by a Panel, or some other container besides the Form, then you'd change to the MouseMove event of that container instead.

Demonstration:

enter image description here

  • Related