Click here to Skip to main content
15,888,454 members
Please Sign up or sign in to vote.
1.44/5 (2 votes)
See more:
I have a button with a background. I created mouseenter & mouseleave event. In the mouseleave event, if a mouse cursor is outside of 2 coordinates it will trigger the mouseleave event.

my problem is when i leave in 500X250 rectangle, it will trigger the mouseleave. i want it to trigger in 250X125 rectangle.

What I have tried:

C#
private void Button_SpanMouseEnter(object sender, EventArgs e)
    {
        Button x = sender as Button;
        x.Size = new Size(500, 250);
        x.Location = new Point(0, 0);
    }

    private void Button_SpanMouseLeave(object sender, EventArgs e)
    {
        Button x = sender as Button;

        //If cursor is outside of this coordinates(0,0) & (250,125)
        //it will trigger this size
        x.Size = new Size(250,125);
        x.Location = new Point(0, 0);
    }
Posted
Updated 12-May-16 10:57am
v2
Comments
CHill60 12-May-16 8:02am    
WinForm, WPF, ASP.NET, other .. which?
Alan N 12-May-16 10:31am    
You probably need to rethink your stragegy here. The MouseLeave event is fired when the mouse cursor moves outside the bounds of the control. Therefore a check to determine if the cursor is still within the bounds can never succeed.
Sergey Alexandrovich Kryukov 12-May-16 11:06am    
Of course. The question makes no practical sense. Besides, changing the size in the handler may recursively invoke the same even, which will lead to a total disaster.
—SA
Sergey Alexandrovich Kryukov 12-May-16 11:06am    
1) Why? 2) What do you mean by "Button"? which one? Full type name, please.
—SA

1 solution

yes. you can have button with background image .

Check for BackgroundImage property of a button.

I think the below solution suits your scenario.

Just put a panel of the size( 250X125 ) you want on the button. Set the backgroundImage property for that panel.

Now since the panel is on the button . You can use Mouseleave event of the panel to fulfill your requirement.

Also, as you mentioned in the question in case of two rectangles, you can use two panels.the outer panel of large size (500X250 rectangle), where as the inner panel (250X125 rectangle.).

Now write the event for Mouse leave of the inner panel. Make sure both panels have same background and inner panel has no borders, so that it appears a single panel for the user.
 
Share this answer
 
v5

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