ScrollBars 枚举

定义

指定 Panel 控件中滚动条的可见性和位置。

此枚举支持其成员值的按位组合。

C#
[System.Flags]
public enum ScrollBars
继承
ScrollBars
属性

字段

Auto 4

根据需要,可显示水平滚动条、垂直滚动条或这两种滚动条。 要不然也可以不显示任何滚动条。

Both 3

同时显示水平滚动条和垂直滚动条。

Horizontal 1

只显示水平滚动条。

None 0

不显示滚动条。

Vertical 2

只显示垂直滚动条。

示例

下面的代码示例演示如何以声明方式将 ScrollBars 属性 Auto设置为 。 面板包含一个表,其整个内容超过面板的大小。 这会导致呈现面板时自动显示垂直和水平滚动条。 然后,用户可以滚动以查看表中的所有数据。

ASP.NET (C#)
<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">
    private void Page_Load(object sender, EventArgs e)
    {
        // Add more rows and columns to the table than can
        // be displayed in the panel area.
        // Scroll bars will be required to view all the data.

        // Add rows and columns to the table.
        for (int rowNum = 0; rowNum < 51; rowNum++)
        {
            TableRow tempRow = new TableRow();
            for (int cellNum = 0; cellNum < 11; cellNum++)
            {
                TableCell tempCell = new TableCell();
                tempCell.Text = 
                    String.Format("({0}, {1})", rowNum, cellNum);
                tempRow.Cells.Add(tempCell);
            }
            Table1.Rows.Add(tempRow);
        }
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head2" runat="server">
    <title>Panel Scrollbars - C# Example</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>

    <h3>Panel.ScrollBars Property Example</h3>        

    <asp:Panel ID="Panel1" runat="Server"
      Height="300px" Width="400px"
      BackColor="Aqua" ScrollBars="Auto">

      <asp:Table ID="Table1" runat="Server"></asp:Table>  

    </asp:Panel>         

    </div>
    </form>
</body>
</html>

下面的代码示例演示 ScrollBars 枚举值。 ListBox使用枚举值填充ScrollBars控件。 根据用户从列表框中选择的值更改面板中显示的滚动条。

ASP.NET (C#)
<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">
    private void Page_Load(object sender, EventArgs e)
    {
        // Add more rows and columns to the table than can
        // be displayed in the panel area.
        // Scroll bars will be required to view all the data.

        // Add rows and columns to the table.
        for (int rowNum = 0; rowNum < 51; rowNum++)
        {
            TableRow tempRow = new TableRow();
            for (int cellNum = 0; cellNum < 11; cellNum++)
            {
                TableCell tempCell = new TableCell();
                tempCell.Text = 
                    String.Format("({0}, {1})", rowNum, cellNum);
                tempRow.Cells.Add(tempCell);
            }
            Table1.Rows.Add(tempRow);
        }
    }

    private void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        // Determine which list item was clicked.
        // Display the selected scroll bars in the panel.
        switch (ListBox1.SelectedIndex)
        {
            case 0:
                Panel1.ScrollBars = ScrollBars.None;
                break;
            case 1:
                Panel1.ScrollBars = ScrollBars.Horizontal;
                break;
            case 2:
                Panel1.ScrollBars = ScrollBars.Vertical;
                break;
            case 3:
                Panel1.ScrollBars = ScrollBars.Both;
                break;
            case 4:
                Panel1.ScrollBars = ScrollBars.Auto;
                break;
            default:
                throw new Exception("Select a valid list item.");
        }
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head2" runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>

    <h3>Panel.ScrollBars Property Example</h3>

    <h4>Select the scrollbars to display in the panel.</h4>
    <asp:ListBox ID="ListBox1" runat="Server"
      Rows="5" AutoPostBack="True"
      SelectionMode="Single"
      OnSelectedIndexChanged="ListBox1_SelectedIndexChanged">
      <asp:ListItem>None</asp:ListItem>
      <asp:ListItem>Horizontal</asp:ListItem> 
      <asp:ListItem>Vertical</asp:ListItem>
      <asp:ListItem>Both</asp:ListItem> 
      <asp:ListItem>Auto</asp:ListItem>              
    </asp:ListBox>

    <hr />              

    <asp:Panel ID="Panel1" runat="Server"
      Height="300px" Width="400px" BackColor="Aqua">
      <asp:Table ID="Table1" runat="Server" />
    </asp:Panel>           
         
    </div>
    </form>
</body>
</html>

注解

ScrollBars枚举表示控件中Panel滚动条的可见性和位置。 该 ScrollBars 属性使用这些枚举值指定要在控件中显示的 Panel 滚动条的类型。 属性的 ScrollBars 默认值为 None,指示不显示滚动条。

如果为属性指定Auto,当控件中Panel内容的大小超过控件的大小Panel时,将自动显示滚动ScrollBars条。 例如,如果控件包含一个 Panel 表,并且面板不够宽,无法显示表格中的所有行,则会显示垂直滚动条。 如果表格的大小超过面板的高度和宽度,将显示垂直滚动条和水平滚动条。

适用于

产品 版本
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8

另请参阅