Export (0) Print
Expand All

ScrollableControl.AutoScrollPosition Property

Gets or sets the location of the auto-scroll position.

Namespace: System.Windows.Forms
Assembly: System.Windows.Forms (in system.windows.forms.dll)

public Point AutoScrollPosition { get; set; }
/** @property */
public Point get_AutoScrollPosition ()

/** @property */
public void set_AutoScrollPosition (Point value)

public function get AutoScrollPosition () : Point

public function set AutoScrollPosition (value : Point)

Property Value

A Point that represents the auto-scroll position in pixels.

The AutoScrollPosition property is used to adjust the position of controls contained on the scrollable control.

When you add controls to a scrollable control, you must add or subtract the x-coordinate and y-coordinate of the AutoScrollPosition as an offset from the corresponding coordinates of the Control.Location property of the control being added. To add a control above or to the left of the scroll position, you must add the offset to the desired location. Alternatively, to add a control below or to the right of the scroll position, you must subtract the offset.

NoteNote

AutoScrollPosition represents the location of the scrollable control's display rectangle. The X and Y coordinate values retrieved are negative if the control has scrolled away from its starting position (0,0). When you set this property, you must always assign positive X and Y values to set the scroll position relative to the starting position. For example, if you have a horizontal scroll bar and you set x and y to 200, you move the scroll 200 pixels to the right; if you then set x and y to 100, the scroll appears to jump the left by 100 pixels, because you are setting it 100 pixels away from the starting position. In the first case, AutoScrollPosition returns {-200, 0}; in the second case, it returns {-100,0}.

To detect when AutoScrollPosition changes, create an event handler for the Paint event, save the old position value in a private variable, and compare the new value to the old value on subsequent Paint events.

The following code example uses the ScrollableControl derived class Panel and adds a button to the upper left corner of the scrollable area. The example allows for the offset determined by the AutoScrollPosition. The example was written under the assumption that you have a Form that contains a Panel with a Button on it. To enable auto-scrolling, place the button outside of the client area of the Panel.

private void button1_Click(object sender, EventArgs e)
{
   /* Add a button to top left corner of the 
    * scrollable area, allowing for the offset. */
   panel1.AutoScroll = true;
   Button myButton = new Button();
   myButton.Location = new Point(
      0 + panel1.AutoScrollPosition.X, 
      0 + panel1.AutoScrollPosition.Y);
   panel1.Controls.Add(myButton);
}

private void button1_Click(Object sender, EventArgs e)
{
    /* Add a button to top left corner of the 
       scrollable area, allowing for the offset. 
     */
    panel1.set_AutoScroll(true);
    Button myButton = new Button();
    myButton.set_Location(new Point(0 + panel1.get_AutoScrollPosition().
        get_X(), 0 + panel1.get_AutoScrollPosition().get_Y()));
    panel1.get_Controls().Add(myButton);
} //button1_Click

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0

Community Additions

ADD
Show:
© 2014 Microsoft