Imports System.Drawing
Imports System.Windows.Forms
Module Exercise
Public Class Starter
Inherits Form
Private tmr As Timer
Dim components As System.ComponentModel.Container
Public Sub New()
InitializeComponent()
End Sub
Public Sub InitializeComponent()
tmr = New Timer
End Sub
End Class
Function Main() As Integer
Dim frmStart As Starter = New Starter
Application.Run(frmStart)
Return 0
End Function
End Module
Notice that we did not have to add it to the Controls
collection of the form.
|
Practical
Learning: Introducing the Timer Control
|
|
- Start a new Windows Application named ScreenSaver1
- Change the following properties of the form:
BackColor: Black
FormBorderStyle: None
WindowState: Maximized
- Right-click the form and click View Code
- Declare a static integer variable named MoveCounter
Public Class Form1
Private Shared MoveCounter As Integer
End Class
|
- In the Class Name combo box, select (Form1 Events)
- In the Method Name combo box, select MouseMove and implement the event as follows:
Public Class Form1
Private Shared MoveCounter As Integer
Private Sub Form1_MouseMove(ByVal sender As Object, _
ByVal e As System.Windows.Forms.MouseEventArgs) _
Handles Me.MouseMove
Cursor.Hide()
If MoveCounter = 20 Then Close()
MoveCounter = MoveCounter + 1
End Sub
End Class
|
- In the Method Name combo box, select KeyDown and implement the event as follows:
Private Sub Form1_KeyDown(ByVal sender As Object, _
ByVal e As System.Windows.Forms.KeyEventArgs) _
Handles Me.KeyDown
' If the user presses Esc, stop the screen saver
If e.KeyCode = Keys.Escape Then Close()
End Sub
|
- Execute the application to test it (to close the form, you will just move
the mouse or press Esc)
- Display the form
|
Characteristics of a Timer |
|
A timer is an object used to count lapses of time and send a message when it has finished counting.
Each count is called a tick. When a tick occurs, the control fires a Tick
event. This Tick event is of type EventArgs, meaning that it
doesn't provide more information than to let you know that a lapse has occurred.
Here is an example of implementing it:
Imports System.Drawing
Imports System.Windows.Forms
Module Exercise
Public Class Starter
Inherits Form
Friend WithEvents tmr As Timer
Dim components As System.ComponentModel.Container
Public Sub New()
InitializeComponent()
End Sub
Public Sub InitializeComponent()
tmr = New Timer
End Sub
Private Sub WasTicked(ByVal sender As Object, _
ByVal e As EventArgs) Handles tmr.Tick
Text = CStr(DateTime.Now)
End Sub
End Class
Function Main() As Integer
Dim frmStart As Starter = New Starter
Application.Run(frmStart)
Return 0
End Function
End Module
The amount of time allocated for counting is called an
interval and it is represented by the Interval property. The Interval is
a very important characteristic of the timer control because it measures and controls the total time needed to perform a complete count. The
Interval is measured in milliseconds. Like any counter, the lower the value, the faster the count will finish, and the higher the value, the longer the count (if you ask one kid to count from 1 to 10 and you ask another to count from 1 to 20
at the same time, if you ask them to shout when they finish, the first kid would finish first and would shout first). The amount of interval you specify will depend on what you are trying to do.
Here is an example of setting this property programmatically:
Public Sub InitializeComponent()
tmr = New Timer
tmr.Interval = 200
End Sub
In order for a timer to count, you must tell it when it should start counting. In some applications, you may want the control to work full-time while in some other applications, you may want the control to work only in response to an intermediate event. The ability to stop and start a Timer control
can be set using the Enabled Boolean property. Here is an example of
setting this property programmatically:
Public Sub InitializeComponent()
tmr = New Timer
tmr.Enabled = True
tmr.Interval = 200
End Sub
When, or as soon as, this property is set to true, the control starts counting.
You can also make it start by calling the Timer.Start() method. Its
syntax is:
Public Sub Start
If, when, or as soon as, the Enabled property is set to false, the control stops and resets its counter to 0.
You can also stop the timer by calling the Timer.Stop() method. Its
syntax is:
Public Sub Stop
|
Practical
Learning: Using Timer Controls
|
|
- From the Components section of the Toolbox, click the Timer control
and click the form
- In the Properties window, set the Enabled property to True
and set the Interval to 200
- Under the form, double-click the timer
- In the top section of the file, under the other using System lines, type using System.Drawing.Drawing2D;
- Scroll down and implement the Tick event as follows:
Private Sub Timer1_Tick(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles Timer1.Tick
' Get the Graphics object of the form
Dim graph As Graphics = Graphics.FromHwnd(Handle)
' Generate a random number
Dim rndNumber As Random = New Random(DateTime.Now.Millisecond)
' Create a list of the colors of the .NET Framework
Dim curColor() As Color = _
{ _
Color.AliceBlue, Color.AntiqueWhite, Color.Aqua, _
Color.Aquamarine, Color.Azure, Color.Beige, _
Color.Bisque, Color.Black, Color.BlanchedAlmond, _
Color.Blue, Color.BlueViolet, Color.Brown, _
Color.BurlyWood, Color.CadetBlue, Color.Chartreuse, _
Color.Chocolate, Color.Coral, Color.CornflowerBlue, _
Color.Cornsilk, Color.Crimson, Color.Cyan, _
Color.DarkBlue, Color.DarkCyan, Color.DarkGoldenrod, _
Color.DarkGray, Color.DarkGreen, Color.DarkKhaki, _
Color.DarkMagenta, Color.DarkOliveGreen, _
Color.DarkOrange, Color.DarkOrchid, Color.DarkRed, _
Color.DarkSalmon, Color.DarkSeaGreen, _
Color.DarkSlateBlue, Color.DarkSlateGray, _
Color.DarkTurquoise, Color.DarkViolet, Color.DeepPink, _
Color.DeepSkyBlue, Color.DimGray, Color.DodgerBlue, _
Color.Firebrick, Color.FloralWhite, Color.ForestGreen, _
Color.Fuchsia, Color.Gainsboro, Color.GhostWhite, _
Color.Gold, Color.Goldenrod, Color.Gray, _
Color.Green, Color.GreenYellow, Color.Honeydew, _
Color.HotPink, Color.IndianRed, Color.Indigo, _
Color.Ivory, Color.Khaki, Color.Lavender, _
Color.LavenderBlush, Color.LawnGreen, Color.LemonChiffon, _
Color.LightBlue, Color.LightCoral, Color.LightCyan, _
Color.LightGoldenrodYellow, Color.LightGray, _
Color.LightGreen, Color.LightPink, Color.LightSalmon, _
Color.LightSeaGreen, Color.LightSkyBlue, _
Color.LightSlateGray, Color.LightSteelBlue, _
Color.LightYellow, Color.Lime, Color.LimeGreen, _
Color.Linen, Color.Magenta, Color.Maroon, _
Color.MediumAquamarine, Color.MediumBlue, _
Color.MediumOrchid, Color.MediumPurple, _
Color.MediumSeaGreen, Color.MediumSlateBlue, _
Color.MediumSpringGreen, Color.MediumTurquoise, _
Color.MediumVioletRed, Color.MidnightBlue, _
Color.MintCream, Color.MistyRose, Color.Moccasin, _
Color.NavajoWhite, Color.Navy, Color.OldLace, _
Color.Olive, Color.OliveDrab, Color.Orange, _
Color.OrangeRed, Color.Orchid, Color.PaleGoldenrod, _
Color.PaleGreen, Color.PaleTurquoise, _
Color.PaleVioletRed, Color.PapayaWhip, _
Color.PeachPuff, Color.Peru, Color.Pink, Color.Plum, _
Color.PowderBlue, Color.Purple, Color.Red, _
Color.RosyBrown, Color.RoyalBlue, Color.SaddleBrown, _
Color.Salmon, Color.SandyBrown, Color.SeaGreen, _
Color.SeaShell, Color.Sienna, Color.Silver, _
Color.SkyBlue, Color.SlateBlue, Color.SlateGray, _
Color.Snow, Color.SpringGreen, Color.SteelBlue, _
Color.Tan, Color.Teal, Color.Thistle, Color.Tomato, _
Color.Transparent, Color.Turquoise, Color.Violet, _
Color.Wheat, Color.White, Color.WhiteSmoke, _
Color.Yellow, Color.YellowGreen _
}
' Create a list of 10 rectangles for each row
Dim row1(10) As Rectangle ' = New Rectangle
Dim row2(10) As Rectangle ' = New Rectangle
Dim row3(10) As Rectangle ' = New Rectangle
Dim row4(10) As Rectangle ' = New Rectangle
Dim row5(10) As Rectangle ' = New Rectangle
Dim row6(10) As Rectangle ' = New Rectangle
Dim row7(10) As Rectangle ' = New Rectangle
Dim row8(10) As Rectangle ' = New Rectangle
' Create the rectangles that will be drawn on the screen
For i As Integer = 0 To 9
row1(i) = New Rectangle(i + (i * (Width / 10)), 0, _
(Width - 36) / 10, Height / 8)
row2(i) = New Rectangle(i + (i * (Width / 10)), _
4 + (Height / 8), (Width - 36) / 10, Height / 8)
row3(i) = New Rectangle(i + (i * (Width / 10)), _
8 + (2 * (Height / 8)), (Width - 36) / 10, Height / 8)
row4(i) = New Rectangle(i + (i * (Width / 10)), _
12 + (3 * (Height / 8)), (Width - 36) / 10, Height / 8)
row5(i) = New Rectangle(i + (i * (Width / 10)), _
16 + (4 * (Height / 8)), (Width - 36) / 10, Height / 8)
row6(i) = New Rectangle(i + (i * (Width / 10)), _
20 + (5 * (Height / 8)), (Width - 36) / 10, Height / 8)
row7(i) = New Rectangle(i + (i * (Width / 10)), _
24 + (6 * (Height / 8)), (Width - 36) / 10, Height / 8)
row8(i) = New Rectangle(i + (i * (Width / 10)), _
28 + (7 * (Height / 8)), (Width - 36) / 10, Height / 8)
Next
' Create the last rectangle of each row
Dim row1a As Rectangle = New Rectangle(9 + (9 * (Width / 10)), _
0, ((Width - 36) / 10) - 2, Height / 8)
Dim row2a As Rectangle = New Rectangle(9 + (9 * (Width / 10)), _
4 + (Height / 8), ((Width - 36) / 10) - 2, Height / 8)
Dim row3a As Rectangle = New Rectangle(9 + (9 * (Width / 10)), _
8 + (2 * (Height / 8)), ((Width - 36) / 10) - 2, Height / 8)
Dim row4a As Rectangle = New Rectangle(9 + (9 * (Width / 10)), _
12 + (3 * (Height / 8)), ((Width - 36) / 10) - 2, Height / 8)
Dim row5a As Rectangle = New Rectangle(9 + (9 * (Width / 10)), _
16 + (4 * (Height / 8)), ((Width - 36) / 10) - 2, Height / 8)
Dim row6a As Rectangle = New Rectangle(9 + (9 * (Width / 10)), _
20 + (5 * (Height / 8)), ((Width - 36) / 10) - 2, Height / 8)
Dim row7a As Rectangle = New Rectangle(9 + (9 * (Width / 10)), _
24 + (6 * (Height / 8)), ((Width - 36) / 10) - 2, Height / 8)
Dim row8a As Rectangle = New Rectangle(9 + (9 * (Width / 10)), _
28 + (7 * (Height / 8)), ((Width - 36) / 10) - 2, Height / 8)
' Create a list of the hatch brushes of the .NET Framework using random colors
Dim curBrush() As HatchBrush = _
{ _
New HatchBrush(HatchStyle.BackwardDiagonal, _
curColor(rndNumber.Next(curColor.Length)), _
curColor(rndNumber.Next(curColor.Length))), _
New HatchBrush(HatchStyle.Cross, _
curColor(rndNumber.Next(curColor.Length)), _
curColor(rndNumber.Next(curColor.Length))), _
New HatchBrush(HatchStyle.DarkDownwardDiagonal, _
curColor(rndNumber.Next(curColor.Length)), _
curColor(rndNumber.Next(curColor.Length))), _
New HatchBrush(HatchStyle.DarkHorizontal, _
curColor(rndNumber.Next(curColor.Length)), _
curColor(rndNumber.Next(curColor.Length))), _
New HatchBrush(HatchStyle.DarkUpwardDiagonal, _
curColor(rndNumber.Next(curColor.Length)), _
curColor(rndNumber.Next(curColor.Length))), _
New HatchBrush(HatchStyle.DarkVertical, _
curColor(rndNumber.Next(curColor.Length)), _
curColor(rndNumber.Next(curColor.Length))), _
New HatchBrush(HatchStyle.DashedDownwardDiagonal, _
curColor(rndNumber.Next(curColor.Length)), _
curColor(rndNumber.Next(curColor.Length))), _
New HatchBrush(HatchStyle.DashedHorizontal, _
curColor(rndNumber.Next(curColor.Length)), _
curColor(rndNumber.Next(curColor.Length))), _
New HatchBrush(HatchStyle.DashedUpwardDiagonal, _
curColor(rndNumber.Next(curColor.Length)), _
curColor(rndNumber.Next(curColor.Length))), _
New HatchBrush(HatchStyle.DashedVertical, _
curColor(rndNumber.Next(curColor.Length)), _
curColor(rndNumber.Next(curColor.Length))), _
New HatchBrush(HatchStyle.DashedVertical, _
curColor(rndNumber.Next(curColor.Length)), _
curColor(rndNumber.Next(curColor.Length))), _
New HatchBrush(HatchStyle.DiagonalBrick, _
curColor(rndNumber.Next(curColor.Length)), _
curColor(rndNumber.Next(curColor.Length))), _
New HatchBrush(HatchStyle.DiagonalCross, _
curColor(rndNumber.Next(curColor.Length)), _
curColor(rndNumber.Next(curColor.Length))), _
New HatchBrush(HatchStyle.Divot, _
curColor(rndNumber.Next(curColor.Length)), _
curColor(rndNumber.Next(curColor.Length))), _
New HatchBrush(HatchStyle.DottedDiamond, _
curColor(rndNumber.Next(curColor.Length)), _
curColor(rndNumber.Next(curColor.Length))), _
New HatchBrush(HatchStyle.DottedGrid, _
curColor(rndNumber.Next(curColor.Length)), _
curColor(rndNumber.Next(curColor.Length))), _
New HatchBrush(HatchStyle.ForwardDiagonal, _
curColor(rndNumber.Next(curColor.Length)), _
curColor(rndNumber.Next(curColor.Length))), _
New HatchBrush(HatchStyle.Horizontal, _
curColor(rndNumber.Next(curColor.Length)), _
curColor(rndNumber.Next(curColor.Length))), _
New HatchBrush(HatchStyle.HorizontalBrick, _
curColor(rndNumber.Next(curColor.Length)), _
curColor(rndNumber.Next(curColor.Length))), _
New HatchBrush(HatchStyle.LargeCheckerBoard, _
curColor(rndNumber.Next(curColor.Length)), _
curColor(rndNumber.Next(curColor.Length))), _
New HatchBrush(HatchStyle.LargeConfetti, _
curColor(rndNumber.Next(curColor.Length)), _
curColor(rndNumber.Next(curColor.Length))), _
New HatchBrush(HatchStyle.LargeGrid, _
curColor(rndNumber.Next(curColor.Length)), _
curColor(rndNumber.Next(curColor.Length))), _
New HatchBrush(HatchStyle.LightDownwardDiagonal, _
curColor(rndNumber.Next(curColor.Length)), _
curColor(rndNumber.Next(curColor.Length))), _
New HatchBrush(HatchStyle.LightHorizontal, _
curColor(rndNumber.Next(curColor.Length)), _
curColor(rndNumber.Next(curColor.Length))), _
New HatchBrush(HatchStyle.LightUpwardDiagonal, _
curColor(rndNumber.Next(curColor.Length)), _
curColor(rndNumber.Next(curColor.Length))), _
New HatchBrush(HatchStyle.LightVertical, _
curColor(rndNumber.Next(curColor.Length)), _
curColor(rndNumber.Next(curColor.Length))), _
New HatchBrush(HatchStyle.Max, _
curColor(rndNumber.Next(curColor.Length)), _
curColor(rndNumber.Next(curColor.Length))), _
New HatchBrush(HatchStyle.Min, _
curColor(rndNumber.Next(curColor.Length)), _
curColor(rndNumber.Next(curColor.Length))), _
New HatchBrush(HatchStyle.NarrowHorizontal, _
curColor(rndNumber.Next(curColor.Length)), _
curColor(rndNumber.Next(curColor.Length))), _
New HatchBrush(HatchStyle.NarrowVertical, _
curColor(rndNumber.Next(curColor.Length)), _
curColor(rndNumber.Next(curColor.Length))), _
New HatchBrush(HatchStyle.OutlinedDiamond, _
curColor(rndNumber.Next(curColor.Length)), _
curColor(rndNumber.Next(curColor.Length))), _
New HatchBrush(HatchStyle.Percent05, _
curColor(rndNumber.Next(curColor.Length)), _
curColor(rndNumber.Next(curColor.Length))), _
New HatchBrush(HatchStyle.Percent10, _
curColor(rndNumber.Next(curColor.Length)), _
curColor(rndNumber.Next(curColor.Length))), _
New HatchBrush(HatchStyle.Percent20, _
curColor(rndNumber.Next(curColor.Length)), _
curColor(rndNumber.Next(curColor.Length))), _
New HatchBrush(HatchStyle.Percent25, _
curColor(rndNumber.Next(curColor.Length)), _
curColor(rndNumber.Next(curColor.Length))), _
New HatchBrush(HatchStyle.Percent30, _
curColor(rndNumber.Next(curColor.Length)), _
curColor(rndNumber.Next(curColor.Length))), _
New HatchBrush(HatchStyle.Percent40, _
curColor(rndNumber.Next(curColor.Length)), _
curColor(rndNumber.Next(curColor.Length))), _
New HatchBrush(HatchStyle.Percent50, _
curColor(rndNumber.Next(curColor.Length)), _
curColor(rndNumber.Next(curColor.Length))), _
New HatchBrush(HatchStyle.Percent60, _
curColor(rndNumber.Next(curColor.Length)), _
curColor(rndNumber.Next(curColor.Length))), _
New HatchBrush(HatchStyle.Percent70, _
curColor(rndNumber.Next(curColor.Length)), _
curColor(rndNumber.Next(curColor.Length))), _
New HatchBrush(HatchStyle.Percent75, _
curColor(rndNumber.Next(curColor.Length)), _
curColor(rndNumber.Next(curColor.Length))), _
New HatchBrush(HatchStyle.Percent80, _
curColor(rndNumber.Next(curColor.Length)), _
curColor(rndNumber.Next(curColor.Length))), _
New HatchBrush(HatchStyle.Percent90, _
curColor(rndNumber.Next(curColor.Length)), _
curColor(rndNumber.Next(curColor.Length))), _
New HatchBrush(HatchStyle.Plaid, _
curColor(rndNumber.Next(curColor.Length)), _
curColor(rndNumber.Next(curColor.Length))), _
New HatchBrush(HatchStyle.Percent05, _
curColor(rndNumber.Next(curColor.Length)), _
curColor(rndNumber.Next(curColor.Length))) _
}
' Draw the rectangles to cover the screen
For i As Integer = 0 To 9
graph.FillRectangle(curBrush(rndNumber.Next(curBrush.Length)), row1(i))
graph.DrawRectangle(New Pen(curBrush(rndNumber.Next(curBrush.Length))), row1(i))
graph.FillRectangle(curBrush(rndNumber.Next(curBrush.Length)), row2(i))
graph.DrawRectangle(New Pen(curBrush(rndNumber.Next(curBrush.Length))), row2(i))
graph.FillRectangle(curBrush(rndNumber.Next(curBrush.Length)), row3(i))
graph.DrawRectangle(New Pen(curBrush(rndNumber.Next(curBrush.Length))), row3(i))
graph.FillRectangle(curBrush(rndNumber.Next(curBrush.Length)), row4(i))
graph.DrawRectangle(New Pen(curBrush(rndNumber.Next(curBrush.Length))), row4(i))
graph.FillRectangle(curBrush(rndNumber.Next(curBrush.Length)), row5(i))
graph.DrawRectangle(New Pen(curBrush(rndNumber.Next(curBrush.Length))), row5(i))
graph.FillRectangle(curBrush(rndNumber.Next(curBrush.Length)), row6(i))
graph.DrawRectangle(New Pen(curBrush(rndNumber.Next(curBrush.Length))), row6(i))
graph.FillRectangle(curBrush(rndNumber.Next(curBrush.Length)), row7(i))
graph.DrawRectangle(New Pen(curBrush(rndNumber.Next(curBrush.Length))), row7(i))
graph.FillRectangle(curBrush(rndNumber.Next(curBrush.Length)), row8(i))
graph.DrawRectangle(New Pen(curBrush(rndNumber.Next(curBrush.Length))), row8(i))
Next
graph.FillRectangle(curBrush(rndNumber.Next(curBrush.Length)), row1a)
graph.DrawRectangle(New Pen(curBrush(rndNumber.Next(curBrush.Length))), row1a)
graph.FillRectangle(curBrush(rndNumber.Next(curBrush.Length)), row2a)
graph.DrawRectangle(New Pen(curBrush(rndNumber.Next(curBrush.Length))), row2a)
graph.FillRectangle(curBrush(rndNumber.Next(curBrush.Length)), row3a)
graph.DrawRectangle(New Pen(curBrush(rndNumber.Next(curBrush.Length))), row3a)
graph.FillRectangle(curBrush(rndNumber.Next(curBrush.Length)), row4a)
graph.DrawRectangle(New Pen(curBrush(rndNumber.Next(curBrush.Length))), row4a)
graph.FillRectangle(curBrush(rndNumber.Next(curBrush.Length)), row5a)
graph.DrawRectangle(New Pen(curBrush(rndNumber.Next(curBrush.Length))), row5a)
graph.FillRectangle(curBrush(rndNumber.Next(curBrush.Length)), row6a)
graph.DrawRectangle(New Pen(curBrush(rndNumber.Next(curBrush.Length))), row6a)
graph.FillRectangle(curBrush(rndNumber.Next(curBrush.Length)), row7a)
graph.DrawRectangle(New Pen(curBrush(rndNumber.Next(curBrush.Length))), row7a)
graph.FillRectangle(curBrush(rndNumber.Next(curBrush.Length)), row8a)
graph.DrawRectangle(New Pen(curBrush(rndNumber.Next(curBrush.Length))), row8a)
End Sub
|
- Execute the application
The Environment class provides a special property used to count a specific number of lapses that have occurred since you started your computer. This information or counter is available through the
TickCount property. This property counts the number of milliseconds that have elapsed since you started your computer. Just like the
timer control, what you do with the result of this property is up to you and it can be used in various circumstances.
After retrieving the value that the Environment.TickCount
property, you can display it in a text-based control. Here is an example:
Imports System.Drawing
Imports System.Windows.Forms
Module Exercise
Public Class Starter
Inherits Form
Dim components As System.ComponentModel.Container
Public Sub New()
InitializeComponent()
End Sub
Public Sub InitializeComponent()
End Sub
Private Sub FormLoaded(ByVal sender As Object, _
ByVal e As EventArgs) Handles Me.Load
Text = CStr(Environment.TickCount)
End Sub
End Class
Function Main() As Integer
Dim frmStart As Starter = New Starter
Application.Run(frmStart)
Return 0
End Function
End Module
|
Practical
Learning: Counting the Computer's Ticks
|
|
- Start a new Windows Application named CompAppElapsedTime1
- Design the form as follows:

- From the Components section of the Toolbox, click the Timer control
and click the form
- Change the timer's properties as follows:
Interval:20
Enabled: True
- Right-click the form and click View Code
- Declare an integer variable named CompTime
Public Class Form1
Private CompTime As Integer
End Class
|
- In the Class Name combo box, select (Form1 Events)
- In the Method Name combo box, select Load and initialize the
variable in the Load event as follows:
Private Sub Form1_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) _
Handles Me.Load
CompTime = Environment.TickCount
End Sub
|
- In the Class Name combo box, select timer1
- In the Method Name combo box, select Tick and implement the event as follows:
Private Sub timer1_Tick(ByVal sender As Object, _
ByVal e As System.EventArgs) _
Handles timer1.Tick
Dim CurTickValue As Integer = Environment.TickCount
Dim Difference As Integer = CurTickValue - CompTime
label1.Text = "This computer has been ON for " & CStr(CurTickValue)
label2.Text = "This application has been running for " & CStr(Difference)
End Sub
|
- In the Class Name combo box, select btnClose
- In the Method Name combo box, select Click and implement the event
as follows:
Private Sub btnClose_Click(ByVal sender As Object, _
ByVal e As System.EventArgs) _
Handles btnClose.Click
End
End Sub
|
- Test the application

