本文介绍了在VB中设置随机计时器间隔的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的目标是使代码在间隔1到30秒的任意时间随机发出蜂鸣声.到目前为止,这是我的代码:
My goal is to make code that will beep randomly anywhere from 1 to 30 seconds apart. Here is my code so far:
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是个好数字.我只是不知道如何将随机数应用于计时器间隔.
This beeps every one second. Now, I want to change the Timer
Interval so that it will beep randomly between 1 and 30 seconds. Maybe later I will add options for the user to define the bounds, but for now, 1 and 30 are good numbers. I just don't know how to apply a random number to my Timer Interval.
推荐答案
在每个Tick
上更改Interval
:
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
这篇关于在VB中设置随机计时器间隔的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!