ScrollableControl.AutoScroll ScrollableControl.AutoScroll ScrollableControl.AutoScroll ScrollableControl.AutoScroll Property

定义

获取或设置一个值,该值指示容器是否允许用户滚动到任何放置在其可见边界之外的控件。Gets or sets a value indicating whether the container enables the user to scroll to any controls placed outside of its visible boundaries.

public:
 virtual property bool AutoScroll { bool get(); void set(bool value); };
public virtual bool AutoScroll { get; set; }
member this.AutoScroll : bool with get, set
Public Overridable Property AutoScroll As Boolean

属性值

如果容器允许自动滚动,则为 true;否则为 falsetrue if the container enables auto-scrolling; otherwise, false. 默认值为 falseThe default value is false.

示例

下面的代码示例演示当AutoScroll属性设置为true时,如何根据需要自动提供水平和/或垂直滚动条。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. 创建新的 Windows 窗体应用程序。Create a new Windows Forms application.

  2. 在窗体上添加一个 Panel 控件。Add a Panel to the form.

  3. 向面板中添加,并将其text1命名为。TextBoxAdd 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. 在窗体上添加一个 Button 控件。Add a Button to the form.

  6. 为按钮的Click事件添加处理程序。Add a handler for the Click event of the button.

  7. 添加下面的示例代码,并从按钮的Click处理程序调用它。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.

示例代码在将AutoScroll属性设置为true之前, AutoScrollMargin以及在设置属性之前,检查文本框是否在面板的边界之外。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. 如果AutoScroll设置为true,则当文本框完全在面板中时,将不会显示任何滚动条。If AutoScroll is set to true, no scroll bars will appear when the text box is completely within the panel. 此外,还可以将边距设置为默认设置0,0。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 );
      }
   }
}
private 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);
       }
    }
 }
 
Private Sub 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) Or _
        (text1.Location.Y > panel1.Location.Y) Then
        panel1.AutoScroll = True
        ' If the AutoScrollMargin is set to less
        ' than (5,5), set it to 5,5. 
        If (panel1.AutoScrollMargin.Width < 5) Or _
            (panel1.AutoScrollMargin.Height < 5) Then
            
            panel1.SetAutoScrollMargin(5, 5)
        End If
    End If
End Sub

注解

如果true为,则此属性使容器的虚拟大小大于其可见边界。When true, this property enables the container to have a virtual size that is larger than its visible boundaries.

目前 Windows 窗受到如下限制:当 RightToLeft 启用且 AutoScroll设置为 Yes 时,系统将阻止所有派生自 ScrollableControl 的类正常运行。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. 例如,假设您在窗体上放置了一个控件, Panel如或派生自Panel (例如FlowLayoutPanelTableLayoutPanel)的一个容器。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. 如果将容器上的 AutoScroll 设置为 Yes,然后将容器内一个或多个控件的 Anchor 属性设置为 Right,则不会出现任何滚动条。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. 派生自 ScrollableControl 的类的运行方式与将 AutoScroll 设置为 No 时相同。The class derived from ScrollableControl acts as if AutoScroll were set to No.

目前,唯一的解决方法是将 ScrollableControl 嵌套到其他 ScrollableControl 内。Currently, the only workaround is to nest the ScrollableControl inside another ScrollableControl. 例如,如果需要在此情况下运行 TableLayoutPanel,可以将它置于 Panel 控件中并将 Panel 上的 AutoScroll 设置为 YesFor 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.

备注

AutoScroll自动维护滚动条的可见性。AutoScroll maintains the visibility of the scrollbars automatically. 因此,当启用HScrollVScroll true ,将AutoScroll或属性设置为不起作用。Therefore, setting the HScroll or VScroll property to true has no effect when AutoScroll is enabled.

适用于

另请参阅