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

Home
VB.Net
1.2D
2.Application
3.Class
4.Data Structure
5.Data Types
6.Database ADO.net
7.Date Time
8.Development
9.Event
10.File Directory
11.Generics
12.GUI
13.Internationalization I18N
14.Language Basics
15.LINQ
16.Network Remote
17.Reflection
18.Security
19.Thread
20.Windows Presentation Foundation
21.Windows System
22.XML
23.XML LINQ
VB.Net » GUI » User Control 




User-defined control with a timer and a label
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 NothingThen
            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(2424)
      Me.myCClockUserControl.Name = "myCClockUserControl"
      Me.myCClockUserControl.Size = New System.Drawing.Size(9648)
      Me.myCClockUserControl.TabIndex = 0
      '
      'FrmClock
      '
      Me.AutoScaleBaseSize = New System.Drawing.Size(513)
      Me.ClientSize = New System.Drawing.Size(14493)
      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 NothingThen
            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(15072)
      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(15072)
      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.EventArgsHandles tmrClock.Tick

      ' get current time (Now), converto to string
      lblDisplay.Text = DateTime.Now.ToLongTimeString
   End Sub

End Class 
           
       














Related examples in the same category
1.Sub class UserControl to create your own controlSub class UserControl to create your own control
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.