Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / VB

ScrollBar Control with Different Visual Style using VB.NET 2008

5.00/5 (4 votes)
9 Mar 2012CPOL1 min read 27.2K   2K  
Why another Scroll Bar? The standard ScrollBar is too limited in functionality and I couldn't find a custom control written that did all that I wanted. This is a User Control with Microsoft Office 2007 Visual Style.

Introduction

Why another Scroll Bar? The standard ScrollBar is too limited in functionality and I couldn't find a custom control written that did all that I wanted. This is a User Control with Microsoft Office 2007 Visual Style. It is simple to use, just drop it on the form, and use it like the normal ScrollBar.

Background

MBScrollBar is a ContextMenuStrip which inherits all the properties of simple Scroll Bar control. I added Microsoft Office 2007 like Visuals in MBScrollBar, also it supports mouse wheel. The language used is VB.NET. There are so many classes which provides same functionality, but for that we have to write minimum two lines of code to add that renderer class in our application. MBScrollBar is the Scroll Bar which already contains MBScrollBarRenderer Class You Just add the reference of MBScrollBar.Dll and used it by Dragging and Dropping.

MBScrollBar Control

Code

The concept for this Scroll Bar came from the Microsoft Office 2007 Scroll Bar. I organized methods of MBScrollBar into layers like this:

Following methods which are responsible for rendering simple Scroll Bar like Microsoft office

This method Render Background of MBScrollBar item:

VB
''' <summary>
    ''' Draw Background for MBScrollBar
    ''' </summary>
    ''' <param name="g">graphics As Graphics</param>
    ''' <param name="rect">Rect As Rectangle</param>
    ''' <param name="orientation">Orientation As MBScrollBarOrientation</param>
    Public Shared Sub DrawBackground(ByVal g As Graphics, ByVal rect As Rectangle, ByVal orientation As MBScrollBarOrientation)
        If (g Is Nothing) Then
            Throw New ArgumentNullException("g")
        End If
        If (rect.IsEmpty Or g.IsVisibleClipEmpty Or Not g.VisibleClipBounds.IntersectsWith(rect)) Then
            Return
        End If
        If (orientation = MBScrollBarOrientation.Vertical) Then
            DrawBackgroundVertical(g, rect)
        Else
            DrawBackgroundHorizontal(g, rect)
        End If   
 End Sub

This method Render Arrow Button of MBScrollBar Item:

VB
''' <summary>
''' Draw Arrow Button For MBScrollBar
''' </summary>
''' <param name="g">graphics As Graphics</param>
''' <param name="rect">Rect As Rectangle</param>
''' <param name="state">State As MBScrollBarState</param>
''' <param name="arrowUp">ArrowUp As Boolean</param>
''' <param name="orientation">Orientation As MBScrollBarOrientation</param>
Public Shared Sub DrawArrowButton(ByVal g As Graphics, ByVal rect As Rectangle, 
    ByVal state As MBScrollBarArrowButtonState, ByVal arrowUp As Boolean, 
    ByVal orientation As MBScrollBarOrientation)
        If g Is Nothing Then
            Throw New ArgumentNullException("g")
        End If
        If rect.IsEmpty Or g.IsVisibleClipEmpty Or (
            Not g.VisibleClipBounds.IntersectsWith(rect)) Then
            Return
        End If
        If orientation = MBScrollBarOrientation.Vertical Then
            DrawArrowButtonVertical(g, rect, state, arrowUp)
        Else
            DrawArrowButtonHorizontal(g, rect, state, arrowUp)
        End If
    End Sub

This method Handles painting of MBScrollBar:

VB
''' <summary>
''' Handles MBScrollBar Mouse Wheel
''' </summary>
Protected Overrides Sub OnMouseWheel(ByVal e As System.Windows.Forms.MouseEventArgs)
        MyBase.OnMouseWheel(e)
        If (Me._Value < Me.Minimum Or Me._Value > Me.Maximum) Then
            Return
        Else
            If e.Delta > 0 Then
                Me._Value = Me._Value - 1
            Else
                Me._Value = Me._Value + 1
            End If
            If Me._Value > 100 Then Me._Value = 100
            If Me._Value < 0 Then Me._Value = 0
        End If
        Me.ChangeThumbPosition(Me.GetThumbPosition())
        Me.Refresh()
    End Sub

Using the Code

It is very easy to use the MBScrollBar in your application. Just add the reference of the provided DLL to your application and just drag and drop.

History

  • MBScrollBar Version 1.0.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)