After you place an HScrollBar 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 click on a control and select Properties menu item. The Properties window looks like Figure 2.
Figure 2
Location, Height, Width, and Size
The Location property takes a Point that specifies the starting position of the HScrollBar 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 an HScrollBar control.
hScroller.Dock = DockStyle.Bottom
hScroller.Width = 250
hScroller.Height = 200
Name
Name property represents a unique name of an HScrollBar control. It is used to access the control in the code. The following code snippet sets and gets the name and text of an HScrollBar control.
hScroller.Name = "HScrollBar1"
Maximum, Minimum, and Value
Value property of an HScrollBar represents the current value of an HScrollBar control. The Minimum and Maximum properties are used to set minimum and maximum limit of an HScrollBar. The following code snippet sets the Minimum, Maximum, and Value properties of an HScrollBar control.
hScroller.Minimum = 0
hScroller.Maximum = 100
hScroller.Value = 40
Attaching HScrollBar 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 an HScrollBar and based on this value, we apply 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.
Friend WithEvents hScroller As New HScrollBar
The following code snippet is used to redraw the image on a PictureBox based on the value of the HScrollBar.
Private Sub hScroller_Scroll(ByVal sender As System.Object, _
ByVal e As System.Windows.Forms.ScrollEventArgs) _
Handles hScroller.Scroll
Dim g As Graphics = PictureBox1.CreateGraphics()
g.DrawImage(PictureBox1.Image,
New Rectangle(0, 0, PictureBox1.Height, hScroller.Value))
End Sub