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

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

问题描述

我正在创建一个应用程序,其中有一个带有一些标签的面板.
一些标签位于面板的显示矩形之外,因此面板会自动显示垂直滚动条(这正是我想要的).

I'm creating an app where I have a Panel with some Labels in it.
Some of the Labels are located beyond the Display Rectangle of the Panel, so the Panel shows a vertical ScrollBar automatically (which is what I want).

但是,每当我使用鼠标滚轮向下滚动面板时,当其中一个标签在鼠标下方向上滚动时,它就会停止滚动.就像焦点已从 Panel 更改为 Label 并且 Label 阻止了 Panel 滚动.

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 prevents the Panel from scrolling.

无论鼠标下方是什么,我只想使用鼠标滚轮滚动整个面板.

I want to just scroll the whole Panel using the mouse wheel no matter what comes under the mouse.

推荐答案

Panel 控件无法获得焦点且不可选.它只是一个容器.

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.

此实现修改了面板控件 Style (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 overridden, 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 System.Windows.Forms.Panel()with Me.Panel1 = New PanelWithFocus().

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天全站免登陆