Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

383
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda