Define user control : User Control « GUI Windows Form « C# / C Sharp






Define user control

Define user control
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

public class Form1 : Form
{

    private ClockUserControl clockUserControl1;

  public Form1() {
        InitializeComponent();
  }


      private void InitializeComponent()
      {
         this.clockUserControl1 = new ClockUserControl();
         this.SuspendLayout();
         // 
         // clockUserControl1
         // 
         this.clockUserControl1.Location = new System.Drawing.Point(12, 12);
         this.clockUserControl1.Name = "clockUserControl1";
         this.clockUserControl1.Size = new System.Drawing.Size(154, 74);
         this.clockUserControl1.TabIndex = 0;
         // 
         // Clock
         // 
         this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
         this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
         this.ClientSize = new System.Drawing.Size(178, 99);
         this.Controls.Add(this.clockUserControl1);
         this.Name = "Clock";
         this.Text = "Clock";
         this.ResumeLayout(false);

      }
  [STAThread]
  static void Main()
  {
    Application.EnableVisualStyles();
    Application.Run(new Form1());
  }

}


public class ClockUserControl : UserControl
{
      private System.Windows.Forms.Label displayLabel;
      private System.Windows.Forms.Timer clockTimer;
      
      public ClockUserControl() {
         InitializeComponent();
      }  

      private void clockTimer_Tick(object sender, EventArgs e) {
         displayLabel.Text = DateTime.Now.ToLongTimeString();
      }
      private void InitializeComponent()
      {
         this.displayLabel = new System.Windows.Forms.Label();
         this.clockTimer = new System.Windows.Forms.Timer( new System.ComponentModel.Container() );
         this.SuspendLayout();
         // 
         // displayLabel
         // 
         this.displayLabel.BackColor = System.Drawing.SystemColors.ActiveCaptionText;
         this.displayLabel.Font = new System.Drawing.Font( "Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ( ( byte ) ( 0 ) ) );
         this.displayLabel.Location = new System.Drawing.Point( 4, 10 );
         this.displayLabel.Name = "displayLabel";
         this.displayLabel.Size = new System.Drawing.Size( 143, 52 );
         this.displayLabel.TabIndex = 0;
         this.displayLabel.Text = "12:55:55 AM";
         this.displayLabel.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
         // 
         // clockTimer
         // 
         this.clockTimer.Enabled = true;
         this.clockTimer.Interval = 1000;
         this.clockTimer.Tick += new System.EventHandler( this.clockTimer_Tick );
         // 
         // ClockUserControl
         // 
         this.AutoScaleDimensions = new System.Drawing.SizeF( 6F, 13F );
         this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
         this.BackColor = System.Drawing.Color.White;
         this.Controls.Add( this.displayLabel );
         this.Name = "ClockUserControl";
         this.Size = new System.Drawing.Size( 150, 72 );
         this.ResumeLayout( false );

      }

}


           
       








Related examples in the same category

1.subclass System.Windows.Forms.UserControl to create custom control
2.Draw a Custom Control
3.Creating an event for a component.
4.The read-only property control.