Windows Develop Bookmark and Share   
 index > Windows Forms General > Mouse events entering and leaving forms
 

Mouse events entering and leaving forms

I'm developing a Word 2007 add-in using Visual Studio 2008 on a Vista Computer. The add in will require frequent interation between a form (topmost) and the active Word document. The issue is when the user clicks on the form the active document loses focus so that when they return to the document they need to click the document twiceto retrieve data from the document to the form,once for the document to gain focus and the second time to select the data. The user clicks a button to add the data to a text box on the form. This does not flow very well. I would like to switch between the form and the Word document so that when the mouse enters the form, the form gains focus and when the mouse leaves the form, the document gains focus. This would allow the form to work similar to function dialog boxes in excel.I tried using mouseenter and mouseleave events to track the location of the mouse for this purpose. Unfortunately its not that easy. The form has several controls such as buttons and a text box. The mouse enter event fires when the mouse first enters the form, but the mouseleave event fires when the mouse enters one of the controls on the form. This is not what I want. I need the mouse events to fire based on entering and leaving the form only. I took a stab at Win32 API but the language is foreign to me and I think Microsoft is switching from API to WPF anyway. Is it possible to do what I want or is that just the way it is?

Thanks in advance

Brad

Brad Smith, CPA  Wednesday, October 29, 2008 3:00 PM
You're floundering pretty badly. Why do you think you need to use a WPF timer class? System.Windows.Forms.Timer is available in any project that you started by using the Windows Forms Application project template. Just drop it from the toolbox onto your form. Set Enabled to True and Interval to 1000 and double-click it to add the Tick event handler.
nobugz  Wednesday, October 29, 2008 10:12 PM
Just use a timer:

private void timer1_Tick(object sender, EventArgs e) {
Point pos = Control.MousePosition;
bool onForm = pos.X >= this.Left && pos.Y >= this.Top &&
pos.X < this.Right && pos.Y < this.Bottom;
// Do something
//...
}

nobugz  Wednesday, October 29, 2008 3:15 PM

nobugz,

Thanks for your response. I tried to translate what you suggested to VB. I'm having the same problem. When the mouse goes over one of the other controls in the form, the mouseleave event fires when I only want the mouse leave to fire when I actually exit the form.

Here's my latest code:

Code Snippet

Private Sub timer2_Tick(ByVal sender As Object, ByVal e As EventArgs) Handles Me.MouseLeave
Dim pos = MousePosition
Try
If pos.X >= Left And pos.Y >= Top And pos.X < Right And pos.Y < Bottom Then
myApp.Activate()
End If
Catch
End Try
End Sub

Brad
Brad Smith, CPA  Wednesday, October 29, 2008 5:31 PM
I don't get it, why do you need a MouseLeave event? The Tick event can tell you when the mouse has entered or left the form. Add a boolean:

Dim mMouseOnForm As Boolean

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Dim pos As Point = Control.MousePosition
Dim onForm As Boolean = pos.X >= Left AndAlso pos.Y >= Top AndAlso pos.X < Right AndAlso pos.Y < Bottom
If onForm <> mMouseOnForm Then
mMouseOnForm = onForm
If onForm Then
'--- Do something when the mouse hovers over the form
Else
'--- Do something when the mouse is no longer hovering over the form
End If
End If
End Sub

nobugz  Wednesday, October 29, 2008 6:13 PM

nobugz,

I agree the tick event sounds like the best course of action. I didn't try it before because I'm not familiar with it.

I tried using your code but it needs modification to work here's the new code:

Code Snippet

Imports System.Windows.Threading

Public Class Form1

Public Event Tick As EventHandler

Public myinstance As DispatcherTimer

Public myhandler As EventHandler

Private Sub dispatcherTimer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Me.Tick

AddHandler myinstance.Tick, myhandler

Dim mMouseOnForm As Boolean

Dim pos As Point = MousePosition

Dim onForm As Boolean = pos.X >= Left AndAlso pos.Y >= Top AndAlso pos.X < Right AndAlso pos.Y < Bottom

Try

If onForm <> mMouseOnForm Then

mMouseOnForm = onForm

If onForm Then

MsgBox("On the Form")

Else

MsgBox("Off the Form")

End If

End If

Catch

End Try

End Sub

End Class

The tick event is not firing. I don't know what I'm doing wrong. Please Help!

Brad

Brad Smith, CPA  Wednesday, October 29, 2008 8:27 PM
You'll need to post to the WPF forum at social.msdn.microsoft.com, your code is not related to Windows Forms, the topic of this forum.
nobugz  Wednesday, October 29, 2008 8:55 PM
OK I don't know what I'm doing! I don't want WPF and I am not using WPF. I'm using VB andI was trying to make the code you provided work. How can I get regular windows forms recognize the tick event?

Brad Smith, CPA  Wednesday, October 29, 2008 9:19 PM
You're floundering pretty badly. Why do you think you need to use a WPF timer class? System.Windows.Forms.Timer is available in any project that you started by using the Windows Forms Application project template. Just drop it from the toolbox onto your form. Set Enabled to True and Interval to 1000 and double-click it to add the Tick event handler.
nobugz  Wednesday, October 29, 2008 10:12 PM
The tip on dragging the timer to the form did the trick. It's working perfectly now. Thank You!

Brad Smith, CPA  Thursday, October 30, 2008 3:37 PM

You can use google to search for other answers

Custom Search

More Threads

• Formatting cells in a datagridview that hosts a custom control
• Fill WebBrowser
• Retrieving a value from a CheckedListBox
• ADO.Net Errors
• Three level MenuStrip merge
• How To work with listBox
• Can Main Report and SubReport get data from different tables?
• Avoiding CPU 100% usage
• Form.hide()
• WebBrowser's Navigate method crashes