[.NET] Form Mouse Enter/Exit

Soldato
Joined
12 Jun 2005
Posts
5,361
Hi there,

I want to create something so that if my mouse enters anywhere that is within the forms boundaries even if its onto an object which is on the form, and then something for when it leaves the boundaries.

Is this possible?

Thanks.
 
onMouseEnter Is the events name, just create a handler for it in your form


select your form in the designer, select the tab page in the prperties dialogu with the little lightening bolt on it and scroll down a bit, you'll se the moseOver event there, double clicky and add your code.

It'll put on of these in the designer generated code (Form1.Designer.cs):

this.MouseEnter += new System.EventHandler(this.Form1_MouseEnter);

and a method in your code (Form1.cs) for you to write your event handler in

private void Form1_MouseEnter(object sender, EventArgs e)
{
//your code goes here
}

HT
 
I have fixed this, and just to say the code above is incorrect. Because if i go onto an object, the event doesn't trigger, it has to go onto the form for that to trigger.
 
the code above will work for any component, a form, a menu, a button whatever.

OnMouseLeave will be fired when the mouse pointer leaves the component / form

the phrase you were looking for was thanks for your help but that wasn't quite the code I needed...
 
happytechie said:
the phrase you were looking for was thanks for your help but that wasn't quite the code I needed...

Chillax mate, thats what i meant, doesn't do what i stated in the opening thread, I wasn't taking a digg.
 
If you want it to work like I think you do you cannot do it with mouse enter exit etc... if you have lots of controls you will have enter and exit handlers firing all the time. You will need to hook the window using a API for it to work with the entire form + controls etc...

Can I ask why you need it to work like this? Only time I have needed something like this is when I was making a menu that minimized when you left the form.

TrUz
 
Hi there,

Well the solution i used was something like this:

Code:
'A rectangle that covers the whole form
Dim formRectangle As New Rectangle(0, 0, 190, 70)

'A Timer to check if the mouse is on the form
    Private Sub mouseOnForm(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles onForm.Tick

'If the rectangle contains the mouse coordinates of where the mouse is
        If formRectangle.Contains(Me.PointToClient(System.Windows.Forms.Cursor.Position)) Then
            'If it does then do this
            rectangleMove.Location = New Drawing.Point(173, 2)
            Me.Opacity = 0.8
        Else
            'If it doesn't then do this.
            rectangleMove.Location = New Drawing.Point(-20, -20)
        End If
        Me.Refresh()

    End Sub

And you just have the timer running when you need to check if the mouse is on the form.

The reason I needed this, and if you refer to my program in the "Little MP3 Player" thread. When the mouse is anywhere on the form a rectangle appears (rectangleMove). When you click and drag on that rectangle it moves the form where you drag the form.

Hope that helps.
 
Back
Top Bottom