滚动面板自动隐藏组件内部

时间:2014-03-14 16:42:40

标签: vb.net winforms

我的代码有效,但问题是我在其中放了一个列表框。我想确保即使用户将鼠标悬停在列表框上,该窗格也会关闭。

我有两个问题:

  1. 主要问题:面板的计时器无法正常工作,因为列表框组件有点阻止它。我不确定如何抵消这种情况。

  2. 我想隐藏或折叠列表框以获得视觉吸引力。

  3. 这是我的代码:

    Public Class Form1
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Panel1.Dock = DockStyle.Left
        End Sub
    
    
        Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
            If Panel1.Width < 150 Then
                Panel1.Width = Panel1.Width + 100
            ElseIf Panel1.Width = 150 Then
                Timer1.Enabled = False
            End If
        End Sub
    
        Private Sub Timer2_Tick(sender As Object, e As EventArgs) Handles Timer2.Tick
            If Panel1.Width >= 100 Then
                Panel1.Width = Panel1.Width - 50
                If Panel1.Width < 100 And Panel1.Width > 25 Then
                    Panel1.Width = Panel1.Width - 15
                End If
    
            ElseIf Panel1.Width <= 25 Then
                Timer2.Enabled = False
            End If
        End Sub
    
        Private Sub Panel1_MouseEnter(sender As Object, e As EventArgs) Handles Panel1.MouseEnter
            Timer1.Enabled = True
            Timer2.Enabled = False
        End Sub
    
    
        Private Sub Panel1_MouseLeave(sender As Object, e As EventArgs) Handles Panel1.MouseLeave
            Timer1.Enabled = False
            Timer2.Enabled = True
        End Sub
    
    End Class
    

1 个答案:

答案 0 :(得分:1)

ListBox将导致面板的MouseEnter和MouseLeave事件触发。

请注释掉那些事件:

'Private Sub Panel1_MouseEnter(sender As Object, e As EventArgs) Handles Panel1.MouseEnter
'    Timer1.Enabled = True
'    Timer2.Enabled = False
'End Sub

'Private Sub Panel1_MouseLeave(sender As Object, e As EventArgs) Handles Panel1.MouseLeave
'    Timer1.Enabled = False
'    Timer2.Enabled = True
'End Sub

然后尝试添加第三个计时器以在面板上跟踪鼠标:

Private Sub Timer3_Tick(sender As Object, e As EventArgs) Handles Timer3.Tick
  If Panel1.ClientRectangle.Contains(Panel1.PointToClient(MousePosition)) Then
    If Not Timer1.Enabled AndAlso Panel1.Width < 150 Then
      Timer1.Enabled = True
      Timer2.Enabled = False
    End If
  Else
    If Not Timer2.Enabled AndAlso Panel1.Width > 25 Then
      Timer1.Enabled = False
      Timer2.Enabled = True
    End If
  End If
End Sub