滚动时查找面板的左上角坐标

时间:2010-01-08 17:09:03

标签: .net winforms scroll

滚动时如何查找面板的左上角坐标? (.net 2)

假设在VB.NET中的一个示例,它在自定义面板(myPanel.vb)的左边框中维护一个textBox:

  Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
    MyBase.OnPaint(e)
    TextBox1.Location = New Point(AutoScrollPosition.X, TextBox1.Location.Y)
    ...

此代码不起作用...

我也尝试了

Dim parentPanel As Panel = DirectCast(Parent, Panel)
TextBox1.Location = _ 
    New Point(parentPanel.AutoScrollPosition.X, TextBox1.Location.Y)

也不行。

在第一种情况下,AutoscrollPosition始终为= 0,在第二种情况下,面板根本不会滚动。

2 个答案:

答案 0 :(得分:2)

您可以使用AutoScrollPosition属性

答案 1 :(得分:1)

最后,找到问题...此问题始终与focusing of the panel to first enabled control (texbox in our case) problem

相关联

当texbox处于活动状态时,它会接收焦点并返回滚动位置。

因此,将texbox保留在左边界的解决方案是

A)禁用文本框(textBox1.Enabled = false)

B)在主机控件面板中,使用:

覆盖 OnPaint
Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
  Dim parentPanel As Panel = DirectCast(Parent, Panel)
  TextBox1.Location = _ 
      New Point(-parentPanel.AutoScrollPosition.X, TextBox1.Location.Y)

当然,使用启用的textBox做同样的事情会很有趣......