VB.Net Tutorial/GUI/Timer
Содержание
Scrolling Text by Timer
<source lang="vbnet">Imports System.Windows.Forms public class ScrollingText
public Shared Sub Main Application.Run(New Form1) End Sub
End class Public Class Form1
Inherits System.Windows.Forms.Form Public Sub New() MyBase.New() InitializeComponent() End Sub 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 Private components As System.ruponentModel.IContainer Friend WithEvents Label1 As System.Windows.Forms.Label Friend WithEvents Button1 As System.Windows.Forms.Button Friend WithEvents Button2 As System.Windows.Forms.Button Friend WithEvents Button3 As System.Windows.Forms.Button Friend WithEvents Timer1 As System.Windows.Forms.Timer <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent() Me.ruponents = New System.ruponentModel.Container Me.Label1 = New System.Windows.Forms.Label Me.Button1 = New System.Windows.Forms.Button Me.Button2 = New System.Windows.Forms.Button Me.Button3 = New System.Windows.Forms.Button Me.Timer1 = New System.Windows.Forms.Timer(Me.ruponents) Me.SuspendLayout() " "Label1 " Me.Label1.Font = New System.Drawing.Font("Microsoft Sans Serif", 15.75!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte)) Me.Label1.Location = New System.Drawing.Point(32, 32) Me.Label1.Name = "Label1" Me.Label1.Size = New System.Drawing.Size(216, 24) Me.Label1.TabIndex = 0 Me.Label1.Text = "Scrolling Text" " "Button1 " Me.Button1.Location = New System.Drawing.Point(24, 80) Me.Button1.Name = "Button1" Me.Button1.Size = New System.Drawing.Size(72, 24) Me.Button1.TabIndex = 1 Me.Button1.Text = "Start" " "Button2 " Me.Button2.Location = New System.Drawing.Point(112, 80) Me.Button2.Name = "Button2" Me.Button2.Size = New System.Drawing.Size(64, 24) Me.Button2.TabIndex = 2 Me.Button2.Text = "Stop" " "Button3 " Me.Button3.Location = New System.Drawing.Point(192, 80) Me.Button3.Name = "Button3" Me.Button3.Size = New System.Drawing.Size(72, 24) Me.Button3.TabIndex = 3 Me.Button3.Text = "Exit" " "Timer1 " Me.Timer1.Interval = 300 " "Form1 " Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13) Me.ClientSize = New System.Drawing.Size(292, 150) Me.Controls.Add(Me.Button3) Me.Controls.Add(Me.Button2) Me.Controls.Add(Me.Button1) Me.Controls.Add(Me.Label1) Me.Name = "Form1" Me.Text = "Scroll Text" Me.ResumeLayout(False) End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Do While Label1.Text.Length < 13 Label1.Text = Label1.Text & " " Loop Label1.AutoSize = True Timer1.Start() End Sub Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click Timer1.Stop() End Sub Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click End End Sub Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick Label1.Text = Microsoft.VisualBasic.Right(Label1.Text, 1) & Microsoft.VisualBasic.Left(Label1.Text, Label1.Text.Length - 1) End Sub
End Class</source>
Timer event
<source lang="vbnet">Imports System.Windows.Forms public class TimerEvent
public Shared Sub Main Application.Run(New Form1) End Sub
End class Public Class Form1
Inherits System.Windows.Forms.Form Public Sub New() MyBase.New() InitializeComponent() End Sub 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 Private components As System.ruponentModel.IContainer Friend WithEvents Label1 As System.Windows.Forms.Label Friend WithEvents Label2 As System.Windows.Forms.Label Friend WithEvents Timer1 As System.Windows.Forms.Timer Friend WithEvents TextBox1 As System.Windows.Forms.TextBox Friend WithEvents TextBox2 As System.Windows.Forms.TextBox <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent() Me.ruponents = New System.ruponentModel.Container Me.Label1 = New System.Windows.Forms.Label Me.Label2 = New System.Windows.Forms.Label Me.Timer1 = New System.Windows.Forms.Timer(Me.ruponents) Me.TextBox1 = New System.Windows.Forms.TextBox Me.TextBox2 = New System.Windows.Forms.TextBox Me.SuspendLayout() " "Label1 " Me.Label1.Location = New System.Drawing.Point(16, 24) Me.Label1.Name = "Label1" Me.Label1.Size = New System.Drawing.Size(112, 24) Me.Label1.TabIndex = 0 Me.Label1.Text = "Systen Date" " "Label2 " Me.Label2.Location = New System.Drawing.Point(16, 80) Me.Label2.Name = "Label2" Me.Label2.Size = New System.Drawing.Size(112, 24) Me.Label2.TabIndex = 1 Me.Label2.Text = "System Time" " "Timer1 " " "TextBox1 " Me.TextBox1.Location = New System.Drawing.Point(112, 16) Me.TextBox1.Name = "TextBox1" Me.TextBox1.Size = New System.Drawing.Size(104, 20) Me.TextBox1.TabIndex = 2 Me.TextBox1.Text = "" " "TextBox2 " Me.TextBox2.Location = New System.Drawing.Point(112, 72) Me.TextBox2.Name = "TextBox2" Me.TextBox2.Size = New System.Drawing.Size(104, 20) Me.TextBox2.TabIndex = 3 Me.TextBox2.Text = "" " "Form1 " Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13) Me.ClientSize = New System.Drawing.Size(292, 158) Me.Controls.Add(Me.TextBox2) Me.Controls.Add(Me.TextBox1) Me.Controls.Add(Me.Label2) Me.Controls.Add(Me.Label1) Me.Name = "Form1" Me.Text = "Form1" Me.ResumeLayout(False) End Sub Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick Dim TDay As String Dim i As Short TextBox2.Text = CStr(TimeOfDay) i = Weekday(Today) Select Case i Case 1 TDay = "Sunday" Case 2 TDay = "Monday" Case 3 TDay = "Tuesday" Case 4 TDay = "Wednesday" Case 5 TDay = "Thursday" Case 6 TDay = "Friday" Case 7 TDay = "Saturday" End Select TextBox1.Text = TDay & "," & Today End Sub Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Me.Timer1.Start() End Sub
End Class</source>
Timer interval
<source lang="vbnet">imports System imports System.Drawing imports System.Windows.Forms public class Timers : inherits Form
dim lblTime as Label public sub New() Size = new Size(300,100) lblTime = new Label() lblTime.Parent = me lblTime.Size = new Size(200, 25) lblTime.Location = new Point(20,20) lblTime.BorderStyle = BorderStyle.FixedSingle lblTime.TextAlign = ContentAlignment.MiddleCenter dim t as new Timer() t.Interval = 100 t.Start() AddHandler t.Tick, AddressOf t_Tick end sub public shared sub Main() Application.Run(new Timers()) end sub private sub t_Tick(ByVal sender as object,ByVal e as EventArgs) lblTime.Text = DateTime.Now.ToString("dddd, MMMM d, yyyy h:mm:ss tt") end sub
end class</source>
Timer Start and Stop
<source lang="vbnet">Imports System.Windows.Forms public class ScrollingText
public Shared Sub Main Application.Run(New Form1) End Sub
End class Public Class Form1
Inherits System.Windows.Forms.Form Public Sub New() MyBase.New() InitializeComponent() End Sub 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 Private components As System.ruponentModel.IContainer Friend WithEvents Label1 As System.Windows.Forms.Label Friend WithEvents Button1 As System.Windows.Forms.Button Friend WithEvents Button2 As System.Windows.Forms.Button Friend WithEvents Button3 As System.Windows.Forms.Button Friend WithEvents Timer1 As System.Windows.Forms.Timer <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent() Me.ruponents = New System.ruponentModel.Container Me.Label1 = New System.Windows.Forms.Label Me.Button1 = New System.Windows.Forms.Button Me.Button2 = New System.Windows.Forms.Button Me.Button3 = New System.Windows.Forms.Button Me.Timer1 = New System.Windows.Forms.Timer(Me.ruponents) Me.SuspendLayout() " "Label1 " Me.Label1.Font = New System.Drawing.Font("Microsoft Sans Serif", 15.75!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte)) Me.Label1.Location = New System.Drawing.Point(32, 32) Me.Label1.Name = "Label1" Me.Label1.Size = New System.Drawing.Size(216, 24) Me.Label1.TabIndex = 0 Me.Label1.Text = "Scrolling Text" " "Button1 " Me.Button1.Location = New System.Drawing.Point(24, 80) Me.Button1.Name = "Button1" Me.Button1.Size = New System.Drawing.Size(72, 24) Me.Button1.TabIndex = 1 Me.Button1.Text = "Start" " "Button2 " Me.Button2.Location = New System.Drawing.Point(112, 80) Me.Button2.Name = "Button2" Me.Button2.Size = New System.Drawing.Size(64, 24) Me.Button2.TabIndex = 2 Me.Button2.Text = "Stop" " "Button3 " Me.Button3.Location = New System.Drawing.Point(192, 80) Me.Button3.Name = "Button3" Me.Button3.Size = New System.Drawing.Size(72, 24) Me.Button3.TabIndex = 3 Me.Button3.Text = "Exit" " "Timer1 " Me.Timer1.Interval = 300 " "Form1 " Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13) Me.ClientSize = New System.Drawing.Size(292, 150) Me.Controls.Add(Me.Button3) Me.Controls.Add(Me.Button2) Me.Controls.Add(Me.Button1) Me.Controls.Add(Me.Label1) Me.Name = "Form1" Me.Text = "Scroll Text" Me.ResumeLayout(False) End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Do While Label1.Text.Length < 13 Label1.Text = Label1.Text & " " Loop Label1.AutoSize = True Timer1.Start() End Sub Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click Timer1.Stop() End Sub Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click End End Sub Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick Label1.Text = Microsoft.VisualBasic.Right(Label1.Text, 1) & Microsoft.VisualBasic.Left(Label1.Text, Label1.Text.Length - 1) End Sub
End Class</source>
Use three timers
<source lang="vbnet">Imports System.Windows.Forms
public class ThreeTimers
public Shared Sub Main Application.Run(New Form1) End Sub
End class Public Class Form1
Private Sub tmrTenthSecond_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmrTenthSecond.Tick lblTenthSecond.Text = Now.ToString("hh:mm:ss.f") End Sub Private Sub tmrSecond_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmrSecond.Tick lblSeconds.Text = Now.ToString("hh:mm:ss") End Sub Private Sub tmrTenSeconds_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmrTenSeconds.Tick lblTenSeconds.Text = Now.ToString("hh:mm:ss") End Sub " Display initial times. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load lblTenthSecond.Text = Now.ToString("hh:mm:ss.f") lblSeconds.Text = Now.ToString("hh:mm:ss") lblTenSeconds.Text = Now.ToString("hh:mm:ss") End Sub
End Class <Global.Microsoft.VisualBasic.rupilerServices.DesignerGenerated()> _ Partial Public Class Form1
Inherits System.Windows.Forms.Form "Form overrides dispose to clean up the component list. <System.Diagnostics.DebuggerNonUserCode()> _ Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean) If disposing AndAlso components IsNot Nothing Then components.Dispose() End If MyBase.Dispose(disposing) End Sub "Required by the Windows Form Designer Private components As System.ruponentModel.IContainer "NOTE: The following procedure is required by the Windows Form Designer "It can be modified using the Windows Form Designer. "Do not modify it using the code editor. <System.Diagnostics.DebuggerStepThrough()> _ Private Sub InitializeComponent() Me.ruponents = New System.ruponentModel.Container Me.tmrTenthSecond = New System.Windows.Forms.Timer(Me.ruponents) Me.tmrSecond = New System.Windows.Forms.Timer(Me.ruponents) Me.tmrTenSeconds = New System.Windows.Forms.Timer(Me.ruponents) Me.Label1 = New System.Windows.Forms.Label Me.Label2 = New System.Windows.Forms.Label Me.Label3 = New System.Windows.Forms.Label Me.lblTenthSecond = New System.Windows.Forms.Label Me.lblSeconds = New System.Windows.Forms.Label Me.lblTenSeconds = New System.Windows.Forms.Label Me.SuspendLayout() " "tmrTenthSecond " Me.tmrTenthSecond.Enabled = True " "tmrSecond " Me.tmrSecond.Enabled = True Me.tmrSecond.Interval = 1000 " "tmrTenSeconds " Me.tmrTenSeconds.Enabled = True Me.tmrTenSeconds.Interval = 10000 " "Label1 " Me.Label1.AutoSize = True Me.Label1.Location = New System.Drawing.Point(8, 8) Me.Label1.Name = "Label1" Me.Label1.Size = New System.Drawing.Size(46, 13) Me.Label1.TabIndex = 0 Me.Label1.Text = "1/10 sec" " "Label2 " Me.Label2.AutoSize = True Me.Label2.Location = New System.Drawing.Point(8, 32) Me.Label2.Name = "Label2" Me.Label2.Size = New System.Drawing.Size(29, 13) Me.Label2.TabIndex = 1 Me.Label2.Text = "1 sec" " "Label3 " Me.Label3.AutoSize = True Me.Label3.Location = New System.Drawing.Point(8, 56) Me.Label3.Name = "Label3" Me.Label3.Size = New System.Drawing.Size(35, 13) Me.Label3.TabIndex = 2 Me.Label3.Text = "10 sec" " "lblTenthSecond " Me.lblTenthSecond.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D Me.lblTenthSecond.Location = New System.Drawing.Point(72, 8) Me.lblTenthSecond.Name = "lblTenthSecond" Me.lblTenthSecond.Size = New System.Drawing.Size(80, 16) Me.lblTenthSecond.TabIndex = 3 " "lblSeconds " Me.lblSeconds.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D Me.lblSeconds.Location = New System.Drawing.Point(72, 32) Me.lblSeconds.Name = "lblSeconds" Me.lblSeconds.Size = New System.Drawing.Size(80, 16) Me.lblSeconds.TabIndex = 4 " "lblTenSeconds " Me.lblTenSeconds.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D Me.lblTenSeconds.Location = New System.Drawing.Point(72, 56) Me.lblTenSeconds.Name = "lblTenSeconds" Me.lblTenSeconds.Size = New System.Drawing.Size(80, 16) Me.lblTenSeconds.TabIndex = 5 " "Form1 " Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!) Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font Me.ClientSize = New System.Drawing.Size(167, 88) Me.Controls.Add(Me.lblTenSeconds) Me.Controls.Add(Me.lblSeconds) Me.Controls.Add(Me.lblTenthSecond) Me.Controls.Add(Me.Label3) Me.Controls.Add(Me.Label2) Me.Controls.Add(Me.Label1) Me.Name = "Form1" Me.Text = "UseTimer" Me.ResumeLayout(False) Me.PerformLayout() End Sub Friend WithEvents tmrTenthSecond As System.Windows.Forms.Timer Friend WithEvents tmrSecond As System.Windows.Forms.Timer Friend WithEvents tmrTenSeconds As System.Windows.Forms.Timer Friend WithEvents Label1 As System.Windows.Forms.Label Friend WithEvents Label2 As System.Windows.Forms.Label Friend WithEvents Label3 As System.Windows.Forms.Label Friend WithEvents lblTenthSecond As System.Windows.Forms.Label Friend WithEvents lblSeconds As System.Windows.Forms.Label Friend WithEvents lblTenSeconds As System.Windows.Forms.Label
End Class</source>
Use Timer to refresh Label
<source lang="vbnet">Imports System.Windows.Forms <Global.Microsoft.VisualBasic.rupilerServices.DesignerGenerated()> _ Partial Class Form1
Inherits System.Windows.Forms.Form "Form overrides dispose to clean up the component list. <System.Diagnostics.DebuggerNonUserCode()> _ Protected Overrides Sub Dispose(ByVal disposing As Boolean) If disposing AndAlso components IsNot Nothing Then components.Dispose() End If MyBase.Dispose(disposing) End Sub "Required by the Windows Form Designer Private components As System.ruponentModel.IContainer "NOTE: The following procedure is required by the Windows Form Designer "It can be modified using the Windows Form Designer. "Do not modify it using the code editor. <System.Diagnostics.DebuggerStepThrough()> _ Private Sub InitializeComponent() Me.ruponents = New System.ruponentModel.Container Me.CurrentTime = New System.Windows.Forms.Label Me.ClockTimer = New System.Windows.Forms.Timer(Me.ruponents) Me.SuspendLayout() " "CurrentTime " Me.CurrentTime.AutoSize = True Me.CurrentTime.Location = New System.Drawing.Point(8, 8) Me.CurrentTime.Name = "CurrentTime" Me.CurrentTime.Size = New System.Drawing.Size(39, 13) Me.CurrentTime.TabIndex = 0 Me.CurrentTime.Text = "Label1" " "ClockTimer " Me.ClockTimer.Enabled = True Me.ClockTimer.Interval = 1000 " "Form1 " Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!) Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font Me.ClientSize = New System.Drawing.Size(292, 35) Me.Controls.Add(Me.CurrentTime) Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle Me.MaximizeBox = False Me.Name = "Form1" Me.Text = "Clock Timer" Me.ResumeLayout(False) Me.PerformLayout() End Sub Friend WithEvents CurrentTime As System.Windows.Forms.Label Friend WithEvents ClockTimer As System.Windows.Forms.Timer
End Class Public Class Form1
Private Sub ClockTimer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ClockTimer.Tick CurrentTime.Text = Now.ToLongTimeString End Sub
End Class
public class ClockTimerDemo
public Shared Sub Main Application.Run(New Form1) End Sub
End class</source>