Vertical ScrollBar In C#

A VScrollBar control is a supporting control that is used to add vertical scrolling capability to a control that does not have built-in scrolling such as a container control. You do not need this control for the controls that already have built-in scrolling.

In this article, I will discuss how to create a VScrollBar control and apply vertical scrolling on Windows Forms controls that do not have a built-in scrolling feature. After that, I will discuss various properties and methods available for the VScrollBar control.

Creating a VScrollBar

We can create a VScrollBar control using a Forms designer at design-time or using the VScrollBar class in code at run-time (also known as dynamically).

To create a VScrollBar control at design-time, you simply drag and drop a VScrollBar control from Toolbox to a Form in Visual Studio. After you drag and drop a VScrollBar on a Form, the VScrollBar looks like Figure 1. Once a VScrollBar is on the Form, you can move it around and resize it using the mouse and set its properties and events.

VScrollBar
Figure 1

Creating a VScrollBar control at run-time is merely a work of creating an instance of VScrollBar class, setting its properties and adding VScrollBar class to the Form controls.

The first step to create a dynamic VScrollBar is to create an instance of VScrollBar class. The following code snippet creates a VScrollBar control object.
  1. VScrollBar vScroller = newVScrollBar();  
In the next step, you may set properties of a VScrollBar control. The following code snippet sets the height and width properties of a VScrollBar.
  1. vScroller.Height = 200;  
  2. vScroller.Width = 30;  
Once a VScrollBar control is ready with its properties, the next step is to add the VScrollBar control to the Form. To do so, we use Form.Controls.Add method. The following code snippet adds a VScrollBar control to the current Form.
  1. this.Controls.Add(vScroller);  
Setting VScrollBar Properties

After you place a VScrollBar control on a Form, the next step is to set properties.

The easiest way to set properties is from the Properties Window. You can open Properties window by pressing F4 or right clicking on a control and selecting Properties menu item. The Properties window looks like Figure 2.

VScrollBar Properties
Figure 2

Location, Height, Width, and Size

The Location property takes a point that specifies the starting position of the VScrollBar on a Form. The Size property specifies the size of the control. We can also use Width and Height property instead of Size property. The Dock property is used to dock a control. The following code snippet sets Dock, Width, and Height properties of a VScrollBar control.
  1. vScroller.Dock = DockStyle.Right;  
  2. vScroller.Width = 30;  
  3. vScroller.Height = 200;  
Name

Name property represents a unique name of a VScrollBar control. It is used to access the control in the code. The following code snippet sets and gets the name and text of a VScrollBar control.
  1. authorGroup.Name = "VScrollBar1";  
Maximum, Minimum, and Value

Value property of a VScrollBar represents the current value of a VScrollBar control. The Minimum and Maximum properties are used to set minimum and maximum limit of a VScrollBar. The following code snippet sets the Minimum, Maximum, and Value properties of a VScrollBar control.
  1. vScroller.Minimum = 0;  
  2. vScroller.Maximum = 100;  
  3. vScroller.Value = 40;  
Attaching VScrollBar to a Control

Horizontal scroll bar control is attached to a control by its scroll event. On the scroll event hander, we usually read the current value of a VScrollBar and based on this value, we apply it on other controls. For example, we can implement scrolling in a PictureBox control by displaying image in a PictureBox again on the scroll event bar.

The following code snippet adds an event handler for the Scroll event.
  1. vScroller.Scroll += new System.Windows.Forms.ScrollEventHandler(vScroller_Scroll);  
The following code snippet is used to redraw the image on a PictureBox based on the value of the VScrollBar.
  1. privatevoid vScroller_Scroll(object sender, ScrollEventArgs e) {  
  2.     Graphics g = pictureBox1.CreateGraphics();  
  3.     g.DrawImage(pictureBox1.Image, newRectangle(0, 0, pictureBox1.Height, vScroller.Value));  
  4. }  
Summary 

A VScrollBar control is used to add vertical scrolling feature to a Windows Forms control that does not have built-in scrolling feature. In this article, we discussed how to create a VScrollBar control in Windows Forms at design-time as well as run-time. After that, we saw how to use various properties and methods.


Mindcracker
Founded in 2003, Mindcracker is the authority in custom software development and innovation. We put best practices into action. We deliver solutions based on consumer and industry analysis.