Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

381
Views
How to show or hide a Panel using Mouse coordinates if the Form is resized?

I have a form that contains a Panel.
At first, this Panel invisible and becomes visible when the mouse enters its Client Area.
The code I use works as long as I do not resize the Form.
By changing the Form size, this code no longer works.

enter image description here

I tried this code:

Private Sub Form1_MouseMove(sender As Object, e As MouseEventArgs) Handles Me.MouseMove
    Label1.Text = e.Location.ToString
    If e.Location.Y > 441 Then
        Panel1.Visible = True
    Else
        Panel1.Visible = False
    End If
End Sub
over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Since it appears that this Panel is docked to the bottom of your Form, you can just consider the Panel's Bounds (the rectangle that describes the area a Control occupies inside its container) to determine whether the Mouse position is inside its Client Area.

The Rectangle.Contains(Point) method returns True or False if a Point is contained inside the bounds of the Rectangle specified.
You can then use the Boolean result to also set the visibility of the Panel:

Private Sub Form1_MouseMove(sender As Object, e As MouseEventArgs) Handles Me.MouseMove
    Panel1.Visible = Panel1.Bounds.Contains(e.Location)
End Sub

Extra:
How to handle other common cases:

  • Panel1 is not direct child of the Form, so the Form won't receive Mouse events when the Mouse pointer enters the bounds of the actual Parent of the Panel
  • The Form's Client Area is covered by other Controls, so it won't receive mouse events when the Mouse pointer hovers over these Controls

You cannot add event handlers to all Controls in the Form that may, at some point, be involved in these actions. Controls can be added or removed or moved around in the designer. Not feasible.

In these and other similar cases, implementing IMessageFilter comes in handy.
You can use the Application.AddMessageFilter / Application.RemoveMessageFilter methods to monitor messages directed to any Control before they are dispatched to the destination.
You can also prevent the messages from reaching the destination, if needed, returning True in the IMessageFilter.PreFilterMessage() method.

Here, the Form class implements IMessageFilter directly. You can of course create a reusable helper class to do the same.

Public Class SomeForm
    Implements IMessageFilter

    Private Const WM_MOUSEMOVE As Integer = &H200
    Public Sub New()
        InitializeComponent()
        Application.AddMessageFilter(Me)
    End Sub

    Protected Overrides Sub OnFormClosed(e As FormClosedEventArgs)
        Application.RemoveMessageFilter(Me)
        MyBase.OnFormClosed(e)
    End Sub

    Public Function PreFilterMessage(ByRef m As Message) As Boolean Implements IMessageFilter.PreFilterMessage
        If m.Msg = WM_MOUSEMOVE Then
            Panel1.Visible = Panel1.RectangleToScreen(Panel1.ClientRectangle).Contains(MousePosition)
        End If
        Return False
    End Function
End Class
over 4 years ago · Santiago Trujillo Report

0

Try this:

Private Sub Form1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove
    Label1.Text = e.Location.ToString

    If e.X > Panel1.Left And e.X < Panel1.Left + Panel1.Width And e.Y > Panel1.Top And e.Y < Panel1.Top + Panel1.Height Then
        Panel1.Visible = True
    Else
        Panel1.Visible = False
    End If
End Sub
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!