We help IT Professionals succeed at work.

Visual Basic Example

on
Medium Priority
482 Views
Dear Sir,
Could you please give me a step by step walk thru' of how to run this code in Visual basic
Rgds,
``````Here is some code

Add a 200x200 PictureBox (you can make it smaller or bigger but make it square) and a Label.

Drag in the inner blue circle to change the minutes.  Drag in the outer red circle to change the hour.

(...I have my angles going the opposite direction of what you wanted...)

Public Class Form1

Private Const PI As Single = 3.141592654

' these two determine the clock position
' counterclockwise motion increases angle
' Valid values are 0 --> 359 (six degrees = one minute)
' up = 0
' left = 90
' down = 180
' right = 270

' Time = 3:55
Private hourHand As Single = 270
Private minuteHand As Single = 30

' these are the actual time values you can read from somewhere else
' like another form
Public curHour As Integer
Public curMinute As Integer
Private curSecond As Integer

Private clockFace As Bitmap

Private Function DrawClockFace() As Bitmap
Dim bmp As New Bitmap(PictureBox1.Width, PictureBox1.Height)
Dim g As Graphics = Graphics.FromImage(bmp)

Dim cx As Single, cy As Single
Dim a As Integer
Dim p As Point
Dim s As Integer

' Compute the center of the PictureBox
cx = PictureBox1.Width / 2
cy = PictureBox1.Height / 2

' Clear the box
g.Clear(PictureBox1.BackColor)

' Main Circle of the Clock
Dim radius As Double = PictureBox1.Width * 0.8 / 2

For a = 0 To 354 Step 6
' Minute Tick Marks
p = getPoint(cx, cy, a, PictureBox1.Width * 0.6 / 2)
g.DrawRectangle(Pens.Blue, New Rectangle(p.X, p.Y, 1, 1))

If a Mod 30 = 0 Then
' Hour Tick Marks
p = getPoint(cx, cy, a, PictureBox1.Width * 0.7 / 2)
g.DrawRectangle(Pens.Red, New Rectangle(p.X, p.Y, 1, 1))

' Put the Hours (1 to 12) on the Clock Face
p = getPoint(cx - 6, cy - 8, a, PictureBox1.Width * 0.9 / 2)
s = 12 - a / 30
If s = 0 Then
s = 12
End If
g.DrawString(s, Me.Font, Brushes.Black, p.X, p.Y)
End If
Next a
g.Dispose()

Return bmp
End Function

Private Sub PictureBox1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint
If IsNothing(clockFace) Then
clockFace = DrawClockFace()
PictureBox1.Image = clockFace
End If

Dim cx As Single, cy As Single
Dim p As Point

cx = PictureBox1.Width / 2
cy = PictureBox1.Height / 2

p = getPoint(cx, cy, minuteHand, PictureBox1.Width * 0.58 / 2)
e.Graphics.DrawLine(Pens.Blue, cx, cy, p.X, p.Y)

p = getPoint(cx, cy, hourHand, PictureBox1.Width * 0.38 / 2)
e.Graphics.DrawLine(Pens.Red, cx, cy, p.X, p.Y)

curHour = 12 - (hourHand \ 30)
If curHour = 0 Then
curHour = 12
End If

curMinute = 60 - minuteHand \ 6
If curMinute >= 60 Then
curMinute = curMinute - 60
End If

Label1.Text = curHour & ":" & curMinute.ToString("00")
End Sub

Private Sub PictureBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown
PictureBox1_MouseMove(sender, e)
End Sub

Private Sub PictureBox1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove
Dim cx As Single, cy As Single
Dim angle As Single
Dim dist As Single

If e.Button = Windows.Forms.MouseButtons.Left Then
cx = PictureBox1.Width / 2
cy = PictureBox1.Height / 2

angle = getAngleInDegrees(cx, cy, e.X, e.Y)
dist = getDistance(cx, cy, e.X, e.Y)

