我的目标是制作能够在1到30秒之间随机发出哔哔声的代码。到目前为止,这是我的代码:
Public Class Form1
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Console.Beep()
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Timer1.Stop()
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Timer1.Start()
End Sub
End Class
每秒发出一声哔哔声。现在,我想更改Timer
间隔,以便它会在1到30秒之间随机发出哔哔声。也许以后我会为用户添加选项以定义边界,但是现在,1和30都是好数字。我只是不知道如何在我的Timer Interval中应用一个随机数。
答案 0 :(得分:0)
更改每个Interval
上的Tick
:
Public Class Form1
Private p_oRandom As Random
Private Const INTERVAL_MIN_SEC As Integer = 1
Private Const INTERVAL_MAX_SEC As Integer = 30
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
p_oRandom = New Random
End Sub
Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As EventArgs) Handles _
Timer1.Tick
Console.Beep()
Timer1.Interval = p_oRandom.Next(INTERVAL_MIN_SEC, INTERVAL_MAX_SEC) * 1000
End Sub
End Class