Export (0) Print
Expand All

ScrollableControl::AutoScroll Property

Updated: September 2008

Gets or sets a value indicating whether the container enables the user to scroll to any controls placed outside of its visible boundaries.

Namespace:  System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)

public:
virtual property bool AutoScroll {
	bool get ();
	void set (bool value);
}

Property Value

Type: System::Boolean
true if the container enables auto-scrolling; otherwise, false. The default value is false.

When true, this property enables the container to have a virtual size that is larger than its visible boundaries.

There is currently a limitation in Windows Forms that prevents all classes derived from ScrollableControl from acting properly when both RightToLeft is enabled and AutoScroll is set to Yes. For example, let's say that you place a control such as Panel—or a container class derived from Panel (such as FlowLayoutPanel or TableLayoutPanel)—on your form. If you set AutoScroll on the container to Yes and then set the Anchor property on one or more of the controls inside of the container to Right, then no scrollbar ever appears. The class derived from ScrollableControl acts as if AutoScroll were set to No.

Currently, the only workaround is to nest the ScrollableControl inside another ScrollableControl. For instance, if you need TableLayoutPanel to work in this situation, you can place it inside of a Panel control and set AutoScroll on the Panel to Yes.

NoteNote:

AutoScroll maintains the visibility of the scrollbars automatically. Therefore, setting the HScroll or VScroll property to true has no effect when AutoScroll is enabled.

The following code example shows how horizontal and/or vertical scroll bars are provided automatically as needed when the AutoScroll property is set to true. To run the example, follow these steps:

  1. Create a new Windows Forms application.

  2. Add a Panel to the form.

  3. Add a TextBox to the panel and name it text1.

  4. Move the text box so that the right part extends beyond the right edge of the panel.

    You should see only an outline of the part of the text box that is outside the bounds of the panel. If the whole text box is visible, the text box is on the form and not in the panel.

  5. Add a Button to the form.

  6. Add a handler for the Click event of the button.

  7. Add the following example code and call it from the button's Click handler.

When you run the example, you can only see the part of the text box that is inside the boundaries of the panel. When you click the button, you will see a horizontal scroll bar appear that will enable you to see the rest of the text box.

If you position a part of the text box below the bottom of the panel, you will see a vertical scroll bar when you click the button.

The example code checks to see whether the text box is outside the bounds of the panel before it sets the AutoScroll property to true, and before it sets the AutoScrollMargin property. This out-of-bounds check is not required. If AutoScroll is set to true, no scroll bars will appear when the text box is completely within the panel. Also, you can leave the margins at their default settings of 0,0.

void SetAutoScrollMargins()
{
   /* If the text box is outside the panel's bounds, 
          turn on auto-scrolling and set the margin. */ 
   if ( text1->Location.X > panel1->Location.X || text1->Location.Y > panel1->Location.Y )
   {
      panel1->AutoScroll = true;

      /* If the AutoScrollMargin is set to less 
                than (5,5), set it to 5,5. */ 
      if ( panel1->AutoScrollMargin.Width < 5 || panel1->AutoScrollMargin.Height < 5 )
      {
         panel1->SetAutoScrollMargin( 5, 5 );
      }
   }
}

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

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

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 3.5, 2.0

Date

History

Reason

September 2008

Revised AutoScroll code example.

Customer feedback.

Community Additions

ADD
Show:
© 2014 Microsoft