Export (0) Print
Expand All
4 out of 6 rated this helpful - Rate this topic

ScrollBar Class

Implements the basic functionality of a scroll bar control.

For a list of all members of this type, see ScrollBar Members.

System.Object
   System.MarshalByRefObject
      System.ComponentModel.Component
         System.Windows.Forms.Control
            System.Windows.Forms.ScrollBar
               System.Windows.Forms.HScrollBar
               System.Windows.Forms.VScrollBar

[Visual Basic]
MustInherit Public Class ScrollBar
   Inherits Control
[C#]
public abstract class ScrollBar : Control
[C++]
public __gc __abstract class ScrollBar : public Control
[JScript]
public abstract class ScrollBar extends Control

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Remarks

You typically do not inherit directly from ScrollBar. To create your own scroll bar class, inherit from the VScrollBar or HScrollBar class.

To adjust the value range of the scroll bar control, set the Minimum and Maximum properties. To adjust the distance the scroll box moves, set the SmallChange and LargeChange properties. To adjust the starting point of the scroll box, set the Value property when the control is initially displayed.

Note   The scroll box is sometimes referred to as the scroll thumb.

Example

The following example adds horizontal and vertical scroll bars to a PictureBox control, and loads an Image into the picture box on the DoubleClick event of the control. At any time you can double-click the picture box and load a new image. If the image is larger than the control, the scroll bars will be displayed so you can scroll to view the remainder of the image. The example assumes that a PictureBox, with a VScrollBar and HScrollBar docked to two of its edges, has been created on a Form. It also assumes a refernce to the the System.Drawing namespace has been added. Make sure the HandleScroll method is set as the Scroll event handler delegate for both the VScrollBar and HScrollBar. For additional code that can extend this example, see the LargeChange, SmallChange, Maximum, Minimum or Value members. You might consider setting the properties of your scroll bar based on the properties of its parent control. For instance, you can add a method to the example that sets the Maximum value equal to the Height or Width of the Image assigned to the PictureBox. Set the LargeChange value equal to the height or width of the PictureBox (with the height or width of the scroll bar subtracted). This prevents the user from scrolling past the edge of the image, and the LargeChange will only move the image's viewable area the same distance as the area displayed in the picture box.

[Visual Basic] 
Private Sub pictureBox1_DoubleClick(sender As [Object], e As EventArgs) _
  Handles pictureBox1.DoubleClick
   ' Open the dialog box so the user can select a new image.
   If openFileDialog1.ShowDialog() <> DialogResult.Cancel Then
      ' Display the image in the PictureBox.
      pictureBox1.Image = Image.FromFile(openFileDialog1.FileName)
      Me.DisplayScrollBars()
      Me.SetScrollBarValues()
   End If
End Sub      
      
Private Sub Form1_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Resize
   ' If the PictureBox has an image, see if it needs
   ' scrollbars and refresh the image. 
   If Not (pictureBox1.Image Is Nothing) Then
      Me.DisplayScrollBars()
      Me.SetScrollBarValues()
      Me.Refresh()
   End If
End Sub 

Public Sub DisplayScrollBars()
   ' If the image is wider than the PictureBox, show the HScrollBar.
   If pictureBox1.Width > pictureBox1.Image.Width - _
     Me.vScrollBar1.Width Then
      hScrollBar1.Visible = False
   Else
      hScrollBar1.Visible = True
   End If
         
   ' If the image is taller than the PictureBox, show the VScrollBar.
   If pictureBox1.Height > pictureBox1.Image.Height - _
     Me.hScrollBar1.Height Then
      vScrollBar1.Visible = False
   Else
      vScrollBar1.Visible = True
   End If
End Sub
      
      
Private Sub HandleScroll(sender As [Object], se 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()
   
   g.DrawImage(pictureBox1.Image, New Rectangle(0, 0, pictureBox1.Right - vScrollBar1.Width, _
     pictureBox1.Bottom - hScrollBar1.Height), _
     New Rectangle(hScrollBar1.Value, vScrollBar1.Value, pictureBox1.Right - vScrollBar1.Width, _
     pictureBox1.Bottom - hScrollBar1.Height), GraphicsUnit.Pixel)
         
   pictureBox1.Update()
End Sub
      
      
Public Sub SetScrollBarValues()
   ' Set the Maximum, Minimum, LargeChange and SmallChange properties.
   Me.vScrollBar1.Minimum = 0
   Me.hScrollBar1.Minimum = 0
   
   ' If the offset does not make the Maximum less than zero, set its value.
   If Me.pictureBox1.Image.Size.Width - pictureBox1.ClientSize.Width > 0 Then
      Me.hScrollBar1.Maximum = Me.pictureBox1.Image.Size.Width - _
        pictureBox1.ClientSize.Width
   End If
   ' If the VScrollBar is visible, adjust the Maximum of the 
   ' HSCrollBar to account for the width of the VScrollBar.
   If Me.vScrollBar1.Visible Then
      Me.hScrollBar1.Maximum += Me.vScrollBar1.Width
   End If
   Me.hScrollBar1.LargeChange = Me.hScrollBar1.Maximum / 10
   Me.hScrollBar1.SmallChange = Me.hScrollBar1.Maximum / 20
   ' Adjust the Maximum value to make the raw Maximum value attainable by user interaction.
   Me.hScrollBar1.Maximum += Me.hScrollBar1.LargeChange
         
   ' If the offset does not make the Maximum less than zero, set its value.
   If Me.pictureBox1.Image.Size.Height - pictureBox1.ClientSize.Height > 0 Then
      Me.vScrollBar1.Maximum = Me.pictureBox1.Image.Size.Height - _
        pictureBox1.ClientSize.Height
   End If
   ' If the HScrollBar is visible, adjust the Maximum of the 
   ' VSCrollBar to account for the width of the HScrollBar.
   If Me.hScrollBar1.Visible Then
      Me.vScrollBar1.Maximum += Me.hScrollBar1.Height
   End If
   Me.vScrollBar1.LargeChange = Me.vScrollBar1.Maximum / 10
   Me.vScrollBar1.SmallChange = Me.vScrollBar1.Maximum / 20
   ' Adjust the Maximum value to make the raw Maximum value attainable by user interaction.
   Me.vScrollBar1.Maximum += Me.vScrollBar1.LargeChange
End Sub

[C#] 
private void pictureBox1_DoubleClick (Object sender, EventArgs e)
{
   // Open the dialog box so the user can select a new image.
   if(openFileDialog1.ShowDialog() != DialogResult.Cancel)
   {
      // Display the image in the PictureBox.
      pictureBox1.Image = Image.FromFile(openFileDialog1.FileName);
      this.DisplayScrollBars();
      this.SetScrollBarValues();
   }
}
 
protected void Form1_Resize (Object sender, EventArgs e)
{
   /* If the PictureBox has an image, see if it needs 
      scrollbars and refresh the image. */
   if(pictureBox1.Image != null)
   {
      this.DisplayScrollBars();
      this.SetScrollBarValues();
      this.Refresh();
   }
}
 
public void DisplayScrollBars()
{
   // If the image is wider than the PictureBox, show the HScrollBar.
   if (pictureBox1.Width > pictureBox1.Image.Width - this.vScrollBar1.Width)
   {
      hScrollBar1.Visible = false;
   }
   else
   {
      hScrollBar1.Visible = true;
   }
 
   // If the image is taller than the PictureBox, show the VScrollBar.
   if (pictureBox1.Height > pictureBox1.Image.Height - this.hScrollBar1.Height)
   {
      vScrollBar1.Visible = false;
   }
   else
   {
      vScrollBar1.Visible = true;
   }
}
 
private void HandleScroll(Object sender, ScrollEventArgs se)
{
   /* Create a graphics object and draw a portion 
      of the image in the PictureBox. */
   Graphics g = pictureBox1.CreateGraphics();
   
   g.DrawImage(pictureBox1.Image, 
     new Rectangle(0, 0, pictureBox1.Right - vScrollBar1.Width, 
     pictureBox1.Bottom - hScrollBar1.Height), 
     new Rectangle(hScrollBar1.Value, vScrollBar1.Value, 
     pictureBox1.Right - vScrollBar1.Width,
     pictureBox1.Bottom - hScrollBar1.Height), 
     GraphicsUnit.Pixel);

     pictureBox1.Update();
}

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

[C++] 
private:
    void pictureBox1_DoubleClick (Object *sender, EventArgs *e)    {
       // Open the dialog box so the user can select a new image.
       if (openFileDialog1->ShowDialog() != DialogResult::Cancel) {
       // Display the image in the PictureBox.
       pictureBox1->Image = System::Drawing::Image::FromFile(openFileDialog1->FileName);
       this->DisplayScrollBars();
           this->SetScrollBarValues();
       }
    };

    void Form1_Resize (Object *sender, EventArgs *e) {
           /* If the PictureBox has an image, see if it needs
          scrollbars and refresh the image. */
       if (pictureBox1->Image != null) {
          this->SetScrollBarValues();
          this->DisplayScrollBars();
          this->Refresh();
       }
    };

    void DisplayScrollBars() {
           // If the image is wider than the PictureBox, show the HScrollBar.
       if (pictureBox1->Width > pictureBox1->Image->Width - this->vScrollBar1->Width) {
          hScrollBar1->Visible = false;
       } else     {
          hScrollBar1->Visible = true;
       }

           // If the image is taller than the PictureBox, show the VScrollBar.
       if (pictureBox1->Height > pictureBox1->Image->Height - this->hScrollBar1->Height) {
          vScrollBar1->Visible = false;
       } else {
       vScrollBar1->Visible = true;
       }
    };

    void HandleScroll(Object *sender, ScrollEventArgs *se) {
       /* Create a graphics object and draw a portion
          of the image in the PictureBox. */
       Graphics __gc *g = pictureBox1->CreateGraphics();
       g->DrawImage(pictureBox1->Image, Rectangle(0, 0, pictureBox1->Right - vScrollBar1->Width,
             pictureBox1->Bottom - hScrollBar1->Height),
             Rectangle(hScrollBar1->Value, vScrollBar1->Value, pictureBox1->Right - vScrollBar1->Width,
             pictureBox1->Bottom - hScrollBar1->Height), GraphicsUnit::Pixel);
    };



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

[JScript] 
private function pictureBox1_DoubleClick (sender : Object, 
                                            e : EventArgs)
 {
    // Open the dialog so the user can select a new image.
    if(openFileDialog1.ShowDialog() != System.Windows.Forms.DialogResult.Cancel)
    {
       // Display the image in the PictureBox.
       pictureBox1.Image = Image.FromFile(openFileDialog1.FileName);
       this.DisplayScrollBars();
       this.SetScrollBarValues();
    }
 }
 
 private function Form1_Resize (sender : Object, 
                                  e : EventArgs)
 {
    /* If the PictureBox has an image, see if it needs 
       scrollbars and refresh the image. */
    if(pictureBox1.Image != null)
    {
       this.SetScrollBarValues();
       this.DisplayScrollBars();
       this.Refresh();
    }
 }
 
 public function DisplayScrollBars()
 {
    // If the image is wider than the PictureBox, show the HScrollBar.
    if (pictureBox1.Width > pictureBox1.Image.Width - this.vScrollBar1.Width)
    {
       hScrollBar1.Visible = false;
    }
    else
    {
       hScrollBar1.Visible = true;
    }
 
    // If the image is taller than the PictureBox, show the VScrollBar.
       if (pictureBox1.Height > pictureBox1.Image.Height - this.hScrollBar1.Height)
    {
       vScrollBar1.Visible = false;
    }
    else
    {
       vScrollBar1.Visible = true;
    }
 }
 
 private function HandleScroll(sender : Object, 
                              se : ScrollEventArgs)
 {
   /* Create a graphics object and draw a portion 
      of the image in the PictureBox. */
   Graphics g = pictureBox1.CreateGraphics();
   
   g.DrawImage(pictureBox1.Image, 
     new Rectangle(0, 0, pictureBox1.Right - vScrollBar1.Width, 
     pictureBox1.Bottom - hScrollBar1.Height), 
     new Rectangle(hScrollBar1.Value, vScrollBar1.Value, 
     pictureBox1.Right - vScrollBar1.Width,
     pictureBox1.Bottom - hScrollBar1.Height), 
     GraphicsUnit.Pixel);

     pictureBox1.Update();
 }



public function 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;
 }

Requirements

Namespace: System.Windows.Forms

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework

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

See Also

ScrollBar Members | System.Windows.Forms Namespace | VScrollBar | HScrollBar

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.