您的位置:首页 > 其它

一个关于委托事件的例子(计时器)

2005-11-29 00:15 405 查看
Public Class Form1
Inherits System.Windows.Forms.Form

#Region " Windows 窗体设计器生成的代码 "

Public Sub New()
MyBase.New()

'该调用是 Windows 窗体设计器所必需的。
InitializeComponent()

'在 InitializeComponent() 调用之后添加任何初始化

End Sub

'窗体重写 dispose 以清理组件列表。
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub

'Windows 窗体设计器所必需的
Private components As System.ComponentModel.IContainer

'注意: 以下过程是 Windows 窗体设计器所必需的
'可以使用 Windows 窗体设计器修改此过程。
'不要使用代码编辑器修改它。
Friend WithEvents WebClient1 As System.Net.WebClient
Friend WithEvents Button1 As System.Windows.Forms.Button
Friend WithEvents Button2 As System.Windows.Forms.Button
Friend WithEvents Button3 As System.Windows.Forms.Button
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.WebClient1 = New System.Net.WebClient
Me.Button1 = New System.Windows.Forms.Button
Me.Button2 = New System.Windows.Forms.Button
Me.Button3 = New System.Windows.Forms.Button
Me.SuspendLayout()
'
'WebClient1
'
Me.WebClient1.BaseAddress = ""
Me.WebClient1.Credentials = Nothing
'
'Button1
'
Me.Button1.Location = New System.Drawing.Point(40, 8)
Me.Button1.Name = "Button1"
Me.Button1.Size = New System.Drawing.Size(256, 23)
Me.Button1.TabIndex = 0
Me.Button1.Text = "开始计时"
'
'Button2
'
Me.Button2.Enabled = False
Me.Button2.FlatStyle = System.Windows.Forms.FlatStyle.Flat
Me.Button2.Location = New System.Drawing.Point(40, 88)
Me.Button2.Name = "Button2"
Me.Button2.Size = New System.Drawing.Size(256, 23)
Me.Button2.TabIndex = 1
Me.Button2.Text = "00:00:00.00"
'
'Button3
'
Me.Button3.Location = New System.Drawing.Point(40, 48)
Me.Button3.Name = "Button3"
Me.Button3.Size = New System.Drawing.Size(256, 23)
Me.Button3.TabIndex = 2
Me.Button3.Text = "结束计时"
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(6, 14)
Me.ClientSize = New System.Drawing.Size(392, 130)
Me.Controls.Add(Me.Button3)
Me.Controls.Add(Me.Button2)
Me.Controls.Add(Me.Button1)
Me.Font = New System.Drawing.Font("Arial", 9.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedToolWindow
Me.MaximizeBox = False
Me.MinimizeBox = False
Me.Name = "Form1"
Me.Text = "计时器"
Me.ResumeLayout(False)

End Sub

#End Region

Dim timer1 As New System.Timers.Timer
Dim ms As Integer = 0
Dim h As Integer = 0
Dim m As Integer = 0
Dim s As Integer = 0
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.Button2.Text = h.ToString("00") & ":" & m.ToString("00") & ":" & s.ToString("00") & "." & ms.ToString("00")
timer1.Interval = 10
timer1.Enabled = True
Me.Button3.Enabled = False
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
ms = 0 : h = 0 : m = 0 : s = 0
AddHandler timer1.Elapsed, AddressOf mydelegate

Me.Button1.Enabled = False
Me.Button3.Enabled = True
End Sub

Private Sub mydelegate(ByVal sender As System.Object, ByVal e As System.Timers.ElapsedEventArgs)
ms += 1
If ms >= 100 Then
ms -= 100
s += 1
If s >= 60 Then
s -= 60
m += 1
If m >= 60 Then
m -= 60
h += 1
End If
End If
End If
Me.Button2.Text = h.ToString("00") & ":" & m.ToString("00") & ":" & s.ToString("00") & "." & ms.ToString("00")
End Sub

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
RemoveHandler timer1.Elapsed, AddressOf Me.mydelegate
Me.Button3.Enabled = False
Me.Button1.Enabled = True
End Sub

End Class

运行结果如下:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: