ScrollBar.Value 属性

定义

获取或设置表示滚动框在滚动条控件中的当前位置的数值。

C#
[System.ComponentModel.Bindable(true)]
public int Value { get; set; }

属性值

处于 MinimumMaximum 范围内的数值。 默认值为 0。

属性

例外

所赋的值小于 Minimum 属性值。

- 或 -

所赋的值大于 Maximum 属性值。

示例

以下示例滚动图片框中的图像。 每当用户滚动时,它都使用 Value 滚动条的 来重绘图像的新部分。 此代码示例是为类概述提供的更大示例的 ScrollBar 一部分。

备注

有关如何在 Visual Studio 中运行此示例的说明,请参阅如何:使用 Visual Studio 编译和运行完整的Windows 窗体代码示例

C#
private void HandleScroll(Object sender, ScrollEventArgs e)
{
    //Create a graphics object and draw a portion of the image in the PictureBox.
    Graphics g = pictureBox1.CreateGraphics();

    int xWidth = pictureBox1.Width;
    int yHeight = pictureBox1.Height;

    int x;
    int y;

    if (e.ScrollOrientation == ScrollOrientation.HorizontalScroll)
    {
        x = e.NewValue;
        y = vScrollBar1.Value;
    }
    else //e.ScrollOrientation == ScrollOrientation.VerticalScroll
    {
        y = e.NewValue;
        x = hScrollBar1.Value;
    }

    g.DrawImage(pictureBox1.Image,
      new Rectangle(0, 0, xWidth, yHeight),  //where to draw the image
      new Rectangle(x, y, xWidth, yHeight),  //the portion of the image to draw
      GraphicsUnit.Pixel);

    pictureBox1.Update();
}

适用于

产品 版本
.NET Framework 1.1, 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, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9