User-defined control with a timer and a label
Imports System
Imports System.Drawing
Imports System.Windows.Forms
Public Class MainClass
Shared Sub Main()
Dim myform As Form = New FrmClock()
Application.Run(myform)
End Sub ' Main
End Class
Public Class FrmClock
Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
End Sub
'Form overrides dispose to clean up the component list.
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
Friend WithEvents myCClockUserControl As CClockUserControl
'Required by the Windows Form Designer
Private components As System.ComponentModel.Container
'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.myCClockUserControl = New CClockUserControl()
Me.SuspendLayout()
'
'myCClockUserControl
'
Me.myCClockUserControl.Location = New System.Drawing.Point(24, 24)
Me.myCClockUserControl.Name = "myCClockUserControl"
Me.myCClockUserControl.Size = New System.Drawing.Size(96, 48)
Me.myCClockUserControl.TabIndex = 0
'
'FrmClock
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(144, 93)
Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.myCClockUserControl})
Me.Name = "FrmClock"
Me.Text = "Clock"
Me.ResumeLayout(False)
End Sub
#End Region
End Class
' create a clock control that inherits from UserControl
Public Class CClockUserControl
Inherits UserControl
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
End Sub
'UserControl overrides dispose to clean up the component list.
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
' displays time
Friend WithEvents lblDisplay As Label
' non-visible event-triggering timer object
Friend WithEvents tmrClock As Timer
'Required by the Windows Form Designer
Private components As System.ComponentModel.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.components = New System.ComponentModel.Container()
Me.tmrClock = New System.Windows.Forms.Timer(Me.components)
Me.lblDisplay = New System.Windows.Forms.Label()
Me.SuspendLayout()
'
'tmrClock
'
Me.tmrClock.Enabled = True
'
'lblDisplay
'
Me.lblDisplay.Dock = System.Windows.Forms.DockStyle.Fill
Me.lblDisplay.Font = New System.Drawing.Font("Microsoft Sans Serif", 12.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.lblDisplay.Name = "lblDisplay"
Me.lblDisplay.Size = New System.Drawing.Size(150, 72)
Me.lblDisplay.TabIndex = 0
Me.lblDisplay.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
'
'CClockUserControl
'
Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.lblDisplay})
Me.Name = "CClockUserControl"
Me.Size = New System.Drawing.Size(150, 72)
Me.ResumeLayout(False)
End Sub
#End Region
' update label at every tick
Private Sub tmrClock_Tick(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles tmrClock.Tick
' get current time (Now), converto to string
lblDisplay.Text = DateTime.Now.ToLongTimeString
End Sub
End Class
Related examples in the same category