Search code examples
c#positioncursor

If Cursor is in Location


if (Cursor.Position == closeButton.Location)
{
    closeButton.BackColor = Color.FromArgb(255, 231, 76, 60);
}

This if statement doesn't work for some reason, any help?

I would like it to check if the Cursor position is in the Location set.


Solution

  • You need to check for ClientRectangle property of your button. So this is the proper syntax to use:

    if (closeButton.ClientRectangle.Contains(closeButton.PointToClient(Cursor.Position)))
    {
        closeButton.BackColor = Color.FromArgb(255, 231, 76, 60);
    }