ScrollableControl.AutoScroll 屬性

定義

取得或設定值,指出容器是否可讓使用者捲動至任何放置在可視界限以外的控制項。

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

屬性值

Boolean

如果容器啟用自動捲動,則為 true,否則為 false。 預設值是 false

範例

下列程式碼範例顯示當 屬性設定為 trueAutoScroll ,如何視需要自動提供水準和/或垂直捲動條。 若要執行範例,請遵循下列步驟:

  1. 新建 Windows Forms 應用程式

  2. Panel 加入表單。

  3. TextBox將 新增至面板,並將它命名為 text1

  4. 移動文字方塊,讓右部分延伸到面板的右邊緣。

    您應該只會看到文字方塊部分的外框,而該文字方塊超出面板界限。 如果顯示整個文字方塊,文字方塊位於表單上,而不是在面板中。

  5. Button 加入表單。

  6. Click 按鈕的事件新增處理常式。

  7. 新增下列範例程式碼,並從按鈕的 Click 處理常式呼叫它。

當您執行範例時,只能看到面板界限內文字方塊的一部分。 當您按一下按鈕時,您會看到水準捲軸隨即出現,可讓您看到文字方塊的其餘部分。

如果您在面板底部放置文字方塊的一部分,當您按一下按鈕時,會看到垂直捲動條。

範例程式碼會檢查文字方塊是否在面板的界限之外,再將 屬性設定 AutoScroll AutoScrollMargintrue ,以及設定 屬性之前。 不需要此超出界限檢查。 如果 AutoScroll 設定為 true ,當文字方塊完全在面板內時,將不會顯示捲軸。 此外,您可以將邊界保留為 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 ,這個屬性可讓容器具有大於其可見界限的虛擬大小。

注意

在 Windows Forms,當子控制項錨定在右或下 (Control 包含 RightBottom) 容器的行為就像設定為 false 一樣 AutoScroll

Windows Form 目前有一項限制,會導致在 RightToLeft 啟用且 AutoScroll 設為 true 的情況下,所有衍生自 ScrollableControl 的類別都無法正常運作。 例如,假設您在表單上放置一個控制項,例如 Panel 或衍生自 Panel (的容器類別,例如 FlowLayoutPanelTableLayoutPanel) 。 如果您將容器上的 AutoScroll 設為 true,然後在容器內的一或多個控制項上,將 Anchor 屬性設為 Right,則不顯示任何捲軸。 衍生自 ScrollableControl 的類別作用如同 AutoScroll 已設為 false。 目前唯一的解決方法是將 ScrollableControl 巢放在另一個 ScrollableControl 中。 例如,如果您需要 TableLayoutPanel 在此情況下運作,您可以將它放在 Panel 控制項中,並將 Panel 上的 AutoScroll 設為 true

注意

AutoScroll 會自動維護捲軸的可見度。 因此,啟用 時 AutoScrollHScroll 將 或 VScroll 屬性設定為 true 沒有作用。

適用於

另請參閱