开发者

Find the upper left coordinate of a panel when scrolling

How to find the upper left coordinate of a panel when scrolling? (.net 2)

Let's say an example in VB.NET that maintains a textBox in the left border of the custom the panel (myPanel.vb):

  Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
    MyBase.OnPaint(e)
    TextBox1.Location = New Poi开发者_如何学Gont(AutoScrollPosition.X, TextBox1.Location.Y)
    ...

this code does not work...

I tried also

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

not work as well.

In first case AutoscrollPosition remains always = 0, in the second, the panel does not scroll at all.


You can use the AutoScrollPosition property


Finally, find the problem... this problem is always linked to the focusing of the panel to first enabled control (texbox in our case) problem.

When the texbox is active it recieves the focus and turns the scroll position back.

So, the solution to mantain the texbox to the left border was to

A) Disable the textbox (textBox1.Enabled = false)

B) In the host controls panel override the OnPaint with:

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)

Surely, it will be interesting to do the same thing with a enabled textBox...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