I'm making an alarm clock whose form's backColor should change randomlly every 5 seconds. I am not sure as to how to do this. I'm using a timer so after the alarm clock's given time matches the current time, a song will be played. But I do not know how to count the seconds, or milliseconds that pass after starting time.

4 Years
Discussion Span
Last Post by codeorder

See if this helps.

Public Class Form1
    Private WithEvents tmrMain As New Timer With {.Interval = 2000, .Enabled = True}
    Private rnd As New Random

    Private Sub tmrMain_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles tmrMain.Tick
        Static r, g, b As Integer
        r = rnd.Next(0, 256) : g = rnd.Next(0, 256) : b = rnd.Next(0, 256)
        Me.BackColor = Color.FromArgb(r, g, b)
    End Sub
End Class

It appears to work, but I found something else in the end, thanks.

Dim rojo, verde, azul As Integer
Private Sub timColores_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles timColores.Tick
        txtHora.Text = Format(Now(), "hh:mm:ss")
        rojo = Int(Rnd() * 255)
        verde = Int(Rnd() * 255)
        azul = Int(Rnd() * 255)

        txtDespertador.BackColor = Color.FromArgb(rojo, verde, azul)
    End Sub

I would change those 255's to 256 since Int(Rnd() * 255) will return values from 0 To 254 and Not 0 To 255 as it should.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.