.NET Forum / Windows Forms / WinForm Controls / December 2006
MouseLeave e MouseEnter
|
|
Thread rating:  |
Magallo - 18 Dec 2006 11:10 GMT Hi all, I've got a problem using the MouseLeave/MouseEnter events. What I would like to do is something like that: I have a panel and a button inside it.. Using the MouseEnter/MouseLeave events on the panel, I dinamically show/hide the button so that when I enter the panel, the button is shown, and when I leave the panel the button is hidden.
It works correctly. The problem occurs when I pass the mouse over the button. Also in this case the panel MouseLeave event occurs, so that the button disappears. So the effect is that: I enter the panel, the button is shown, I pass the mouse over it to click it but it disappears.
What I should do is changing the behaviour of the MouseLeave events implementation so that I hide the button only if the mouse is effectively outside the panel and not when it is over a control that is a child of the panel.
Is there a way to do this?
I hope I have been clear.
Thanks.
Magallo
Torsten Kerz - 18 Dec 2006 13:37 GMT > I've got a problem using the MouseLeave/MouseEnter events. What I would > like > to do is something like that: I have a panel and a button inside it.. > Using the MouseEnter/MouseLeave events on the panel, I dinamically > show/hide the button so that when I enter the panel, the button is shown, > and when I leave the panel the button is hidden. The behavior of the events is right, because only the control under the cursor counts.
I would ...: Take also mouseenter and leave of the button,
start a shorttime-timer in both leave-events, stop this timer in the enter-events and hide the frame in the timer-event.
that's it.
Greetings, Torsten.
Oliver Sturm - 18 Dec 2006 14:42 GMT Hello Torsten,
>I would ...: >Take also mouseenter and leave of the button, > >start a shorttime-timer in both leave-events, >stop this timer in the enter-events and >hide the frame in the timer-event. Interesting idea, but it sounds rather complicated (for example, assuming the UI may be a little more complex than that described in the OP's post). I guess it would be an awful lot easier to check whether the mouse cursor is really outside the bounds of the panel before actually making the button invisible. Something like
if (!myPanel.ClientRectangle.Contains(MousePosition)) // hide button
Oliver Sturm
 Signature http://www.sturmnet.org/blog
Torsten Kerz - 18 Dec 2006 18:00 GMT Hi Oliver.
> Interesting idea, but it sounds rather complicated (for example, assuming > the UI may be a little more complex than that described in the OP's post). You're right. :)
> I guess it would be an awful lot easier to check whether the mouse cursor > is really outside the bounds of the panel before actually making the > button invisible. This doesn't give a garantie, that the inner controls will be hidden. Mouse enter and leave of the panel can fail, if the pointer is moved to fast or if an other application (for example: mouse driver and "automatic pointer to dialog-movement") moves the mousepointer.
On the other side it wouldn't be to much work, to set the handlers of subcontrols automaticly. Something like this:
Public Class Form1 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Timer1.Enabled = False Timer1.Interval = 50
For Each control As Control In Panel1.Controls AddHandler control.MouseEnter, AddressOf Panel1_MouseEnter AddHandler control.MouseLeave, AddressOf Panel1_MouseLeave control.Visible = False Next End Sub
Private Sub Panel1_MouseEnter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Panel1.MouseEnter Timer1.Stop() For Each control As Control In Panel1.Controls control.Visible = True Next End Sub
Private Sub Panel1_MouseLeave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Panel1.MouseLeave Timer1.Start() End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick Timer1.Stop() For Each control As Control In Panel1.Controls control.Visible = False Next End Sub End Class
Greetings, Torsten.
Oliver Sturm - 18 Dec 2006 18:10 GMT Hello Torsten,
>>I guess it would be an awful lot easier to check whether the mouse cursor >>is really outside the bounds of the panel before actually making the [quoted text clipped - 7 lines] >moves the >mousepointer. Yeah - my suggestion doesn't have to do with which events to listen for. But the decision of whether or not the "correct" state (i.e. the one where the button must be hidden/shown) has been reached, can easily be made by looking at the position of the mouse again. Given the complexity of the sequence of leave/enter events that may come up, it seems much easier to me to make that check before doing anything to the button, than it would be to try and track the events themselves reliably.
Oliver Sturm
 Signature http://www.sturmnet.org/blog
Torsten Kerz - 18 Dec 2006 19:45 GMT Hmm ...
> Yeah - my suggestion doesn't have to do with which events to listen for. > But the decision of whether or not the "correct" state (i.e. the one where > the button must be hidden/shown) has been reached, can easily be made by > looking at the position of the mouse again. ok - its possible, to check the mouse position and get the correct state with it. But i miss one thing: which event can do this check?
but - combine the idea of "check the mouseposition" with the idea of track down the subcontrols and add handlers for mouseleave and it will be possible, to discard the timer and the additional handling of mouseenter.
---
just one very simple, additional idea, which will work, if there are only buttons and other clickable controls inside the frame and if every click to one of this controls should hide the panel:
panel1.Capture = True 'in mouseenter-event of the panel
Oliver Sturm - 18 Dec 2006 21:16 GMT Hello Torsten,
>ok - its possible, to check the mouse position and get the correct state >with it. >But i miss one thing: which event can do this check? I'm not sure I understand your question... the OP asked a question about how to use the various events reliably for his purposes. As far as I can see, this question has long since been answered - basically it has to be some combination of OnMouseEnter and OnMouseLeave, possibly hooking into child elements as well.
The important thing is that my suggestion never targeted this part of the problem. I would even go so far to say I haven't thought about it at all - otherwise I would have suggested also hooking into the window and possibly Application (de-)activation events to make sure you catch the cases where the user just alt-tabs out of the app.
Here's what my suggestion deals with: Regardless of which events the OP ends up using to track the mouse cursor entering/leaving various parts of his UI, that tracking is bound to be complex, and there can be situations where the state can be difficult to track. (Imagine: A MouseEnter from the containing panel, a MouseLeave from moving into a contained element, a MouseEnter from entering that contained element - if you even bothered hooking into that -, then an Application.Deactivate (or whatever it's called) because the user hits alt-tab, ...) This was why I suggested to complement whatever combination of event handlers with a rather simple status check, to find out whether the mouse cursor is in the desired region or not. My idea is that this can happen at any time, in any desired event handler, thereby eliminating the need for elaborate state handling.
>but - combine the idea of "check the mouseposition" with the idea of track >down the >subcontrols and add handlers for mouseleave and it will be possible, to >discard the >timer and the additional handling of mouseenter. Yes, I believe that's what I was saying.
Oliver Sturm
 Signature http://www.sturmnet.org/blog
