如何在面板中滚动项目 [英] How do I scroll over items in a panel

查看:65
本文介绍了如何在面板中滚动项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在vb.net中创建一个winforms应用程序,并且有一个带有一些标签的面板.标签越过了面板的底部,因此面板自动显示了垂直滚动条(这就是我想要的).但是,每当我使用鼠标滚轮向下滚动面板时,当其中一个标签在鼠标下方向上滚动时,它将停止滚动.就像焦点从面板更改为标签,标签不需要滚动一样.我想使用鼠标滚轮来滚动整个面板,无论鼠标下方是什么.

I'm creating a winforms app in vb.net and I have a panel with some labels in it. The labels go past the bottom of the panel so the panel shows a vertical scroll bar automatically (which is what I want). However, whenever I scroll the panel down using the mouse wheel, it stops scrolling when one of the labels scrolls up under the mouse. It's like the focus has changed from the panel to the label and the label doesn't need to scroll. I want to just scroll the whole panel using the mouse wheel no matter what comes under the mouse.

推荐答案

面板控件无法获得焦点,并且无法选择.这只是一个容器.

A Panel control can't have focus and is not selectable. It's just a container.

您可以制作一个派生自Panel的自定义控件,并使它能够接收焦点.
在这种情况下,这很有帮助,因为这允许滚动自定义面板而无需任何其他逻辑.
即使另一个通常可以获得焦点的控件(如TextBox)也挡在了路上.

You can make a custom control derived from Panel and enable it to receive the focus.
This helps a lot in this situation, because this allows to scroll the custom Panel without any other logic behind.
Even if another control, which usually can get the focus - like a TextBox - gets in the way.

此实现修改了Panel控件样式(ControlStyles.Selectable)以使其接受焦点(TabStop属性也设置为True).

This implementation modifies a Panel control Style (ControlStyles.Selectable) to enable it to accept the Focus (the TabStop property is also set to True).

OnMouseDown也被覆盖,因此,如果面板内的控件窃取了焦点,则只需单击面板以将焦点移至其上,然后滚动它即可.

OnMouseDown is also overidden, so that, if a control inside the Panel steals the Focus, you just need to click on the Panel to move the focus on it and then scroll it.

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的控件),然后将其放在窗体上.

To insert this custom control in a Form, locate it in the ToolBox (at the top of it, look for a control named PanelWithFocus) and drop it on the Form.

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

If you want to substitute an existing Panel with this one, open your Form.Designer and change Me.Panel1 = New System.Windows.Forms.Panel() with Me.Panel1 = New PanelWithFocus().

Friend WithEvents Panel1 As Panel相同,即变为Friend WithEvents Panel1 As PanelWithFocus

这篇关于如何在面板中滚动项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