If dist > PictureBox1.Width * 0.6 / 2 And dist < PictureBox1.Width * 0.8 / 2 Then
hourHand = 360 - (angle \ 30) * 30 ' discrete jumps (note integer division)
' make it jump between hours at halfway point
If angle Mod 30 > 15 Then
hourHand = hourHand - 30
End If
If hourHand = 360 Then
hourHand = 0
End If

PictureBox1.Refresh()
ElseIf dist < PictureBox1.Width * 0.6 / 2 Then
minuteHand = 360 - (angle \ 6) * 6 ' discrete jumps (note integer division)
If angle Mod 6 >= 3 Then
minuteHand = minuteHand - 6
End If
If minuteHand = 360 Then
minuteHand = 0
End If

PictureBox1.Refresh()
End If
End If
End Sub

Private Function getDistance(ByVal Ax As Single, ByVal Ay As Single, ByVal Bx As Single, ByVal By As Single) As Single
' Pythagoreans Theorem
getDistance = Math.Sqrt((Ax - Bx) * (Ax - Bx) + (Ay - By) * (Ay - By))
End Function

Private Function getPoint(ByVal cx As Single, ByVal cy As Single, ByVal angleInDegrees As Single, ByVal distance As Single) As Point
Dim tp As Point

' angleInDegrees
' Valid values are 0 --> 359 (six degrees = one minute)
' draw a line from the center of the circle:
' 0 = up
' 90 = left
' 180 = down
' 270 = right

' Offset and Invert Angle then Convert Degrees to Radians
angleInDegrees = -(angleInDegrees + 90) * PI / 180

' Compute the point that is at the specified angle from horizontal and
' at the specifed distance
tp.X = cx + distance * Math.Cos(angleInDegrees)
tp.Y = cy + distance * Math.Sin(angleInDegrees)

Return tp
End Function

Private Function getAngleInDegrees(ByVal cx As Single, ByVal cy As Single, ByVal X As Single, ByVal Y As Single) As Single
Dim dx As Single, dy As Single
Dim angle As Single

' draw a line from the center of the circle
' to the where the cursor is...
' If the line points:
' up = 0 degrees
' right = 90 degrees
' down = 180 degrees
' left = 270 degrees

dy = Y - cy
dx = X - cx
If dx = 0 Then
' Straight up and down cause div by zero error
If dy <= 0 Then
angle = 0
Else
angle = 180
End If
Else
angle = Math.Atan(dy / dx)
' Convert to Degrees
angle = angle * (180 / PI)

' Make it a number from 0 to 360 degrees
If X > cx Then
angle = 90 + angle
Else
angle = 270 + angle
End If
End If

Return angle
End Function

End Class
``````
Comment
Watch Question

View Solution Only

Senior .Net Consultant
CERTIFIED EXPERT
Top Expert 2016

Commented:
homework? EE is not a place where you can get your homework done. Work on it by yourself and come back with precise questions.
High School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
CERTIFIED EXPERT
Top Expert 2009

Commented:
That's not homework emoreau, it's some code I posted here:
http://www.experts-exchange.com/Programming/Languages/.NET/Visual_Basic.NET/Q_22016413.html

Here is what it produces:
AnalogClockExample.jpg
High School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
CERTIFIED EXPERT
Top Expert 2009
Commented:
What don't you get?...

It's really easy to setup:
(1) Create a New Project.
(2) Add a PictureBox Control (PictureBox1) and set its Size Property to 200,200.
(4) Press F7 to see the Code for the Form.
(5) Paste the Code I submitted over everything in the Code window.
(6) Run it by hitting F5.
(7) Drag Inside the BLUE dots to change the Minute hand.
(8) Drag Outside the BLUE dots (where the red dots are) to change the Hour hand.
(9) The selected time will appear in the Label.

Not the solution you were looking for? Getting a personalized solution is easy.

Thanks for using Experts Exchange.

• View three pieces of content (articles, solutions, posts, and videos)
• Ask the experts questions (counted toward content limit)
• Customize your dashboard and profile