Saturday, 17 December 2016

Digital Clock in Visual basic

Visual Basic programming is so simple that sometime you just need to write a one line code to create a wonderful tiny little but nice application. For example, you can write a one-line code to create a digital clock. In this program, you have to insert a timer control into the form . Then go to the properties window to set the timer's interval value to 1000 so that Visual Basic updates the time every 1000 milliseconds, or once a second. Other properties that you ought to set are to change the caption  such as "My Clock" and  to set Form1's MaxButton to false so that it cannot be resized by the user.


Now, double click the timer and enter the one line code as follows:

 Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick

        lblhour.Text = Format(Hour(TimeOfDay))
        lblminut.Text = Format(Minute(TimeOfDay))
        lblsecond.Text = Format(Second(TimeOfDay))
        If lblhour.Text <= 12 Then
            lbltime.Text = "AM"
        Else
            lbltime.Text = "pm"
        End If

        If Hour(TimeOfDay) > 12 Then
            lblhour.Text = Hour(TimeOfDay) - 12

        End If

    End Sub

0 comments:

Post a Comment