ScrollBar.Value Property

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 int Value { get; set; }
/** @property */
public int get_Value ()

/** @property */
public void set_Value (int value)

public function get Value () : int

public function set Value (value : int)

Property Value

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

Exception typeCondition

ArgumentOutOfRangeException

The assigned value is less than the Minimum property value.

-or-

The assigned value is greater than the Maximum property value.

The following code example uses the derived classes VScrollBar and HScrollBar, and sets some of their common properties. Their Maximum properties are set equal to the Height or Width of the Image assigned to their parent, a PictureBox. The LargeChange property is set equal to the size of the picture box (with the height or width of the scroll bars subtracted). The SmallChange property is set equal to the LargeChange property value divided by 5. Lastly, the Value property values for both of the scroll bars are set to 0. The result is vertical and horizontal scroll bars displayed on a picture box that displays the upper-left corner of the Image. The scroll bars will not scroll past the end of the image, and when a LargeChange scroll happens, the image will only move the same distance as the area displayed in the picture box. It takes five SmallChange scrolls to move the same distance as one LargeChange scroll. This code assumes a PictureBox, HScrollBar, VScrollBar, and Image have all been created on a Form. It also assumes that a reference to the System.Drawing namespace has been added. For additional code that can extend this example, see the ScrollBar class overview.

public void SetScrollBarValues() 
{
   // Set the Maximum, Minimum, LargeChange and SmallChange properties.
   this.vScrollBar1.Minimum = 0;
   this.hScrollBar1.Minimum = 0;
   // If the offset does not make the Maximum less than zero, set its value. 
   if( (this.pictureBox1.Image.Size.Width - pictureBox1.ClientSize.Width) > 0)
   {
      this.hScrollBar1.Maximum = this.pictureBox1.Image.Size.Width - pictureBox1.ClientSize.Width;
   }
   /* If the VScrollBar is visible, adjust the Maximum of the 
      HSCrollBar to account for the width of the VScrollBar. */
   if(this.vScrollBar1.Visible)
   {
      this.hScrollBar1.Maximum += this.vScrollBar1.Width;
   }
   this.hScrollBar1.LargeChange = this.hScrollBar1.Maximum / 10;
   this.hScrollBar1.SmallChange = this.hScrollBar1.Maximum / 20;
   // Adjust the Maximum value to make the raw Maximum value attainable by user interaction.
   this.hScrollBar1.Maximum += this.hScrollBar1.LargeChange;
     
   // If the offset does not make the Maximum less than zero, set its value.    
   if( (this.pictureBox1.Image.Size.Height - pictureBox1.ClientSize.Height) > 0)
   {
      this.vScrollBar1.Maximum = this.pictureBox1.Image.Size.Height - pictureBox1.ClientSize.Height;
   }
   /* If the HScrollBar is visible, adjust the Maximum of the 
      VSCrollBar to account for the width of the HScrollBar.*/
   if(this.hScrollBar1.Visible)
   {
      this.vScrollBar1.Maximum += this.hScrollBar1.Height;
   }
   this.vScrollBar1.LargeChange = this.vScrollBar1.Maximum / 10;
   this.vScrollBar1.SmallChange = this.vScrollBar1.Maximum / 20;
   // Adjust the Maximum value to make the raw Maximum value attainable by user interaction.
   this.vScrollBar1.Maximum += this.vScrollBar1.LargeChange;
}

public void SetScrollBarValues()
{
    // Set the Maximum, Minimum, LargeChange and SmallChange properties.
    this.vScrollBar1.set_Minimum(0);
    this.hScrollBar1.set_Minimum(0);

    // If the offset does not make the Maximum less than zero, 
    // set its value. 
    if (this.pictureBox1.get_Image().get_Size().get_Width() 
        - pictureBox1.get_ClientSize().get_Width() > 0) {
            this.hScrollBar1.set_Maximum(
                this.pictureBox1.get_Image().get_Size().get_Width() 
                - pictureBox1.get_ClientSize().get_Width());
    }

    /* If the VScrollBar is visible, adjust the Maximum of the 
       HSCrollBar to account for the width of the VScrollBar. 
     */
    if (this.vScrollBar1.get_Visible()) {
        this.hScrollBar1.set_Maximum(
            this.hScrollBar1.get_Maximum() + this.vScrollBar1.get_Width());
    }

    this.hScrollBar1.set_LargeChange(this.hScrollBar1.get_Maximum() / 10);
    this.hScrollBar1.set_SmallChange(this.hScrollBar1.get_Maximum() / 20);

    // Adjust the Maximum value to make the raw Maximum value attainable 
    // by user interaction.
    this.hScrollBar1.set_Maximum(
        this.hScrollBar1.get_Maximum() 
        + this.hScrollBar1.get_LargeChange());

    // If the offset does not make the Maximum less than zero, 
    // set its value.    
    if (this.pictureBox1.get_Image().get_Size().get_Height() 
        - pictureBox1.get_ClientSize().get_Height() > 0) {
            this.vScrollBar1.set_Maximum(
                this.pictureBox1.get_Image().get_Size().get_Height() 
                - pictureBox1.get_ClientSize().get_Height());
    }

    /* If the HScrollBar is visible, adjust the Maximum of the 
       VSCrollBar to account for the width of the HScrollBar.
     */
    if (this.hScrollBar1.get_Visible()) {
        this.vScrollBar1.set_Maximum(this.vScrollBar1.get_Maximum() 
            + this.hScrollBar1.get_Height());
    }

    this.vScrollBar1.set_LargeChange(this.vScrollBar1.get_Maximum() / 10);
    this.vScrollBar1.set_SmallChange(this.vScrollBar1.get_Maximum() / 20);

    // Adjust the Maximum value to make the raw Maximum value attainable 
    // by user interaction.
    this.vScrollBar1.set_Maximum(
        this.vScrollBar1.get_Maximum() 
        + this.vScrollBar1.get_LargeChange());
} //SetScrollBarValues

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

Community Additions

ADD
Show: