ScrollBar.Value Property

 
System_CAPS_noteNote

The .NET API Reference documentation has a new home. Visit the .NET API Browser on docs.microsoft.com to see the new experience.

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

Namespace:   System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)

<BindableAttribute(True)>
Public Property Value As Integer

Property Value

Type: System.Int32

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

Exception Condition
ArgumentOutOfRangeException

The assigned value is less than the Minimum property value.

-or-

The assigned value is greater than the Maximum property value.

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.

System_CAPS_noteNote

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.

Private Sub HandleScroll(ByVal sender As [Object], ByVal e As ScrollEventArgs) _
  Handles vScrollBar1.Scroll, hScrollBar1.Scroll

    'Create a graphics object and draw a portion of the image in the PictureBox.
    Dim g As Graphics = pictureBox1.CreateGraphics()

    Dim xWidth As Integer = pictureBox1.Width
    Dim yHeight As Integer = pictureBox1.Height

    Dim x As Integer
    Dim y As Integer

    If (e.ScrollOrientation = ScrollOrientation.HorizontalScroll) Then

        x = e.NewValue
        y = vScrollBar1.Value

    Else 'e.ScrollOrientation == ScrollOrientation.VerticalScroll

        y = e.NewValue
        x = hScrollBar1.Value
    End If

    'First Rectangle: Where to draw the image.
    'Second Rectangle: The portion of the image to draw.

    g.DrawImage(pictureBox1.Image, _
      New Rectangle(0, 0, xWidth, yHeight), _
      New Rectangle(x, y, xWidth, yHeight), _
      GraphicsUnit.Pixel)

    pictureBox1.Update()
End Sub

.NET Framework
Available since 1.1
Return to top
Show: