这是我到目前为止的代码。当我开始时,什么也没有发生。我希望标签改变他们每秒钟显示的文本。显示的文本应该是基于方程的随机数。谢谢你!
Public Class Form1
Sub RandomValueFinder()
Do
Dim RandomNumber As Decimal
RandomNumber = Int((1 * Rnd()) - 0.5)
Dim RandomValuePH As Decimal = 7.3 + (0.8 * RandomNumber)
Dim RandomValueTemp As Decimal = 95 + (7 * RandomNumber)
Dim RandomValueFlow As Decimal = 1200 + (100 * RandomNumber)
Dim RandomValuePressure5 As Decimal = 5 + (2 * RandomNumber)
Dim RandomValuePressure20 As Decimal = 20 + (2 * RandomNumber)
LabelPH.Text = RandomValuePH
LabelTemp.Text = RandomValueTemp
LabelFlow.Text = RandomValueFlow
LabelPressure5.Text = RandomValuePressure5
LabelPressure20.Text = RandomValuePressure20
Loop
End Sub
End Class
我有几个注意事项。
首先,你永远不会调用你的RandomValueFinder
方法,所以代码永远不会运行。
其次,您当前通过Do/Loop更新标签。如果你真的调用了你的方法结果会是它会进入一个无限循环绑定了UI这实际上会"冻结"表单。
第三,表达式中不需要括号,因为有PEMDAS/BODMAS。
最后,不要使用Int()或Rnd(),它们是遗留的。
我建议使用计时器并在适当的方法中调用您的方法:
Public Class Form1
Private ReadOnly _random As Random
Private Readonly _timer As Forms.Timer
Sub New()
InitializeComponent()
_random = New Random()
_timer = New Forms.Timer() With { .Interval = 1000 } ' 1000ms = 1s
AddHandler _timer.Tick, AddressOf Timer_Tick
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
_timer.Start()
End Sub
Private Sub Timer_Tick(sender As Object, e As EventArgs)
RandomValueFinder()
End Sub
Sub RandomValueFinder()
Dim RandomNumber As Decimal = 1 * _random.Next() - 0.5
Dim RandomValuePH As Decimal = 7.3 + 0.8 * RandomNumber
Dim RandomValueTemp As Decimal = 95 + 7 * RandomNumber
Dim RandomValueFlow As Decimal = 1200 + 100 * RandomNumber
Dim RandomValuePressure5 As Decimal = 5 + 2 * RandomNumber
Dim RandomValuePressure20 As Decimal = 20 + 2 * RandomNumber
LabelPH.Text = RandomValuePH
LabelTemp.Text = RandomValueTemp
LabelFlow.Text = RandomValueFlow
LabelPressure5.Text = RandomValuePressure5
LabelPressure20.Text = RandomValuePressure20
End Sub
End Class