通常我们使用VB,VB.NET,C#,VC等都是"画" 写代码然后编译
以VB为例,我们使用一个计时器按秒数计时,通常的步骤是:
1.画一个Timer 画一个Label 画一个Command
2.把属性设置好,或者在Form_Load()等地方初始化
下面演示一种新的设计方法,什么都不画,直接在窗体添加这些代码:
Option Explicit Private WithEvents cmdCommand As CommandButton Private WithEvents lblPrint As Label Private WithEvents tmrCount As Timer Private lCount As Long Private Sub cmdCommand_Click() tmrCount.Enabled = Not tmrCount.Enabled If tmrCount.Enabled = False Then cmdCommand.Caption = "开始(&S)" Else cmdCommand.Caption = "停止(&T)" End If End Sub Private Sub Form_Load() Me.Caption = "Pure Code Demo" Me.Height = 3600 Me.Width = 4800 Me.ScaleMode = 1 Set tmrCount = Controls.Add("VB.Timer","tmrCount") Set lblPrint = Controls.Add("VB.Label","lblPrint") Set cmdCommand = Controls.Add("VB.CommandButton","cmdCommand") With tmrCount .Interval = 1000 '你每次计时的间隔,精确到毫秒,你可以设其他值 .Enabled = False End With With lblPrint .Left = 60 .Top = 60 .Width = 3315 .Height = 255 .Visible = True .Caption = "累计计时 " & CStr(lCount) & " 次" End With With cmdCommand .Left = 3420 .Top = 60 .Width = 1215 .Height = 255 .Visible = True .Caption = "开始(&S)" End With End Sub Private Sub tmrCount_Timer() On Error GoTo ErrHandle lCount = lCount + 1 lblPrint.Caption = "累计计时 " & CStr(lCount) & "次" Exit Sub ErrHandle: lCount = 0 tmrCount.Enabled = False cmdCommand.Caption = "开始(&S)" End Sub
运行看看是不是一样的效果?