User-defined control with a timer and a label : Custom Control « GUI « VB.Net Tutorial






User-defined control with a timer and a label
Imports System.Windows.Forms

public class UserControlDefinition
   public Shared Sub Main
        Application.Run(New FrmClock)
   End Sub
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 myClockControl As ClockControl
   
   '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.myClockControl = New ClockControl()
      Me.SuspendLayout()
      '
      'myClockControl
      '
      Me.myClockControl.Location = New System.Drawing.Point(24, 24)
      Me.myClockControl.Name = "myClockControl"
      Me.myClockControl.Size = New System.Drawing.Size(96, 48)
      Me.myClockControl.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.myClockControl})
      Me.Name = "FrmClock"
      Me.Text = "Clock"
      Me.ResumeLayout(False)

   End Sub

#End Region

End Class


Public Class ClockControl
   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.BackColor = System.Drawing.Color.White
      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
      '
      'ClockControl
      '
      Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.lblDisplay})
      Me.Name = "ClockControl"
      Me.Size = New System.Drawing.Size(150, 72)
      Me.ResumeLayout(False)

   End Sub

#End Region
   Private Sub tmrClock_Tick(ByVal sender As System.Object, _
      ByVal e As System.EventArgs) Handles tmrClock.Tick

      lblDisplay.Text = DateTime.Now.ToLongTimeString
   End Sub

End Class








14.78.Custom Control
14.78.1.Create user controlCreate user control
14.78.2.Paint ControlPaint Control
14.78.3.User-defined control with a timer and a labelUser-defined control with a timer and a label
14.78.4.Custom paint ButtonCustom paint Button
14.78.5.Friend ControlFriend Control