- After testing the application, close it
- To make the values easier to read, change the code of the OnTimer event as follows:
Private Sub timer1_Tick(ByVal sender As Object, _
ByVal e As System.EventArgs) _
Handles timer1.Tick
Dim CurrentTickValue As Integer
Dim Difference As Integer
Dim ComputerHours As Integer
Dim ComputerMinutes As Integer
Dim ComputerSeconds As Integer
Dim ApplicationHours As Integer
Dim ApplicationMinutes As Integer
Dim ApplicationSeconds As Integer
CurrentTickValue = Environment.TickCount
Difference = CurrentTickValue - CompTime
ComputerHours = (CurrentTickValue / (3600 * 999)) Mod 24
ComputerMinutes = (CurrentTickValue / (60 * 999)) Mod 60
ComputerSeconds = (CurrentTickValue / 999) Mod 60
ApplicationHours = (Difference / (3600 * 999)) Mod 24
ApplicationMinutes = (Difference / (60 * 999)) Mod 60
ApplicationSeconds = (Difference / 999) Mod 60
label1.Text = "This computer has been ON for " & _
CStr(ComputerHours) & _
" hours, " & _
CStr(ComputerMinutes) & _
" minutes, " & _
CStr(ComputerSeconds) & _
" seconds"
label2.Text = "This application has been running for " & _
CStr(ApplicationHours) & _
" hours, " & _
CStr(ApplicationMinutes) & _
" minutes " & _
CStr(ApplicationSeconds) & _
" seconds"
End Sub
|
- Execute the application to test it:
- After testing the application, close the form
|
Introduction to Progress Bars |
|
A progress bar is a control that displays (small) rectangles that are each filled with a color. These (small) rectangles are separate but adjacent each other so that, as they display, they produce a bar. To have the effect of a progress bar, not all these rectangles display at the same time. Instead, a numeric value specifies how many of these (small) rectangles can display at one time.
To support progress bars, the .NET Framework provides the ProgressBar
class, which is derived from the Control class. In the Toolbox, the progress bar
is represented by the ProgressBar control. At design time, to get a progress
bar, from the Common Controls section of the Toolbox, you can click the
ProgressBar control and click the form (or another container).
To programmatically get a progress bar, declare a variable
of type ProgressBar, use the New operator to allocate memory for
it, and add it to the Controls property of its container. Here is an example:
Imports System.Drawing
Imports System.Windows.Forms
Module Exercise
Public Class Starter
Inherits Form
Private Progress As ProgressBar
Dim components As System.ComponentModel.Container
Public Sub New()
InitializeComponent()
End Sub
Public Sub InitializeComponent()
Text = "Progressive Studies"
Size = New Size(242, 80)
Progress = New ProgressBar
Controls.Add(Progress)
End Sub
End Class
Function Main() As Integer
Dim frmStart As Starter = New Starter
Application.Run(frmStart)
Return 0
End Function
End Module
This would produce:
|
Practical
Learning: Introducing Progress Bars
|
|
- Start a new Windows Application named ProgressClock1
- Design the form as follows:
 |
