需要用鼠标滚轮滚动用户控件

发布于 2024-07-14 03:37:24 字数 255 浏览 8 评论 0原文

我有一个我创建的用户控件。 我在其右侧添加了一个面板和一个垂直滚动条。 我希望能够用鼠标滚轮滚动它。 问题是似乎没有任何事件在鼠标滚轮上触发。 如果我取下面板,则用户控件具有焦点,它将在表单中的鼠标滚轮上触发。 但是,打开面板后,它似乎不会触发面板的鼠标滚轮事件,也不会触发控件内甚至表单上的用户控件。 最好的解决方案是在用户控件中触发事件,但我什至会接受表单上的事件并将其反馈到用户控件中。

如果重要的话,我正在使用 vb.net 和 vs2005。

I have a usercontrol I created. I added a panel and a vertical scrollbar to the right of it. I'd like to be able to scroll it with the mousewheel. The problem is there doesn't seem to be any events that fire on mousewheel. If I take the panel off then the usercontrol has focus and it will fire on mousewheel in the form. But with the panel on it doesn't seem to fire the mousewheel event of the panel, or the usercontrol within the control or even on the form. The best solution would be to have an event fire in the usercontrol but I'd even accept an event on the form and feed it back into the usercontrol.

I'm using vb.net and vs2005 if it matters.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

喜爱皱眉﹌ 2024-07-21 03:37:24

是的,面板可以有焦点。 你只需要给它焦点,我更喜欢在鼠标悬停时使用。

我这样做了,问题就解决了。

yes, a panel can have focus. You just have to give it focus, I prefer to use on mouse over.

I did this and its problem solved.

梦罢 2024-07-21 03:37:24

我一整天都在研究这个问题,我可能已经弄清楚了。 鼠标滚轮事件仅发送到具有焦点的控件。 面板无法获得焦点。 由于面板覆盖了用户控件,因此它也无法获得焦点。 (除非它是表单上的唯一控件)如果在面板上 mouseenter 事件我调用 me.focus 它将焦点设置到用户控件,允许它接收鼠标滚轮事件。 该事件在窗体和控件中触发。 如果有更好的方法,我仍然愿意接受建议,因为这看起来有点老套。

I've been researching this all day, I may have figured this out. The mousewheel event is only sent to the control with focus. A panel can't have focus. Since the panel is covering the usercontrol it can't get the focus either. (unless it's the only control on the form) If on the panel mouseenter event I call me.focus it sets the focus to the usercontrol allowing it to receive the mousewheel event. The event fires in the form and the control both. I'm still open to suggestions if there's a better way though, as this seems a little hacky.

香草可樂 2024-07-21 03:37:24
  • 创建一个新的VB.NET Winforms项目
  • 在窗体上放置一个Panel控件
  • 将面板的“AutoScroll”属性设置为“true”
  • 将以下代码放置在窗体的load事件中

    对于 i 作为整数 = 1 到 100 
          调暗 b 作为新按钮() 
          b.Text = i.ToString() 
          b.尺寸 = 新尺寸(60, 40) 
          b.位置 = 新点(0, (i * b.高度) - b.高度) 
          b.Parent = Panel1 
          Panel1.Controls.Add(b) 
      下一个 
      

“运行项目”表单的加载事件中。

您应该在 Panel 控件内看到一个包含 100 个按钮的窗体。
Panel 控件应包含一个垂直 ScrollBar。
使用面板内的滚轮应滚动按钮。

希望这个例子有帮助。

编辑

<块引用>

我在其右侧添加了一个面板和一个垂直滚动条。

这不是正确的做法。
您需要使用面板的AutoScroll属性。

编辑
另一个示例:

  • 创建一个新的 VB.NET 项目
  • 在表单上放置两个按钮
  • 创建一个新的用户控件
  • 将用户控件的 autoscroll 属性设置为 true
  • 将以下代码粘贴到表单加载事件中

    将 uc 调暗为新 UserControl1 
      uc.Parent = 我 
      Me.Controls.Add(uc) 
      uc.Size = 新大小(100, 100) 
      uc.Location = 新点(0, 0) 
      对于 i 作为整数 = 1 到 100 
          调暗 b 作为新按钮() 
          b.Text = i.ToString() 
          b.尺寸 = 新尺寸(60, 40) 
          b.位置 = 新点(0, (i * b.高度) - b.高度) 
          b.Parent = uc 
          uc.Controls.Add(b) 
      下一个 
      

运行程序。

单击按钮(在表单上)。
请注意,您必须单击 UserControl 才能设置其焦点并使用滚轮。

  • Create a new VB.NET Winforms project
  • Place a Panel control on the form
  • Set the "AutoScroll" property of the panel to "true"
  • Place the following code in the load event of the form

    For i As Integer = 1 To 100
        Dim b As New Button()
        b.Text = i.ToString()
        b.Size = New Size(60, 40)
        b.Location = New Point(0, (i * b.Height) - b.Height)
        b.Parent = Panel1
        Panel1.Controls.Add(b)
    Next
    

Run the project.

You should see a form with 100 buttons inside the Panel control.
The Panel control should contain a vertical ScrollBar.
Using the scroll wheel inside the Panel should scroll through the Buttons.

Hope this example helps.

Edit

I added a panel and a vertical scrollbar to the right of it.

That is not the correct way to do it.
You need to use the AutoScroll property of the Panel.

Edit
Another Example:

  • Create a new VB.NET project
  • Place two buttons on the form
  • Create a new usercontrol
  • Set the autoscroll property of the usercontrol to true
  • Paste the following code in the form load event

    Dim uc As New UserControl1
    uc.Parent = Me
    Me.Controls.Add(uc)
    uc.Size = New Size(100, 100)
    uc.Location = New Point(0, 0)
    For i As Integer = 1 To 100
        Dim b As New Button()
        b.Text = i.ToString()
        b.Size = New Size(60, 40)
        b.Location = New Point(0, (i * b.Height) - b.Height)
        b.Parent = uc
        uc.Controls.Add(b)
    Next
    

Run the program.

Click the Buttons (on the Form).
Notice, that you have to click the UserControl to set its focus and use the scroll wheel.

放手` 2024-07-21 03:37:24

这里是一篇关于在 vb.net 中使用鼠标事件的文章。 它特别提到了鼠标滚动。

Here is an article about working with mouse events in vb.net. It specifically mentions mouse scroll.

倾城花音 2024-07-21 03:37:24

您还可以添加以下代码行:

    private void UserControl1_Scroll(object sender, ScrollEventArgs e)
    {
        System.Diagnostics.Debug.WriteLine(System.DateTime.Now.ToString("hh:mm:ss") + " Scrolling inside" + e.NewValue + " <- " + e.OldValue);
        this.VerticalScroll.Value = e.NewValue;
    }

这允许您在上下移动滚动条时看到控件中的项目。 如果没有它,控件仅在释放滚动条时滚动。

You might also add the following line of code:

    private void UserControl1_Scroll(object sender, ScrollEventArgs e)
    {
        System.Diagnostics.Debug.WriteLine(System.DateTime.Now.ToString("hh:mm:ss") + " Scrolling inside" + e.NewValue + " <- " + e.OldValue);
        this.VerticalScroll.Value = e.NewValue;
    }

This allows you to see items in the control as you move the scrollbar up and down. Without it the control only scrolls when the scrollbar is released.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文