Bob Powell [MVP] - 18 Dec 2006 15:12 GMT You need to create a control based on Panel and then override the mouseleave protected method to see if the mouse is in one of the child controls of the panel. if not, fire the mouseleave event, of not, dont.
 Signature Bob Powell [MVP] Visual C#, System.Drawing
Ramuseco Limited .NET consulting http://www.ramuseco.com
Find great Windows Forms articles in Windows Forms Tips and Tricks http://www.bobpowell.net/tipstricks.htm
Answer those GDI+ questions with the GDI+ FAQ http://www.bobpowell.net/faqmain.htm
All new articles provide code in C# and VB.NET. Subscribe to the RSS feeds provided and never miss a new article.
> Hi all, > I've got a problem using the MouseLeave/MouseEnter events. What I would [quoted text clipped - 20 lines] > > Magallo Bob Powell [MVP] - 18 Dec 2006 15:17 GMT You may also need to override OnMouseEnter to see if the mouse has entered the panel from one of it's child controls and filter raising the event in that case.
 Signature Bob Powell [MVP] Visual C#, System.Drawing
Ramuseco Limited .NET consulting http://www.ramuseco.com
Find great Windows Forms articles in Windows Forms Tips and Tricks http://www.bobpowell.net/tipstricks.htm
Answer those GDI+ questions with the GDI+ FAQ http://www.bobpowell.net/faqmain.htm
All new articles provide code in C# and VB.NET. Subscribe to the RSS feeds provided and never miss a new article.
> Hi all, > I've got a problem using the MouseLeave/MouseEnter events. What I would [quoted text clipped - 20 lines] > > Magallo ThunderMusic - 19 Dec 2006 00:44 GMT I did mine just like this... it's very simple and it works as expected :
void Form_MouseLeave(object sender, System.EventArgs e) { if (this.ClientRectangle.Contains(PointToClient(Control.MousePosition))) return;
if (m_HoverOpacity != m_NormalOpacity) this.Opacity = m_NormalOpacity;
m_isHovering = false; } void Form_MouseEnter(object sender, System.EventArgs e) { if (!m_isHovering) { m_isHovering = true; } }
I hope it helps
ThunderMusic
> Hi all, > I've got a problem using the MouseLeave/MouseEnter events. What I would [quoted text clipped - 20 lines] > > Magallo
Free MagazinesGet these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...
|
|
|