如果我的计数器在文本框 1(TB1) 处以 10 结束,则随机数不会显示在我的文本框 2(TB2) 中

If my counter ends at 10 at textbox1(TB1) the random number won't show in my textbox2(TB2)

如果我的计数器在文本框 1 (TB1) 处以 10 结束,则随机数将不会显示在我的文本框 2(TB2) 中

Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        If TB1.Text = 10 Then
            Dim num1 As Integer
            Dim randomnumber As New Random

            num1 = randomnumber.Next(100, 201)
            TB2.Text = num1
        End If
        Timer1.Enabled = True

    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick

        TB1.Text = TB1.Text + 1
        If TB1.Text = 10 Then
            Timer1.Enabled = False
        End If

    End Sub
End Class

使用数字变量来保持计数,而不是 TextBox。 TextBox.Text 属性 是字符串类型,不是整数!

Option Strict On 放在代码的顶部将强制使用正确的类型。 VB 默认情况下允许您在整数和字符串之间松散地转换,这是不好的做法。

Option Strict On

Public Class Form1

    Private count As Integer

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        count = 0
        Timer1.Enabled = True
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        If count = 10 Then
            Dim randomnumber As New Random()
            Dim num1 = randomnumber.Next(100, 201)
            TB2.Text = num1.ToString()
        End If
        Timer1.Enabled = True

    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        count += 1
        Timer1.Enabled = count <> 10
        TB1.Text = count.ToString()
    End Sub

End Class