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 Forms 應用程式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 Form 目前有一項限制,會導致在 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 已設為 NoThe 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.

適用於

另請參閱