Click here to Skip to main content
15,917,455 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Forum

I want to change the cursor style depending on mouse position in run time.
If the mouse is on borders of the text-box, then cursor style should be either SizeNS or SizeWE. If the mouse is on other part of the text-box, cursor style should be SizeAll.
Problem I am facing is, I am not able to find whether the current position of mouse is on borders or remaining portion of the text-box.

I will be highly thankful if anyone can guide a bit to solve this issue

Thanking in anticipation

NamitaS!!!
Posted

1 solution

Use the MouseMove event:
C#
private void button1_MouseMove(object sender, MouseEventArgs e)
    {
    Button b = sender as Button;
    if (b != null)
        {
        if (((e.X <= b.Margin.Left) || (e.X >= b.Width - b.Margin.Right)) ||
            ((e.Y <= b.Margin.Top) || (e.Y >= b.Height - b.Margin.Bottom)))
            {
            Cursor = Cursors.SizeAll;
            }
        else
            {
            Cursor = Cursors.Default;
            }
        }
    }

private void button1_MouseLeave(object sender, EventArgs e)
    {
    Cursor = Cursors.Default;
    }

The MouseLeave event is just there to ensure the cursor is restored to normal if it is moved quickly.
This shows a button and uses the margins, but it works for all controls, and you can specify your own edge limits.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900