What's New
Q & A
Tip Jar
C# Helper...
Follow VBHelper on Twitter
MSDN Visual Basic Community
TitleMake a monitor that displays a value using a vertical bar gauge in VB .NET
DescriptionThis example shows how to make a vertical bar gauge that monitors a value in Visual Basic .NET. For example, you could use this to monitor network bandwidth, CPU usage, or some other value that changes over time. This example shows how to move the form to the screen's lower-right corner, how to give the form a convenient scale, and how to update the display when a timer fires.
Keywordsgauge, bar, bar gauge, monitor, VB.NET
CategoriesGraphics, VB.NET
Subroutine LowerRight moves the form to the screen's lower right corner. It uses the SystemInformation object's WorkingArea property to see where the work area is and moves the form.
' Move the form to the lower right corner.
Private Sub LowerRight()
    Const GAP As Integer = 4

    Dim working_area As Rectangle = _
    Dim x As Integer = _
        working_area.Left + working_area.Width - _
        Me.Width - GAP
    Dim y As Integer = _
        working_area.Top + working_area.Height - _
        Me.Height - GAP
    Me.Location = New Point(x, y)
End Sub
When the tmrChangeStats timer fires its Tick event, the program changes the value of the m_Value variable and then calls the form's Refresh method to make it redraw itself.
Private m_Value As Double = 50

Private Sub tmrChangeStats_Tick(ByVal sender As _
    System.Object, ByVal e As System.EventArgs) Handles _
    Const DTHETA As Single = PI / 20
    Static theta As Single

    m_Value = _
        Sin(theta) * 25 + _
        Cos(2 * theta) * 15 + _
    theta = theta + DTHETA

End Sub
The Paint event handler first transforms its Graphics object so the point (0, 0) is in the form's lower left corner and the point (10, 100) is in the lower right. You can change the values 10 and 100 to make things convenient for your program.

Next the code loops over rectangles that it might want to fill. If a rectangle's Y value is less than or equal to the program's current value m_Value, then the program fills in that rectangle.

Private Sub Form1_Paint(ByVal sender As Object, ByVal e As _
    System.Windows.Forms.PaintEventArgs) Handles _
    ' Transform so (0,0) is in the lower left corner
    ' and (10, 100) is in the upper right.
    e.Graphics.ScaleTransform( _
        CSng(Me.ClientSize.Width / 10), _
        -CSng(Me.ClientSize.Height / 100))
    e.Graphics.TranslateTransform( _
        0, Me.ClientSize.Height, _

    ' Draw the value.
    Const BAR_HGT As Integer = 2
    For i As Integer = 0 To 100 Step BAR_HGT * 2
        If i <= m_Value Then
            e.Graphics.FillRectangle(Brushes.Blue, 0, i, _
                10, BAR_HGT)
        End If
    Next i
End Sub
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.