Blue Theme Orange Theme Green Theme Red Theme
 
Nevron Chart
Home | Forums | Videos | Advertise | Certifications | Downloads | Blogs | Interviews | Jobs | Beginners | Training
 | Consulting  
Submit an Article Submit a Blog 
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
Mindcracker MVP Summit 2012
Search :       Advanced Search »
Home » Windows Controls C# » Horizontal ScrollBar in C#

Horizontal ScrollBar in C#

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.

Author Rank :
Page Views : 15910
Downloads : 0
Rating :
 Rate it
Level : Beginner
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
 
Discover the top 5 tips for understanding .NET Interop
Become a Sponsor
 Tag Cloud
 Latest Jobs
More ... 
 Latest Interview Questions
More ... 



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 mouse and set its properties and events.

HSCBarImg1.jpg
Figure 1

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

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 = new HScrollBar();

 

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, 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.

HSCBarImg2.jpg
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.

private void hScroller_Scroll(object sender, ScrollEventArgs e)

{

    Graphics g = pictureBox1.CreateGraphics();

    g.DrawImage(pictureBox1.Image,

        new Rectangle(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 discuss 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.

Comment Request!
Thank you for reading this post. Please post your feedback, question, or comments about this post Here.
Login to add your contents and source code to this article
 [Top] Rate this article
 
 About the author
 
Mahesh Chand
Mahesh is the founder of C# Corner and Mindcracker Network, an author of several .NET programming books and a Microsoft MVP for 6 consecutive years. In his day to day work, Mahesh is a Senior Software Consultant with over 14 years of IT industry experience building systems for Financial and Banking, Engineering & Architectural, Imaging, Construction, Biological & Pharmaceuticals, Healthcare and Education industries. His expertise is Windows Forms, ASP.NET, Silverlight, WPF, WCF, Visual Studio 2010, SQL Server, and Oracle.  If you are looking for a Sharepoint, Windows Forms, ASP.NET, WPF, Silverlight, C#, VB.NET, Oracle, and SQL Server Consultant in Philadelphia area or remote location, drop me a line at MAHESH [AT] C-SHARPCORNER [DOT] COM.
Looking for C# Consulting?
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional consulting company, our consultants are well-known experts in .NET and many of them are MVPs, authors, and trainers. We specialize in Microsoft .NET development and utilize Agile Development and Extreme Programming practices to provide fast pace quick turnaround results. Our software development model is a mix of Agile Development, traditional SDLC, and Waterfall models.
Click here to learn more about C# Consulting.
 
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Discover the top 5 tips for understanding .NET
Ricky Leeks presents the top 5 tips for understanding .NET Interoperability. Learn more.
Nevron Chart for .NET 2010.1 Now Available
The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
ASP.NET 4 Hosting
Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites – Click Here!
 
 Post a Feedback, Comment, or Question about this article
Subject:
Comment:
Nevron Chart
Become a Sponsor
 Comments
hey by Asbath On February 2, 2012
How can i show the value from the horizontal scroll bar?? because im using it to adjust the size of characters thanks IGNORE IT I FIGURED IT OUT LOL
Reply | Email | Modify 

 © 2012  contents copyright of their authors. Rest everything copyright Mindcracker. All rights reserved.