views:

35

answers:

2

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 Point(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.

+1  A: 

You can use the AutoScrollPosition property

Thomas Levesque
I specified in the edit the problem.
serhio
+1  A: 

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...

serhio