| Control |
Text |
Name |
Other Properties |
| Label |
 |
Time |
|
Anchor: Right |
| Label |
 |
Hours: |
|
|
| ProgressBar |
 |
00 |
|
Anchor: Left, Right |
| Label |
 |
|
lblHours |
|
| Label |
 |
Minutes: |
|
|
| ProgressBar |
 |
|
|
Anchor: Left, Right |
| Label |
 |
00 |
lblMinutes |
Anchor: Right |
| Label |
 |
Seconds: |
|
|
| ProgressBar |
 |
|
|
Anchor: Left, Right |
| Label |
 |
00 |
lblSeconds |
Anchor: Right |
| Button |
 |
Close |
btnClose |
Anchor: Right |
| Timer |
 |
|
|
Enabled: True
Interval: 20 |
|
- Save all
|
Characteristics of a Progress Bar |
|
The progress bar shares the various characteristics of other
graphical controls (that is, the objects derived from the Control class). These
include the location, the size, the back color, the size, the cursor, the
ability to be anchored, the ability to the docked, the ability to be shown or
hidden, and the ability to be enabled or disabled, etc.
Here is an example:
Public Sub InitializeComponent()
Text = "Progressive Studies"
Size = New Size(280, 80)
Progress = New ProgressBar
Progress.Location = New Point(12, 12)
Progress.Width = 252
Controls.Add(Progress)
End Sub
This would produce:
After adding it to an application, a progress bar assumes a horizontal
position (the actual progress bar of Microsoft Windows, as implemented in Win32,
can also have a vertical orientation; the .NET Framework's version has this and
other limitations).
To show its effect, the progress bar draws its small rectangles
as a bar. These small rectangles are from a starting position to an ending
position. This means that the progress bar uses a range of values. This range is controlled by the
Minimum and the Maximum properties whose default values are 0 and 100 respectively. At design time, you can set them using the limits of an
integer. To programmatically set these values, assign the desired numbers to one
or both. Here is an example:
Public Sub InitializeComponent()
Text = "Progressive Studies"
Size = New Size(280, 80)
Progress = New ProgressBar
Progress.Location = New Point(12, 12)
Progress.Width = 252
Progress.Minimum = 0
Progress.Maximum = 255
Controls.Add(Progress)
End Sub
The small rectangles would be drawn from the left (the Minimum value) to the
right (the Maximum value) sides of the control.
|
The Value of a Progress Bar |
|
At one particular time, the most right rectangle of a progress bar is referred to as its
position and it is represented by the Value property. At design time, to set a specific position for the control, change the value of the
Value property whose default is 0. The position must always be between the
Minimum and Maximum values. At design time, if you change the Minimum
to a value higher than the Value property, the value of Value
would be increased to the new value of Minimum. If you set the value of Value
to a value lower than the Minimum, You would receive an error:
After clicking OK, the value of the Minimum would be reset
to that of the Value property. In the same way, if you set the value of Value
to a value higher than Maximum, you would receive an error.
At run time, you can assign the desired value to the
Value property. Once again, avoid specifying a value that is out of range.
Here is an example:
Public Sub InitializeComponent()
Text = "Progressive Studies"
Size = New Size(280, 80)
Progress = New ProgressBar
Progress.Location = New Point(12, 12)
Progress.Width = 252
Progress.Minimum = 0
Progress.Maximum = 255
Progress.Value = 88
Controls.Add(Progress)
End Sub
This would produce:
|
The Step Value of a Progress Bar |
|
Because a progress bar is usually meant to indicate the progress of an activity, when drawing its small rectangles, it increases its current position in order to draw the next rectangle, except if the control is reset. The number of units that the
object must increase
its value to is controlled by the Step property. By default, it is set to
10. Otherwise, you can set it to a different value of your choice. Here is an
example:
Public Sub InitializeComponent()
Text = "Progressive Studies"
Size = New Size(280, 80)
Progress = New ProgressBar
Progress.Location = New Point(12, 12)
Progress.Width = 252
Progress.Minimum = 0
Progress.Maximum = 255
Progress.Step = 12
Controls.Add(Progress)
End Sub
When the control draws one of its rectangles based on the Step
value, it calls the PerformStep(). Its syntax is:
Public Sub PerformStep
After a small rectangle has been drawn, the current value is
incremented by the value of the Step property. If you want to increase the
value of the control to a value other than that of the Step property,
you can call the Increment() method. Its syntax is:
Public Sub Increment(value As Integer)
The amount by which to increment is passed to the method.
Here is an example:
Imports System.Drawing
Imports System.Windows.Forms
Module Exercise
Public Class Starter
Inherits Form
Private Progress As ProgressBar
Friend WithEvents btnIncrement As Button
Dim components As System.ComponentModel.Container
Public Sub New()
InitializeComponent()
End Sub
Public Sub InitializeComponent()
Text = "Progressive Studies"
Size = New Size(284, 115)
Progress = New ProgressBar
Progress.Location = New Point(12, 12)
Progress.Width = 252
Progress.Minimum = 0
Progress.Maximum = 255
Progress.Value = 88
Progress.Step = 12
btnIncrement = New Button
btnIncrement.Text = "Increment"
btnIncrement.Location = New Point(12, 52)
Controls.Add(Progress)
Controls.Add(btnIncrement)
End Sub
Private Sub IncrementedClicked(ByVal sender As Object, _
ByVal e As EventArgs) _
Handles btnIncrement.Click
Progress.Increment(8)
End Sub
End Class
Function Main() As Integer
Dim frmStart As Starter = New Starter
Application.Run(frmStart)
Return 0
End Function
End Module
|
Practical
Learning: Using Progress Bars
|
|
- Change the properties of the progress bar controls as follows:
| Name |
Maximum |
Step |
| pgrHours |
23 |
1 |
| pgrMinutes |
59 |
1 |
| pgrSeconds |
59 |
1 |
- Double-click the timer control to generate its Tick event and implement it
as follows:
Private Sub timer1_Tick(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles timer1.Tick
' Get the current time
Dim curTime As DateTime = DateTime.Now
' Retrieve the hour value of the current time
Dim H As Integer = curTime.Hour
' Retrieve the minute value of the current time
Dim M As Integer = curTime.Minute
' Retrieve the second value of the current time
Dim S As Integer = curTime.Second
' Draw the progress boxes based on the values of the time
pgrHours.Value = H
pgrMinutes.Value = M
pgrSeconds.Value = S
' Display the values in the corresponding labels
lblHours.Text = CStr(H)
lblMinutes.Text = CStr(M)
lblSeconds.Text = CStr(S)
End Sub
|
- In the Class Name combo box, select btnClose
- In the Method Name combo box, select Click and implement the event as
follows:
Private Sub btnClose_Click(ByVal sender As Object, _
ByVal e As System.EventArgs) _
Handles btnClose.Click
End
End Sub
|
- Execute the application to test it then close the form:
- Close the form and return to your programming environment
|
The Style of a Progress Bar |
|
So far, we have described a progress bar as drawing small
rectangles to represent the control. These rectangles are visually distinct
(don't touch each other) and adjacent. As an alternative, if you want the
progress control to appear smooth, you can make the control draw each next
rectangle exactly where the previous one stops. To support this characteristic,
the ProgressBar class is equipped with the Style property.
The ProgressBar.Style property is based on the
ProgressBarStyle enumeration that has three members:
- Blocks: With this value, the progress bar draws small distinct and
adjacent rectangles:

- Continuous: The progress bar appears smooth as the same rectangles
are not separated:

- Marquee: The progress bar shows an animation that consists of
drawing a group of small rectangles that keep moving from left to right and
restart
If you decide to use the Marquee style, the progress
bar will continually show its animation of small rectangles moving at a speed
set from the MarqueeAnimationSpeed integral property and you cannot get
the value of the progress bar.
|
Practical
Learning: Configuring Progress Bars
|
|
- On the form, click the top progress bar
- Press and hold Ctrl
- Click the other two progress bars
- Release Ctrl
- In the Properties window, click Style, click the arrow of its combo box,
and select Continuous
- Execute the application to see the result

- Close the form and return to your programming environment
|
|