Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
PictureBox1.Visible = False
Label1.Text = CStr(Int(Rnd() * 10))
Label2.Text = CStr(Int(Rnd() * 10))
Label3.Text = CStr(Int(Rnd() * 10))
If (Label1.Text = "9") Or (Label2.Text = "9") Or (Label3.Text = "9") Then
PictureBox1.Visible = True
Beep()
End If
End Sub
และผมได้ลองใส่ลูป while และ delay เพื่อจะให้แสดงเลขระหว่างการสุ่มก็ไม่ออกครับ มันจะแสดงครั้งเดียวตอนสุ่มเจอเลขที่ต้องการแล้วดังนี้
Code (VB.NET)
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
PictureBox1.Visible = False
While (1)
Label1.Text = CStr(Int(Rnd() * 10))
Label2.Text = CStr(Int(Rnd() * 10))
Label3.Text = CStr(Int(Rnd() * 10))
If (Label1.Text = "9") Or (Label2.Text= "9") Or (Label3.Text = "9") Then
PictureBox1.Visible = True
Beep()
Label1.ForeColor = Color.Red
Label2.ForeColor = Color.Red
Label3.ForeColor = Color.Red
System.Threading.Thread.Sleep(200)
Exit While
ElseIf (Label1.Text <> "9") Or (Label2.Text <> "9") Or (Label3.Text <> "9") Then
Label1.ForeColor = Color.Black
Label2.ForeColor = Color.Black
Label3.ForeColor = Color.Black
System.Threading.Thread.Sleep(200)
Continue While
End If
End While
End Sub
Public Class T00_Random
Private Sub btnStart_Click(sender As System.Object, e As System.EventArgs) Handles btnStart.Click
Timer1.Start()
End Sub
Private Sub btnStop_Click(sender As System.Object, e As System.EventArgs) Handles btnStop.Click
Timer1.Stop()
End Sub
Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick
lbl_Random.Text = Rnd() * 1000
txt_Random.Text = Rnd() * 1000
End Sub
End Class