我是VB的新手,我想制作一个程序,将最小数量和最大数量之间的数字随机化。
以下是代码:
Public Class Form1
Dim rnumber As Integer
Public Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
Public Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim rndnumber As Random
Dim number As Integer
rndnumber = New Random
number = rndnumber.Next(1, 10001)
rnumber = number
Timer1.Start()
End Sub
Public Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
If rnumber < TextBox1.Text Or rnumber > TextBox2.Text Then
Dim rndnumber As Random
Dim number As Integer
rndnumber = New Random
number = rndnumber.Next(1, 10001)
Else
Timer1.Stop()
Label4.Text = rnumber.ToString
End If
End Sub
End Class
当我点击按钮时没有发生任何事情,程序甚至都没有冻结!
请帮助,TNX。
答案 0 :(得分:0)
别忘了设置:
Timer1.Enabled = True
在代码中,或在表单上的Timer对象的属性窗口中。
答案 1 :(得分:0)
您不需要在每个tick中创建Random类的实例,而是在Form_Load中创建它:
Public Class Form1
Dim rnumber As Integer
Dim rndnumber As Random
Public Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
rndnumber = New Random
End Sub
'...
Public Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
If rnumber < TextBox1.Text Or rnumber > TextBox2.Text Then
rnumber = rndnumber.Next(1, 10001) 'changed from "number"
Else
Timer1.Stop()
Label4.Text = rnumber.ToString
End If
End Sub
请注意,您从未将生成的值分配给rnumber
,因此您的代码无法按预期运行,因此我将number =
更改为rnumber =
。