ScrollBar.Value Property

Definition

Gets or sets a numeric value that represents the current position of the scroll box on the scroll bar control.

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

Property Value

A numeric value that is within the Minimum and Maximum range. The default value is 0.

Attributes

Exceptions

The assigned value is less than the Minimum property value.

-or-

The assigned value is greater than the Maximum property value.

Examples

The following example scrolls an image in a picture box. It uses the Value of the scrollbar to redraw a new part of the image whenever the user scrolls. This code example is part of a larger example provided for the ScrollBar class overview.

Nota

For instructions about how to run this example in Visual Studio, see How to: Compile and Run a Complete Windows Forms Code Example Using Visual Studio.

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();
}

Applies to

Produk Versi
.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, 10