|
|
Title | Make a monitor that displays a value using a vertical gauge in VB .NET |
Description | This example shows how to make a vertical bar 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. |
Keywords | gauge, bar, monitor, VB.NET |
Categories | Graphics, 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 = _
SystemInformation.WorkingArea
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 _
tmrChangeStats.Tick
Const DTHETA As Single = PI / 20
Static theta As Single
m_Value = _
Sin(theta) * 25 + _
Cos(2 * theta) * 15 + _
50
theta = theta + DTHETA
Me.Refresh()
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 fills in the form from the bottom up to the position represented by m_Value. The transformation makes drawing the bar easier.
|
|
Private Sub Form1_Paint(ByVal sender As Object, ByVal e As _
System.Windows.Forms.PaintEventArgs) Handles _
MyBase.Paint
' 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, _
Drawing2D.MatrixOrder.Append)
' Draw the value.
e.Graphics.FillRectangle(Brushes.Blue, 0, 0, 10, _
CInt(m_Value))
End Sub
|
|
|
|
|
|