Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vb.net 如何滚动面板中的项目_Vb.net_Scroll_Panel - Fatal编程技术网

Vb.net 如何滚动面板中的项目

Vb.net 如何滚动面板中的项目,vb.net,scroll,panel,Vb.net,Scroll,Panel,我正在vb.net中创建一个winforms应用程序,我有一个面板,其中有一些标签。标签经过面板底部,因此面板会自动显示一个垂直滚动条(这就是我想要的)。但是,每当我使用鼠标滚轮向下滚动面板时,当其中一个标签在鼠标下向上滚动时,面板就会停止滚动。就像焦点从面板变为标签,标签不需要滚动。我只想使用鼠标滚轮滚动整个面板,不管鼠标下有什么。创建新的面板用户控件 Public Class PanelX Inherits Panel Public Sub New() Me

我正在vb.net中创建一个winforms应用程序,我有一个面板,其中有一些标签。标签经过面板底部,因此面板会自动显示一个垂直滚动条(这就是我想要的)。但是,每当我使用鼠标滚轮向下滚动面板时,当其中一个标签在鼠标下向上滚动时,面板就会停止滚动。就像焦点从面板变为标签,标签不需要滚动。我只想使用鼠标滚轮滚动整个面板,不管鼠标下有什么。

创建新的面板用户控件

  Public Class PanelX
   Inherits Panel

    Public Sub New()
       Me.AutoScroll = True
    End Sub

   Protected Overrides Sub OnMouseEnter(e As System.EventArgs)
      Me.Select()
      MyBase.OnMouseEnter(e)
   End Sub
  End Class

创建新的面板用户控件

  Public Class PanelX
   Inherits Panel

    Public Sub New()
       Me.AutoScroll = True
    End Sub

   Protected Overrides Sub OnMouseEnter(e As System.EventArgs)
      Me.Select()
      MyBase.OnMouseEnter(e)
   End Sub
  End Class

试过这个对我来说很好用

    Public Sub New()
    ' This call is required by the designer.
    InitializeComponent()
    ' Add any initialization after the InitializeComponent() call.
    AddHandler MouseWheel, AddressOf Panel1_MouseWheel
   End Sub



Private Sub Panel1_MouseWheel(sender As Object, e As MouseEventArgs) Handles Panel1.MouseWheel
    Panel1.Focus()
End Sub

试过这个对我来说很好用

    Public Sub New()
    ' This call is required by the designer.
    InitializeComponent()
    ' Add any initialization after the InitializeComponent() call.
    AddHandler MouseWheel, AddressOf Panel1_MouseWheel
   End Sub



Private Sub Panel1_MouseWheel(sender As Object, e As MouseEventArgs) Handles Panel1.MouseWheel
    Panel1.Focus()
End Sub

面板控件不能有焦点,并且不可选择。它只是一个容器。

您可以创建从面板派生的自定义控件,并使其能够接收焦点。
在这种情况下,这会有很大帮助,因为这样可以滚动自定义面板而无需任何其他逻辑。
即使另一个通常可以获得焦点的控件(如文本框)挡住了去路。

此实现修改面板控件(
ControlStyles.Selective
),使其能够接受焦点(
TabStop
属性也设置为True)。

OnMouseDown
也是隐藏的,因此,如果面板中的控件窃取了焦点,您只需单击面板将焦点移动到面板上,然后滚动即可。

Class PanelWithFocus
    Inherits System.Windows.Forms.Panel

    Public Sub New()
        Me.SetStyle(ControlStyles.Selectable, True)
        InitializeComponent()
    End Sub

    Protected Overrides Sub OnEnter(e As EventArgs)
        MyBase.OnEnter(e)
        Me.Focus()
    End Sub

    Protected Overrides Sub OnMouseDown(e As MouseEventArgs)
        Me.Focus()
        MyBase.OnMouseDown(e)
    End Sub

    Protected Sub InitializeComponent()
        Me.AutoScroll = True
        Me.BorderStyle = BorderStyle.None
        Me.TabStop = True
    End Sub
End Class
要在表单中插入此自定义控件,请在工具箱中找到它(在其顶部,查找名为
PanelWithFocus
)的控件,然后将其放到表单上。

如果要用此面板替换现有面板,请打开
Form.Designer
并将
Me.Panel1=New System.Windows.Forms.Panel()
更改为
Me.Panel1=New PanelWithFocus()


Friend WithEvents Panel 1 As Panel
与此相同,它将成为
Friend WithEvents Panel 1 As Panel WithFocus

面板控件不能具有焦点且不可选择。它只是一个容器。

您可以创建从面板派生的自定义控件,并使其能够接收焦点。
在这种情况下,这会有很大帮助,因为这样可以滚动自定义面板而无需任何其他逻辑。
即使另一个通常可以获得焦点的控件(如文本框)挡住了去路。

此实现修改面板控件(
ControlStyles.Selective
),使其能够接受焦点(
TabStop
属性也设置为True)。

OnMouseDown
也是隐藏的,因此,如果面板中的控件窃取了焦点,您只需单击面板将焦点移动到面板上,然后滚动即可。

Class PanelWithFocus
    Inherits System.Windows.Forms.Panel

    Public Sub New()
        Me.SetStyle(ControlStyles.Selectable, True)
        InitializeComponent()
    End Sub

    Protected Overrides Sub OnEnter(e As EventArgs)
        MyBase.OnEnter(e)
        Me.Focus()
    End Sub

    Protected Overrides Sub OnMouseDown(e As MouseEventArgs)
        Me.Focus()
        MyBase.OnMouseDown(e)
    End Sub

    Protected Sub InitializeComponent()
        Me.AutoScroll = True
        Me.BorderStyle = BorderStyle.None
        Me.TabStop = True
    End Sub
End Class
要在表单中插入此自定义控件,请在工具箱中找到它(在其顶部,查找名为
PanelWithFocus
)的控件,然后将其放到表单上。

如果要用此面板替换现有面板,请打开
Form.Designer
并将
Me.Panel1=New System.Windows.Forms.Panel()
更改为
Me.Panel1=New PanelWithFocus()

对于
Friend WithEvents Panel1 As Panel
,其将成为
Friend WithEvents Panel1 As PanelWithFocus