Export (0) Print
Expand All

ScrollBar.Maximum Property

Gets or sets the upper limit of values of the scrollable range.

Namespace: System.Windows.Forms
Assembly: System.Windows.Forms (in system.windows.forms.dll)

public int Maximum { get; set; }
/** @property */
public int get_Maximum ()

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

public function get Maximum () : int

public function set Maximum (value : int)

Property Value

A numeric value. The default value is 100.

You might consider adjusting the Maximum property dynamically to match the size of the scroll bar's parent in proportion to pixel size or to the number of rows or lines displayed.

NoteNote

The value of a scroll bar cannot reach its maximum value through user interaction at run time. The maximum value that can be reached is equal to 1 plus the Maximum property value minus the LargeChange property value. The maximum value can only be reached programmatically.

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:
© 2015 Microsoft