用VB.net里的Timer控件动态生成随机数,在点击Button2停止Timer控件时,在软件界面中的三个Label会有跳码的现象,很不解,为此,我在网上找了一段截屏的程序在对每次停止Timer控件时,截取屏幕的图,进行对比,发现截屏后,三个Label控件显示的值与软件界面上显示的三个Label的值有时不一致,有时一致的现象,请问各位高手,这是怎么回事呢?
附代码:
Public Class Form1 Private Sub CatchLDDRezult()
Dim p1 As New Point(0, 0)
Dim p2 As New Point(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height)
Dim pic As New Bitmap(p2.X, p2.Y)
Using g As Graphics = Graphics.FromImage(pic)
g.CopyFromScreen(p1, p1, p2)
pic.Save("D:\1.jpg")
End Using
End Sub Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Timer1.Enabled = True
End Sub Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Timer1.Enabled = False
CatchLDDRezult()
End Sub Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Randomize()
Dim intRndLDid As Integer = Int(Rnd() * 941 + 1)
Dim strRndLDid As String = Format(intRndLDid, "0000")
Label1.Text = strRndLDid Randomize()
intRndLDid = Int(Rnd() * 941 + 1)
strRndLDid = Format(intRndLDid, "0000")
Label2.Text = strRndLDid Randomize()
intRndLDid = Int(Rnd() * 941 + 1)
strRndLDid = Format(intRndLDid, "0000")
Label3.Text = strRndLDid Label4.Text = Now
End Sub
End Class
附代码:
Public Class Form1 Private Sub CatchLDDRezult()
Dim p1 As New Point(0, 0)
Dim p2 As New Point(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height)
Dim pic As New Bitmap(p2.X, p2.Y)
Using g As Graphics = Graphics.FromImage(pic)
g.CopyFromScreen(p1, p1, p2)
pic.Save("D:\1.jpg")
End Using
End Sub Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Timer1.Enabled = True
End Sub Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Timer1.Enabled = False
CatchLDDRezult()
End Sub Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Randomize()
Dim intRndLDid As Integer = Int(Rnd() * 941 + 1)
Dim strRndLDid As String = Format(intRndLDid, "0000")
Label1.Text = strRndLDid Randomize()
intRndLDid = Int(Rnd() * 941 + 1)
strRndLDid = Format(intRndLDid, "0000")
Label2.Text = strRndLDid Randomize()
intRndLDid = Int(Rnd() * 941 + 1)
strRndLDid = Format(intRndLDid, "0000")
Label3.Text = strRndLDid Label4.Text = Now
End Sub
End Class
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货