'
'SW403- Fall
2005
'Guillermo V. Julca
'http://www.gjp2s.com
Public Class
frmMain
Inherits
System.Windows.Forms.Form
Dim
intCurrentSeconds As Integer 'Global
variable to store the current seconds value
Dim
intCurrentMinutes As Integer 'Global
variable to store the current minutes value
Dim
intCurrentHours As Integer 'Global
variable to store the current hours value
Dim
lngBinaryCurrentSeconds As Long 'Global
variable to store the current binary seconds value
Dim
lngBinaryCurrentMinutes As Long
'Global variable to store the current binary
minutes value
Dim
lngBinaryCurrentHours As Long
'Global variable to store the current binary
hours value
#Region
" Windows Form Designer generated code "
Public Sub New()
MyBase.New()
'This call is
required by the Windows Form Designer.
InitializeComponent()
'Add any
initialization after the InitializeComponent() call
End Sub
'Form overrides
dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal
disposing As Boolean)
If
disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End
If
End If
MyBase.Dispose(disposing)
End Sub
'Required by the
Windows Form Designer
Private
components As System.ComponentModel.IContainer
'NOTE: The
following procedure is required by the Windows Form Designer
'It can be
modified using the Windows Form Designer.
'Do not modify it
using the code editor.
Friend WithEvents pnlDisplayClock As
System.Windows.Forms.Panel
Friend WithEvents myTimer As
System.Windows.Forms.Timer
Friend WithEvents Panel1 As
System.Windows.Forms.Panel
Friend WithEvents Panel2 As
System.Windows.Forms.Panel
Friend WithEvents Label1 As
System.Windows.Forms.Label
Friend WithEvents Label2 As
System.Windows.Forms.Label
Friend WithEvents Label3 As
System.Windows.Forms.Label
Friend WithEvents Label5 As
System.Windows.Forms.Label
Friend WithEvents lblDigitalMinutes As
System.Windows.Forms.Label
Friend WithEvents Label6 As
System.Windows.Forms.Label
Friend WithEvents lblDigitalSeconds As
System.Windows.Forms.Label
Friend WithEvents lblPMAM As
System.Windows.Forms.Label
Friend WithEvents Label4 As
System.Windows.Forms.Label
Friend WithEvents Label8 As
System.Windows.Forms.Label
Friend WithEvents Label9 As
System.Windows.Forms.Label
Friend WithEvents lblBinaryHours As
System.Windows.Forms.Label
Friend WithEvents lblBinaryMinutes As
System.Windows.Forms.Label
Friend WithEvents lblBinarySeconds As
System.Windows.Forms.Label
Friend WithEvents lblDigitalHours As
System.Windows.Forms.Label
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.components
= New System.ComponentModel.Container
Me.pnlDisplayClock
= New System.Windows.Forms.Panel
Me.myTimer
= New System.Windows.Forms.Timer(Me.components)
Me.Panel1
= New System.Windows.Forms.Panel
Me.lblPMAM
= New System.Windows.Forms.Label
Me.lblDigitalSeconds
= New System.Windows.Forms.Label
Me.Label6
= New System.Windows.Forms.Label
Me.lblDigitalMinutes
= New System.Windows.Forms.Label
Me.Label5
= New System.Windows.Forms.Label
Me.lblDigitalHours
= New System.Windows.Forms.Label
Me.Panel2 = New
System.Windows.Forms.Panel
Me.lblBinarySeconds
= New System.Windows.Forms.Label
Me.lblBinaryMinutes
= New System.Windows.Forms.Label
Me.Label9
= New System.Windows.Forms.Label
Me.Label8
= New System.Windows.Forms.Label
Me.lblBinaryHours
= New System.Windows.Forms.Label
Me.Label4
= New System.Windows.Forms.Label
Me.Label1
= New System.Windows.Forms.Label
Me.Label2
= New System.Windows.Forms.Label
Me.Label3
= New System.Windows.Forms.Label
Me.Panel1.SuspendLayout()
Me.Panel2.SuspendLayout()
Me.SuspendLayout()
'
'pnlDisplayClock
'
Me.pnlDisplayClock.BackColor
= System.Drawing.Color.FromArgb(CType(128, Byte), CType(128, Byte), CType(255, Byte))
Me.pnlDisplayClock.Location
= New System.Drawing.Point(248, 48)
Me.pnlDisplayClock.Name
= "pnlDisplayClock"
Me.pnlDisplayClock.Size
= New System.Drawing.Size(250, 250)
Me.pnlDisplayClock.TabIndex
= 0
'
'myTimer
'
'
'Panel1
'
Me.Panel1.BackColor
= System.Drawing.Color.FromArgb(CType(255, Byte), CType(255, Byte), CType(192, Byte))
Me.Panel1.Controls.Add(Me.lblPMAM)
Me.Panel1.Controls.Add(Me.lblDigitalSeconds)
Me.Panel1.Controls.Add(Me.Label6)
Me.Panel1.Controls.Add(Me.lblDigitalMinutes)
Me.Panel1.Controls.Add(Me.Label5)
Me.Panel1.Controls.Add(Me.lblDigitalHours)
Me.Panel1.Location
= New System.Drawing.Point(40, 48)
Me.Panel1.Name
= "Panel1"
Me.Panel1.Size
= New System.Drawing.Size(200, 250)
Me.Panel1.TabIndex
= 1
'
'lblPMAM
'
Me.lblPMAM.BackColor
= System.Drawing.Color.Black
Me.lblPMAM.Font = New
System.Drawing.Font("Microsoft Sans Serif", 12.0!,
System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.lblPMAM.ForeColor
= System.Drawing.Color.White
Me.lblPMAM.Location
= New System.Drawing.Point(152, 96)
Me.lblPMAM.Name
= "lblPMAM"
Me.lblPMAM.Size
= New System.Drawing.Size(32, 32)
Me.lblPMAM.TabIndex
= 5
Me.lblPMAM.Text
= "PM"
Me.lblPMAM.TextAlign
= System.Drawing.ContentAlignment.MiddleCenter
'
'lblDigitalSeconds
'
Me.lblDigitalSeconds.BackColor
= System.Drawing.Color.Black
Me.lblDigitalSeconds.Font
= New System.Drawing.Font("Microsoft Sans
Serif", 15.0!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point,
CType(0, Byte))
Me.lblDigitalSeconds.ForeColor
= System.Drawing.Color.White
Me.lblDigitalSeconds.Location
= New System.Drawing.Point(112, 96)
Me.lblDigitalSeconds.Name
= "lblDigitalSeconds"
Me.lblDigitalSeconds.Size
= New System.Drawing.Size(32, 32)
Me.lblDigitalSeconds.TabIndex
= 4
Me.lblDigitalSeconds.Text
= "09"
Me.lblDigitalSeconds.TextAlign
= System.Drawing.ContentAlignment.MiddleLeft
'
'Label6
'
Me.Label6.BackColor
= System.Drawing.Color.Black
Me.Label6.Font
= New System.Drawing.Font("Microsoft Sans
Serif", 15.0!, System.Drawing.FontStyle.Bold,
System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.Label6.ForeColor
= System.Drawing.Color.White
Me.Label6.Location
= New System.Drawing.Point(96, 96)
Me.Label6.Name
= "Label6"
Me.Label6.Size
= New System.Drawing.Size(16, 32)
Me.Label6.TabIndex
= 3
Me.Label6.Text
= ":"
Me.Label6.TextAlign
= System.Drawing.ContentAlignment.MiddleCenter
'
'lblDigitalMinutes
'
Me.lblDigitalMinutes.BackColor
= System.Drawing.Color.Black
Me.lblDigitalMinutes.Font
= New System.Drawing.Font("Microsoft Sans
Serif", 15.0!, System.Drawing.FontStyle.Bold,
System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.lblDigitalMinutes.ForeColor
= System.Drawing.Color.White
Me.lblDigitalMinutes.Location
= New System.Drawing.Point(64, 96)
Me.lblDigitalMinutes.Name
= "lblDigitalMinutes"
Me.lblDigitalMinutes.Size
= New System.Drawing.Size(32, 32)
Me.lblDigitalMinutes.TabIndex
= 2
Me.lblDigitalMinutes.Text
= "09"
Me.lblDigitalMinutes.TextAlign
= System.Drawing.ContentAlignment.MiddleCenter
'
'Label5
'
Me.Label5.BackColor
= System.Drawing.Color.Black
Me.Label5.Font
= New System.Drawing.Font("Microsoft Sans
Serif", 15.0!, System.Drawing.FontStyle.Bold,
System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.Label5.ForeColor
= System.Drawing.Color.White
Me.Label5.Location
= New System.Drawing.Point(48, 96)
Me.Label5.Name
= "Label5"
Me.Label5.Size
= New System.Drawing.Size(16, 32)
Me.Label5.TabIndex
= 1
Me.Label5.Text
= ":"
Me.Label5.TextAlign
= System.Drawing.ContentAlignment.MiddleCenter
'
'lblDigitalHours
'
Me.lblDigitalHours.BackColor
= System.Drawing.Color.Black
Me.lblDigitalHours.Font
= New System.Drawing.Font("Microsoft Sans
Serif", 15.0!, System.Drawing.FontStyle.Bold,
System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.lblDigitalHours.ForeColor
= System.Drawing.Color.White
Me.lblDigitalHours.Location
= New System.Drawing.Point(16, 96)
Me.lblDigitalHours.Name
= "lblDigitalHours"
Me.lblDigitalHours.Size
= New System.Drawing.Size(32, 32)
Me.lblDigitalHours.TabIndex
= 0
Me.lblDigitalHours.Text
= "09"
Me.lblDigitalHours.TextAlign
= System.Drawing.ContentAlignment.MiddleRight
'
'Panel2
'
Me.Panel2.BackColor
= System.Drawing.Color.FromArgb(CType(255, Byte), CType(192, Byte), CType(192, Byte))
Me.Panel2.Controls.Add(Me.lblBinarySeconds)
Me.Panel2.Controls.Add(Me.lblBinaryMinutes)
Me.Panel2.Controls.Add(Me.Label9)
Me.Panel2.Controls.Add(Me.Label8)
Me.Panel2.Controls.Add(Me.lblBinaryHours)
Me.Panel2.Controls.Add(Me.Label4)
Me.Panel2.Location
= New System.Drawing.Point(504, 48)
Me.Panel2.Name
= "Panel2"
Me.Panel2.Size
= New System.Drawing.Size(200, 250)
Me.Panel2.TabIndex
= 2
'
'lblBinarySeconds
'
Me.lblBinarySeconds.Font
= New System.Drawing.Font("Microsoft Sans
Serif", 12.0!, System.Drawing.FontStyle.Bold,
System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.lblBinarySeconds.Location
= New System.Drawing.Point(96, 136)
Me.lblBinarySeconds.Name =
"lblBinarySeconds"
Me.lblBinarySeconds.Size
= New System.Drawing.Size(90, 24)
Me.lblBinarySeconds.TabIndex
= 5
Me.lblBinarySeconds.Text
= "111011"
'
'lblBinaryMinutes
'
Me.lblBinaryMinutes.Font
= New System.Drawing.Font("Microsoft Sans
Serif", 12.0!, System.Drawing.FontStyle.Bold,
System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.lblBinaryMinutes.Location
= New System.Drawing.Point(96, 104)
Me.lblBinaryMinutes.Name
= "lblBinaryMinutes"
Me.lblBinaryMinutes.Size
= New System.Drawing.Size(90, 24)
Me.lblBinaryMinutes.TabIndex
= 4
Me.lblBinaryMinutes.Text
= "111011"
'
'Label9
'
Me.Label9.Font
= New System.Drawing.Font("Microsoft Sans
Serif", 10.0!, System.Drawing.FontStyle.Bold,
System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.Label9.Location
= New System.Drawing.Point(16, 104)
Me.Label9.Name
= "Label9"
Me.Label9.Size
= New System.Drawing.Size(72, 24)
Me.Label9.TabIndex
= 3
Me.Label9.Text
= "Minutes :"
'
'Label8
'
Me.Label8.Font
= New System.Drawing.Font("Microsoft Sans
Serif", 10.0!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point,
CType(0, Byte))
Me.Label8.Location
= New System.Drawing.Point(16, 136)
Me.Label8.Name
= "Label8"
Me.Label8.Size
= New System.Drawing.Size(72, 24)
Me.Label8.TabIndex
= 2
Me.Label8.Text
= "Seconds :"
'
'lblBinaryHours
'
Me.lblBinaryHours.Font
= New System.Drawing.Font("Microsoft Sans
Serif", 12.0!, System.Drawing.FontStyle.Bold,
System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.lblBinaryHours.Location
= New System.Drawing.Point(96, 72)
Me.lblBinaryHours.Name
= "lblBinaryHours"
Me.lblBinaryHours.Size
= New System.Drawing.Size(90, 24)
Me.lblBinaryHours.TabIndex
= 1
Me.lblBinaryHours.Text
= "111011"
'
'Label4
'
Me.Label4.Font
= New System.Drawing.Font("Microsoft Sans
Serif", 10.0!, System.Drawing.FontStyle.Bold,
System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.Label4.Location
= New System.Drawing.Point(16, 72)
Me.Label4.Name
= "Label4"
Me.Label4.Size
= New System.Drawing.Size(64, 24)
Me.Label4.TabIndex
= 0
Me.Label4.Text
= "Hours :"
'
'Label1
'
Me.Label1.Font
= New System.Drawing.Font("Microsoft Sans
Serif", 11.25!, CType((System.Drawing.FontStyle.Bold
Or System.Drawing.FontStyle.Underline),
System.Drawing.FontStyle), System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.Label1.ForeColor
= System.Drawing.Color.Blue
Me.Label1.Location
= New System.Drawing.Point(256, 16)
Me.Label1.Name
= "Label1"
Me.Label1.Size
= New System.Drawing.Size(176, 24)
Me.Label1.TabIndex
= 3
Me.Label1.Text
= "Analog Display"
'
'Label2
'
Me.Label2.Font
= New System.Drawing.Font("Microsoft Sans
Serif", 11.25!, CType((System.Drawing.FontStyle.Bold
Or System.Drawing.FontStyle.Underline),
System.Drawing.FontStyle), System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.Label2.ForeColor
= System.Drawing.Color.DarkGreen
Me.Label2.Location
= New System.Drawing.Point(48, 16)
Me.Label2.Name
= "Label2"
Me.Label2.Size
= New System.Drawing.Size(136, 24)
Me.Label2.TabIndex
= 4
Me.Label2.Text
= "Digital Display"
'
'Label3
'
Me.Label3.Font
= New System.Drawing.Font("Microsoft Sans
Serif", 11.25!, CType((System.Drawing.FontStyle.Bold
Or System.Drawing.FontStyle.Underline),
System.Drawing.FontStyle), System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.Label3.ForeColor
= System.Drawing.Color.Maroon
Me.Label3.Location
= New System.Drawing.Point(504, 16)
Me.Label3.Name
= "Label3"
Me.Label3.Size
= New System.Drawing.Size(168, 24)
Me.Label3.TabIndex = 5
Me.Label3.Text
= "Binary Display"
'
'frmMain
'
Me.AutoScaleBaseSize
= New System.Drawing.Size(6, 15)
Me.BackColor
= System.Drawing.Color.FromArgb(CType(192, Byte), CType(255, Byte), CType(192, Byte))
Me.ClientSize
= New System.Drawing.Size(752, 354)
Me.Controls.Add(Me.Label3)
Me.Controls.Add(Me.Label2)
Me.Controls.Add(Me.Label1)
Me.Controls.Add(Me.Panel2)
Me.Controls.Add(Me.Panel1)
Me.Controls.Add(Me.pnlDisplayClock)
Me.Font
= New System.Drawing.Font("Microsoft Sans
Serif", 9.75!, System.Drawing.FontStyle.Regular,
System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.Name
= "frmMain"
Me.StartPosition
= System.Windows.Forms.FormStartPosition.CenterScreen
Me.Text
= "GJ_Clock- Time Displays"
Me.Panel1.ResumeLayout(False)
Me.Panel2.ResumeLayout(False)
Me.ResumeLayout(False)
End Sub
#End
Region
Private Sub Panel1_Paint(ByVal
sender As System.Object, ByVal e As
System.Windows.Forms.PaintEventArgs) Handles
pnlDisplayClock.Paint
Dim
myPenCircunference = New Pen(Color.Blue) 'Pen to draw
the circunference of the analog clock
Dim
myPenHandSeconds = New Pen(Color.Red) 'Pen to
draw the hand seconds
Dim
myPenHandMinutes = New Pen(Color.Black, 2) 'Pen to draw
the hand minutes
Dim
myPenHandHours = New Pen(Color.Black, 2) 'Pen to draw
the hand hours
Dim
myBrush = New SolidBrush(Color.Silver) 'Brush to
paint the analog clock's background
Dim
myBrushCenter = New
SolidBrush(Color.Black) 'Brush to paint the small circle in the center of the
analog clock
Dim
sngCenterSideSquare As Single
'Dimension of the little square used to draw the little circle in the
center of the analog clock
Dim
sngDelta As Single 'Gap beween border's panel and the square that we use to
draw the circle for our analog clock
Dim
sngLengthHandSeconds As Single 'Dimension of the hand seconds
Dim
sngLengthHandMinutes As Single 'Dimension of the hand minutes
Dim
sngLengthHandHours As Single 'Dimension
of the hand hours
Dim
sngRadius As Single 'Radius of the circle for our analog clock
Dim
sngDiameter As Single 'Diameter of the circle for our analog clock
Dim
sngXc As Single 'X-coordinate value of the center of the analog clock'
circle
Dim
sngYc As Single 'Y-coordinate value of the center of the analog clock'
circle
Dim
sngXEnd As Single 'X-coordinate value of the end of the hand seconds,hand
minutes, or hand hours
Dim
sngYEnd As Single 'Y-coordinate value of the end of the hand seconds,hand
minutes, or hand hours
Dim intCount As Integer
Dim
drawFont As New
Font("Arial", 10)
'Font for the clock's numbers
Dim
drawBrush As New
SolidBrush(Color.Green) 'Brush for the clock's numbers
Dim
drawFormat As New
StringFormat 'Format for
the clock's numbers
Dim
sngLengthPosNumbers As Single 'Distance
value from clock's number position to the center of the clock
drawFormat.Alignment =
StringAlignment.Center
sngCenterSideSquare = 5
sngDelta = 0.5
sngDiameter =
pnlDisplayClock.Width 'note pnlDisplayClock.Width= pnlDisplayClock.Height
sngRadius = sngDiameter / 2
sngXc = sngRadius
sngYc = sngRadius
e.Graphics.DrawEllipse(myPenCircunference,
CSng(sngDelta), CSng(sngDelta),
sngDiameter - 2 * sngDelta, sngDiameter - 2 * sngDelta)
e.Graphics.FillEllipse(myBrush, CSng(2 * sngDelta), CSng(2
* sngDelta), sngDiameter - 4 * sngDelta, sngDiameter - 4 * sngDelta)
e.Graphics.FillEllipse(myBrushCenter, CSng((sngDiameter - sngCenterSideSquare) / 2), CSng((sngDiameter - sngCenterSideSquare) / 2), CSng(sngCenterSideSquare), CSng(sngCenterSideSquare))
sngLengthHandSeconds = sngRadius * 0.8
sngLengthPosNumbers =
sngLengthHandSeconds
sngLengthHandMinutes =
sngLengthHandSeconds * 0.8
sngLengthHandHours =
sngLengthHandSeconds * 0.6
'To draw
numbers for the clock
For
intCount = 1 To 12
Call
GetNewEndXYRadiusCoordinates(sngLengthPosNumbers, _
intCount * Math.PI / 6, _
sngXc,
_
sngYc,
_
sngXEnd, _
sngYEnd)
e.Graphics.DrawString(CStr(intCount), drawFont, drawBrush, sngXEnd, sngYEnd
- 10, drawFormat)
Next
' Sets the
timer interval to 1 second.
myTimer.Interval = 1000
myTimer.Start()
'To draw the
seconds hand
'Note each
step of the seconds hand will cover a circular sector with an angle= 2PI/60=
PI/30
Call
GetNewEndXYRadiusCoordinates(sngLengthHandSeconds, _
intCurrentSeconds * Math.PI / 30, _
sngXc, _
sngYc, _
sngXEnd, _
sngYEnd)
e.Graphics.DrawLine(myPenHandSeconds, sngXc,
sngYc, sngXEnd, sngYEnd)
'To draw
the minutes hand
'Note each
step of the minutes hand will cover a circular sector with an angle= 2PI/60=
PI/30
Call
GetNewEndXYRadiusCoordinates(sngLengthHandMinutes, _
(intCurrentMinutes + intCurrentSeconds / 60) * Math.PI / 30, _
sngXc, _
sngYc, _
sngXEnd, _
sngYEnd)
e.Graphics.DrawLine(myPenHandMinutes,
sngXc, sngYc, sngXEnd, sngYEnd)
'To draw the
hours hand
'Note each
step of the hours hand will cover a circular sector with an angle= 2PI/12= PI/6
'assumming 12
hours format
If
intCurrentHours > 12 Then
intCurrentHours = intCurrentHours -
12
End If
Call
GetNewEndXYRadiusCoordinates(sngLengthHandHours, _
(intCurrentHours + intCurrentMinutes / 60) * Math.PI / 6, _
sngXc, _
sngYc, _
sngXEnd, _
sngYEnd)
e.Graphics.DrawLine(myPenHandHours,
sngXc, sngYc, sngXEnd, sngYEnd)
End Sub
Private Sub GetNewEndXYRadiusCoordinates(ByVal sngPolarRadius As
Single, _
ByVal sngAngleRadians As
Single, _
ByVal sngXcenter As Single, _
ByVal sngYcenter As Single, _
ByRef sngXcoordinate As
Single, _
ByRef sngYcoordinate As
Single)
'Note:
sngAngleRadians is the angle between the Y vertical axis and the Polar Radius
'expressed in radians.
'We will use
simple formulas to convert Polar coordinates to XY cooordinates
'
' x
' -----------
' | /
' | /
' | / x = r * Sin(A)
' y |
/
' |
/ y = r * Cos(A)
' |
/ r
' |
/ x^2 + y^2 = r^2
*( Sin(A)^2 + Cos(A)^2 )
' |
/
' |A / x^2 + y^2 = r^2 (Pitagoras)
' | /
' |/
sngXcoordinate = sngXcenter +
sngPolarRadius * Math.Sin(sngAngleRadians)
sngYcoordinate = sngYcenter -
sngPolarRadius * Math.Cos(sngAngleRadians)
'NOTE: Sin(sngAngleRadians)>=0 for all 0<=sngAngleRadians<=PI
' Sin(sngAngleRadians)<0 for all PI<sngAngleRadians< 2PI
' Cos(sngAngleRadians)>=0 for all 0<sngAngleRadians< PI/2 and
3PI/2<sngAngleRadians< 2PI
' Cos(sngAngleRadians)<0 for all PI/2<sngAngleRadians< 3PI/2
End Sub
Private Sub myTimer_Tick(ByVal
sender As System.Object, ByVal e As
System.EventArgs) Handles myTimer.Tick
intCurrentSeconds = Date.Now.Second
intCurrentMinutes = Date.Now.Minute
intCurrentHours = Date.Now.Hour
Call
UpdateDigitalClock(intCurrentHours, intCurrentMinutes, intCurrentSeconds)
ConvertDecimalToBinary(intCurrentSeconds, lngBinaryCurrentSeconds)
ConvertDecimalToBinary(intCurrentMinutes, lngBinaryCurrentMinutes)
ConvertDecimalToBinary(intCurrentHours,
lngBinaryCurrentHours)
Call
UpdateBinaryDisplay(lngBinaryCurrentHours, lngBinaryCurrentMinutes,
lngBinaryCurrentSeconds)
pnlDisplayClock.Invalidate()
End Sub
Private Sub frmMain_Load(ByVal
sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'To
initialize the current time
intCurrentSeconds = Date.Now.Second
intCurrentMinutes = Date.Now.Minute
intCurrentHours = Date.Now.Hour
'To
initialize Digital Clock
Call
UpdateDigitalClock(intCurrentHours, intCurrentMinutes, intCurrentSeconds)
ConvertDecimalToBinary(intCurrentSeconds, lngBinaryCurrentSeconds)
ConvertDecimalToBinary(intCurrentMinutes, lngBinaryCurrentMinutes)
ConvertDecimalToBinary(intCurrentHours,
lngBinaryCurrentHours)
'To
initialize Binary display
Call
UpdateBinaryDisplay(lngBinaryCurrentHours, lngBinaryCurrentMinutes,
lngBinaryCurrentSeconds)
End Sub
Private Sub UpdateDigitalClock(ByVal
intHours As Integer,
_
ByVal intMinutes As Integer, _
ByVal intSeconds As Integer)
lblPMAM.Text = "AM"
If
intHours > 12 Then
intHours = intHours - 12
lblPMAM.Text = "PM"
End If
lblDigitalHours.Text = CStr(intHours)
lblDigitalMinutes.Text =
Microsoft.VisualBasic.Right("0" + CStr(intMinutes),
2)
lblDigitalSeconds.Text =
Microsoft.VisualBasic.Right("0" + CStr(intSeconds),
2)
'Call
lblDigitalSeconds.Refresh()
End Sub
Private Sub UpdateBinaryDisplay(ByVal
lngBinaryHours As Long,
_
ByVal lngBinaryMinutes As
Long, _
ByVal lngBinarySeconds As
Long)
lblBinaryHours.Text = CStr(lngBinaryHours)
lblBinaryMinutes.Text = CStr(lngBinaryMinutes)
lblBinarySeconds.Text = CStr(lngBinarySeconds)
End Sub
Private Sub ConvertDecimalToBinary(ByVal
intDecimalNumber As Integer,
_
ByRef lngBinaryNumber As
Long)
'We will use the basic math logic Decimal to Binary
conversion as follows:
'We have to
divide the decimal number by 2 iterativelly and keep the remainder results for
each division
'until the
next quotient is equal to ZERO. The binary number will be formed for all the
remainders
'starting
from the last remainder to the first remainder.
'
'Example:
Decimal number to convert = 29
'
' 29 / 2 = 14
R1 ^
' 14 / 2 =
7 R0 |
' 7 / 2 =
3 R1 |
' 3 / 2 =
1 R1 |
' 1 / 2 =
0 R1 |
<----- stop here because the quotient = 0
'
' Therefore,
Binary number will be = 11101 =
1*10^4 + 1*10^3 + 1*10^2 + 0*10^1 + 1*10^0
'
'NOTE: Also
you can see the implementation of this algorithm in my website
' that I implemented in C#(Console
Application) many years ago.
'
http://www.gjp2s.com/CSDecimalToBinary.htm
Dim
intAuxDecimalNumber As Integer
Dim
intNextQuotient As Integer
Dim
intRemainder As Integer
Dim
intCount As Integer
intCount = 0
lngBinaryNumber = 0
intAuxDecimalNumber = intDecimalNumber
Do
intNextQuotient =
intAuxDecimalNumber \ 2 'We are using integer
division operator (\)
intRemainder = intAuxDecimalNumber Mod 2 'To get the remainder of the division
lngBinaryNumber = CLng(lngBinaryNumber + (intRemainder * Math.Pow(10.0,
intCount)))
intAuxDecimalNumber =
intNextQuotient
intCount = intCount + 1
Loop While (intNextQuotient <> 0)
End Sub
End Class