An HScrollBar control is a supporting control that is used to add horizontal 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 an HScrollBar control and apply horizontal scrolling on Windows Forms controls that do not have built-in scrolling feature. After that, I will discuss various properties and methods available for the HScrollBar control.
Creating an HScrollBar
We can create an HScrollBar control using a Forms designer at design-time or using the HScrollBar class in code at run-time (also known as dynamically).
To create an HScrollBar control at design-time, you simply drag and drop an HScrollBar control from Toolbox to a Form in Visual Studio. After you drag and drop an HScrollBar on a Form, the HScrollBar looks like Figure 1. Once an HScrollBar is on the Form, you can move it around and resize it using the mouse and set its properties and events.
Creating an HScrollBar control at run-time is merely a work of creating an instance of HScrollBar class, setting its properties and adding HScrollBar class to the Form controls.
The first step to create a dynamic HScrollBar is to create an instance of HScrollBar class. The following code snippet creates an HScrollBar control object.
- HScrollBar hScroller = newHScrollBar();
In the next step, you may set properties of an HScrollBar control. The following code snippet sets the height and width properties of an HScrollBar.
- hScroller.Height = 40;
- hScroller.Width = 300;
Once an HScrollBar control is ready with its properties, the next step is to add the HScrollBar control to the Form. To do so, we use Form.Controls.Add method. The following code snippet adds an HScrollBar control to the current Form.
- this.Controls.Add(hScroller);
Setting HScrollBar Properties
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.
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.
- authorGroup.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.
- hScroller.Scroll += new System.Windows.Forms.ScrollEventHandler(hScroller_Scroll);
The following code snippet is used to redraw the image on a PictureBox based on the value of the HScrollBar.
- privatevoid hScroller_Scroll(object sender, ScrollEventArgs e) {
- Graphics g = pictureBox1.CreateGraphics();
- g.DrawImage(pictureBox1.Image, newRectangle(0, 0, pictureBox1.Height, hScroller.Value));
- }
Summary
An HScrollBar control is used to add horizontal scrolling feature to a Windows Forms control that do not have built-in scrolling feature. In this article, we discussed how to create an HScrollBar control in Windows Forms at design-time as well as run-time. After that, we saw how to use various properties and methods.