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

ScrollBar Class

Implements the basic functionality of a scroll bar control.

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

[ClassInterfaceAttribute(ClassInterfaceType.AutoDispatch)] 
[ComVisibleAttribute(true)] 
public abstract class ScrollBar : Control
/** @attribute ClassInterfaceAttribute(ClassInterfaceType.AutoDispatch) */ 
/** @attribute ComVisibleAttribute(true) */ 
public abstract class ScrollBar extends Control
ClassInterfaceAttribute(ClassInterfaceType.AutoDispatch) 
ComVisibleAttribute(true) 
public abstract class ScrollBar extends Control
Not applicable.

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

NoteNote:

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

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.

User interface guidelines suggest that the SmallChange and LargeChange properties are set relative to the size of the view that the user sees, not to the total size including the unseen part. For example, if you have a picture box with scroll bars displaying a large image, the SmallChange and LargeChange properties should be set relative to the size of the picture box, not to the size of the image.

The following example creates a Form, adds a PictureBox to the Form, and adds a horizontal HScrollBar and a vertical VScrollBar to the PictureBox. When you double-click the PictureBox you can browse to an Image to load in to the PictureBox.

NoteNote:

You can double-click the picture box and load a new image at any time.

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 HandleScroll method is set as the Scroll event handler delegate for both the VScrollBar and HScrollBar.

You must add references to the System.Drawing and System.Windows.Forms namespaces to run this example.

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.

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;


namespace Scrollbar
{
    public class Form1 :  Form
   {
      private  OpenFileDialog openFileDialog1;
      private  PictureBox pictureBox1;
      private  VScrollBar vScrollBar1;
      private  HScrollBar hScrollBar1;
    
      public Form1()
      {
         InitializeComponent();
      }
    
      private void InitializeComponent()
      {
         this.openFileDialog1 = new OpenFileDialog();
         this.pictureBox1 = new PictureBox();
         this.vScrollBar1 = new VScrollBar();
         this.hScrollBar1 = new HScrollBar();
         this.pictureBox1.SuspendLayout();
         this.SuspendLayout();
        
         this.pictureBox1.BorderStyle = BorderStyle.FixedSingle;
         this.pictureBox1.Controls.AddRange(
             new Control[] { this.vScrollBar1, this.hScrollBar1});
         this.pictureBox1.Dock = DockStyle.Fill;
         this.pictureBox1.Size = new System.Drawing.Size(440, 349);
         this.pictureBox1.DoubleClick += new EventHandler(this.pictureBox1_DoubleClick);

         this.vScrollBar1.Dock = DockStyle.Right;
         this.vScrollBar1.Location = new System.Drawing.Point(422, 0);
         this.vScrollBar1.Size = new System.Drawing.Size(16, 331);
         this.vScrollBar1.Visible = false;
         this.vScrollBar1.Scroll += 
             new ScrollEventHandler(this.HandleScroll);
         
         this.hScrollBar1.Dock = DockStyle.Bottom;
         this.hScrollBar1.Location = new System.Drawing.Point(0, 331);
         this.hScrollBar1.Size = new System.Drawing.Size(438, 16);
         this.hScrollBar1.Visible = false;
         this.hScrollBar1.Scroll += 
             new ScrollEventHandler(this.HandleScroll);
         
         this.ClientSize = new System.Drawing.Size(440, 349);
         this.Controls.AddRange(new Control[] {this.pictureBox1});
         this.Text = "Form1";
         this.Resize += new System.EventHandler(this.Form1_Resize);
         this.pictureBox1.ResumeLayout(false);
         this.ResumeLayout(false);

      }

      [STAThread]
      static void Main() 
      {
         Application.Run(new Form1());
      }

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

  }
}

System.Object
   System.MarshalByRefObject
     System.ComponentModel.Component
       System.Windows.Forms.Control
        System.Windows.Forms.ScrollBar
           System.Windows.Forms.HScrollBar
           System.Windows.Forms.VScrollBar
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 98, Windows Server 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 Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.