Hi all,

I've written the code to create a digital clock using a label and it's all working dandy.

Now though, I want to take it to the next level and use images for each of the digits instead of a simple label to make the clock fit more with the theme of the app I'm building.

I've spent a couple of hours searching around for any relevant code but haven't been able to find any....

So, does anyone have any pointers on how to acheive this? I'm assuming it's something to do with assigning an image to each of the 11 digits I need (1,2,3,4,5,6,7,8,9,0,: ) but can't seem to figure out how to do it.

Any help would be appreciated.

Thanks
TheMightySpud

Recommended Answers

All 8 Replies

Thanks for the quick reply,

I've had a look at the link you provided, but I'm having issues figuring out how it applies to assigning an image to a particular string value (ie. an image of the number 1 being assigned to the string value of '1') if you get my meaning.

Thanks
TheMightySpud

Use an if then statement to populate the images.

If strValue = 1 Then
'Load picture 1 here
ElseIf strValue = 2 Then
'Load picture 2 here
End if

Again, have a look at the link. Add all your pictures to an imagelist and add them to your picture box from there by using -

img.ImageDefault = imageList1.Images[0]

See if this helps.

Public Class Form1

    Private myImg(10) As Bitmap '// create Bitmap Array to store images.
    Private myImgAM As New Bitmap("C:\AM.png")
    Private myImgPM As New Bitmap("C:\PM.png")

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        '// add Images to Arrays in set order.
        myImg(0) = New Bitmap("C:\0.png")
        myImg(1) = New Bitmap("C:\1.png")
        myImg(2) = New Bitmap("C:\2.png")
        myImg(3) = New Bitmap("C:\3.png")
        myImg(4) = New Bitmap("C:\4.png")
        myImg(5) = New Bitmap("C:\5.png")
        myImg(6) = New Bitmap("C:\6.png")
        myImg(7) = New Bitmap("C:\7.png")
        myImg(8) = New Bitmap("C:\8.png")
        myImg(9) = New Bitmap("C:\9.png")
        '// start timer.
        Timer1.Enabled = True
    End Sub

    Private Sub displayTime()
        Dim x As String = Format(TimeOfDay, "hhmmsstt") '// get Hours, Minutes, Seconds, AM/PM.
        PictureBox1.Image = myImg(CInt(x.Substring(0, 1))) '// (h)hmmsstt.
        PictureBox2.Image = myImg(CInt(x.Substring(1, 1))) '// h(h)mmsstt.
        PictureBox3.Image = myImg(CInt(x.Substring(2, 1))) '// hh(m)msstt.
        PictureBox4.Image = myImg(CInt(x.Substring(3, 1))) '// hhm(m)sstt.
        PictureBox5.Image = myImg(CInt(x.Substring(4, 1))) '// hhmm(s)stt.
        PictureBox6.Image = myImg(CInt(x.Substring(5, 1))) '// hhmms(s)tt.
        If x.Substring(6, 2) = "AM" Then '// hhmmss(tt).
            PictureBox7.Image = myImgAM
        Else
            PictureBox7.Image = myImgPM
        End If
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        displayTime()
    End Sub
End Class
commented: Nicely executed. +4

Nicely executed.

That's absolutely phenomenal gents, thanks so much.

I'll give it a go in a little bit and let you know how it goes:)

TheMightySpud

Works like a charms gents, thanks again :)

TheMightySpud

Glad it is of use, although late night coding, and etc., I did make a slight code error.
This line..

Private myImg(10) As Bitmap '// create Bitmap Array to store images.

Should actually be -=1...

Private myImg(9) As Bitmap '// create Bitmap Array to store images.

I forgot my Index counting and hope that apology is accepted.

All the best,
codeorder.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.