ListBox: Binding Unusual Properties


   

/*
User Interfaces in C#: Windows Forms and Custom Controls
by Matthew MacDonald

Publisher: Apress
ISBN: 1590590457
*/

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Drawing.Text;

namespace DataBinding101
{
    /// <summary>
    /// Summary description for BindingUnusualProperties.
    /// </summary>
    public class BindingUnusualProperties : System.Windows.Forms.Form
    {
        internal System.Windows.Forms.Label Label2;
        internal System.Windows.Forms.Label Label1;
        internal System.Windows.Forms.ListBox lstFonts;
        internal System.Windows.Forms.Label lblSampleText;
        internal System.Windows.Forms.ListBox lstColors;
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.Container components = null;

        public BindingUnusualProperties()
        {
            //
            // Required for Windows Form Designer support
            //
            InitializeComponent();

            //
            // TODO: Add any constructor code after InitializeComponent call
            //
        }

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        protected override void Dispose( bool disposing )
        {
            if( disposing )
            {
                if(components != null)
                {
                    components.Dispose();
                }
            }
            base.Dispose( disposing );
        }

        #region Windows Form Designer generated code
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.Label2 = new System.Windows.Forms.Label();
            this.Label1 = new System.Windows.Forms.Label();
            this.lstFonts = new System.Windows.Forms.ListBox();
            this.lblSampleText = new System.Windows.Forms.Label();
            this.lstColors = new System.Windows.Forms.ListBox();
            this.SuspendLayout();
            // 
            // Label2
            // 
            this.Label2.Location = new System.Drawing.Point(210, 13);
            this.Label2.Name = "Label2";
            this.Label2.Size = new System.Drawing.Size(140, 12);
            this.Label2.TabIndex = 9;
            this.Label2.Text = "Choose a Font:";
            // 
            // Label1
            // 
            this.Label1.Location = new System.Drawing.Point(14, 13);
            this.Label1.Name = "Label1";
            this.Label1.Size = new System.Drawing.Size(140, 12);
            this.Label1.TabIndex = 8;
            this.Label1.Text = "Choose a Color:";
            // 
            // lstFonts
            // 
            this.lstFonts.Location = new System.Drawing.Point(210, 29);
            this.lstFonts.Name = "lstFonts";
            this.lstFonts.Size = new System.Drawing.Size(180, 134);
            this.lstFonts.TabIndex = 7;
            // 
            // lblSampleText
            // 
            this.lblSampleText.Anchor = (((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
                | System.Windows.Forms.AnchorStyles.Left) 
                | System.Windows.Forms.AnchorStyles.Right);
            this.lblSampleText.Location = new System.Drawing.Point(18, 185);
            this.lblSampleText.Name = "lblSampleText";
            this.lblSampleText.Size = new System.Drawing.Size(372, 96);
            this.lblSampleText.TabIndex = 6;
            this.lblSampleText.Text = "Click an item in one of the lists above to change the font or color of this text." +
                " Once the initial conditions are set up (i.e., the binding), this operation happ" +
                "ens automatically.";
            // 
            // lstColors
            // 
            this.lstColors.Location = new System.Drawing.Point(14, 29);
            this.lstColors.Name = "lstColors";
            this.lstColors.Size = new System.Drawing.Size(176, 134);
            this.lstColors.TabIndex = 5;
            // 
            // BindingUnusualProperties
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 14);
            this.ClientSize = new System.Drawing.Size(404, 294);
            this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                                                          this.Label2,
                                                                          this.Label1,
                                                                          this.lstFonts,
                                                                          this.lblSampleText,
                                                                          this.lstColors});
            this.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
            this.Name = "BindingUnusualProperties";
            this.Text = "Binding Unusual Properties";
            this.Load += new System.EventHandler(this.BindingUnusualProperties_Load);
            this.ResumeLayout(false);

        }
        #endregion

        private void BindingUnusualProperties_Load(object sender, System.EventArgs e)
        {
            // These are our final data sources: two ArrayList objects.
            ArrayList fontObjList = new ArrayList();
            ArrayList colorObjList = new ArrayList();

            // The InstalledFonts collection allows us to enumerate installed fonts.
            // Each FontFamily needs to be converted to a genuine Font object
            // before it is suitable for data binding to the Control.Font property.
            InstalledFontCollection InstalledFonts = new InstalledFontCollection();
            foreach (FontFamily family in InstalledFonts.Families)
            {
                try
                {
                    fontObjList.Add(new Font(family, 12));
                }
                catch
                {
                    // We end up here if the font could not be created
                    // with the default style.
                }
            }

            // In order to retrieve the list of colors, we need to first retrieve
            // the strings for the KnownColor enumeration, and then convert each one
            // into a suitable color object.
            string[] colorNames;
            colorNames = System.Enum.GetNames(typeof(KnownColor));
            TypeConverter cnvrt = TypeDescriptor.GetConverter(typeof(KnownColor));

            foreach (string colorName in colorNames)
            {
                colorObjList.Add(Color.FromKnownColor((KnownColor)cnvrt.ConvertFromString(colorName)));
            }

            // We can now bind both our list controls.
            lstColors.DataSource = colorObjList;
            lstColors.DisplayMember = "Name";
            lstFonts.DataSource = fontObjList;
            lstFonts.DisplayMember = "Name";

            // The label is bound to both data sources.
            lblSampleText.DataBindings.Add("ForeColor", colorObjList, "");
            lblSampleText.DataBindings.Add("Font", fontObjList, "");

        }

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


           
          


Object ListBox Data Binding 2


   

/*
User Interfaces in C#: Windows Forms and Custom Controls
by Matthew MacDonald

Publisher: Apress
ISBN: 1590590457
*/
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;

namespace DataBinding101
{
    /// <summary>
    /// Summary description for ObjectListBinding2.
    /// </summary>
    public class ObjectListBinding2 : System.Windows.Forms.Form
    {
        internal System.Windows.Forms.ListBox lstCity;
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.Container components = null;

        public ObjectListBinding2()
        {
            //
            // Required for Windows Form Designer support
            //
            InitializeComponent();

            //
            // TODO: Add any constructor code after InitializeComponent call
            //
        }

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        protected override void Dispose( bool disposing )
        {
            if( disposing )
            {
                if(components != null)
                {
                    components.Dispose();
                }
            }
            base.Dispose( disposing );
        }

        #region Windows Form Designer generated code
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.lstCity = new System.Windows.Forms.ListBox();
            this.SuspendLayout();
            // 
            // lstCity
            // 
            this.lstCity.Anchor = (((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
                | System.Windows.Forms.AnchorStyles.Left) 
                | System.Windows.Forms.AnchorStyles.Right);
            this.lstCity.IntegralHeight = false;
            this.lstCity.Location = new System.Drawing.Point(4, 9);
            this.lstCity.Name = "lstCity";
            this.lstCity.Size = new System.Drawing.Size(248, 216);
            this.lstCity.TabIndex = 3;
            // 
            // ObjectListBinding2
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 14);
            this.ClientSize = new System.Drawing.Size(260, 234);
            this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                                                          this.lstCity});
            this.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
            this.Name = "ObjectListBinding2";
            this.Text = "ObjectListBinding2";
            this.Load += new System.EventHandler(this.ObjectListBinding2_Load);
            this.ResumeLayout(false);

        }
        #endregion

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

        private void ObjectListBinding2_Load(object sender, System.EventArgs e)
        {
            City2[] cityChoices = {new City2("Seattle", "U.S.A."), 
                                     new City2("New York", "U.S.A."), new City2("Tokyo", "Japan"), 
                                     new City2("Montreal", "Canada")};

            lstCity.DataSource = cityChoices;

        }
    
    }

    public class City2
    {
        private string name;
        private string country;

        public string Name
        {
            get
            {
                return name;
            }
            set
            {
                name = value;
            }
        }

        public string Country
        {
            get
            {
                return country;
            }
            set
            {
                country = value;
            }
        }

        public City2(string name, string country)
        {
            this.Name = name;
            this.Country = country;
        }

        public override string ToString()
        {
            return Name + ", " + Country;
        }

    }

}



           
          


Label grows as the inner text


   

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 System.Windows.Forms.GroupBox groupBox1;
    private System.Windows.Forms.Label label1;
    private System.Windows.Forms.TextBox textBox1;
    private System.Windows.Forms.Button cmdSetText;
    private System.Windows.Forms.Button cmdSetTextConstraint;

  public Form1() {
        InitializeComponent();
  }

    private void InitializeComponent(){
        this.groupBox1 = new System.Windows.Forms.GroupBox();
        this.label1 = new System.Windows.Forms.Label();
        this.textBox1 = new System.Windows.Forms.TextBox();
        this.cmdSetText = new System.Windows.Forms.Button();
        this.cmdSetTextConstraint = new System.Windows.Forms.Button();
        this.groupBox1.SuspendLayout();
        this.SuspendLayout();
        // 
        // groupBox1
        // 
        this.groupBox1.AutoSize = true;
        this.groupBox1.Controls.Add(this.label1);
        this.groupBox1.Location = new System.Drawing.Point(15, 133);
        this.groupBox1.Margin = new System.Windows.Forms.Padding(20);
        this.groupBox1.Name = "groupBox1";
        this.groupBox1.Size = new System.Drawing.Size(200, 100);
        this.groupBox1.TabIndex = 0;
        this.groupBox1.TabStop = false;
        this.groupBox1.Text = "groupBox1";
        // 
        // label1
        // 
        this.label1.AutoSize = true;
        this.label1.Location = new System.Drawing.Point(17, 25);
        this.label1.Margin = new System.Windows.Forms.Padding(5);
        this.label1.Name = "label1";
        this.label1.Size = new System.Drawing.Size(35, 13);
        this.label1.TabIndex = 0;
        this.label1.Text = "label1";
        // 
        // textBox1
        // 
        this.textBox1.Location = new System.Drawing.Point(15, 12);
        this.textBox1.Multiline = true;
        this.textBox1.Name = "textBox1";
        this.textBox1.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
        this.textBox1.Size = new System.Drawing.Size(200, 47);
        this.textBox1.TabIndex = 1;
        this.textBox1.Text = "The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the l" +
            "azy dog.";
        // 
        // cmdSetText
        // 
        this.cmdSetText.Location = new System.Drawing.Point(50, 65);
        this.cmdSetText.Name = "cmdSetText";
        this.cmdSetText.Size = new System.Drawing.Size(165, 23);
        this.cmdSetText.TabIndex = 2;
        this.cmdSetText.Text = "Set Text";
        this.cmdSetText.UseVisualStyleBackColor = true;
        this.cmdSetText.Click += new System.EventHandler(this.cmdSetText_Click);
        // 
        // cmdSetTextConstraint
        // 
        this.cmdSetTextConstraint.Location = new System.Drawing.Point(50, 91);
        this.cmdSetTextConstraint.Name = "cmdSetTextConstraint";
        this.cmdSetTextConstraint.Size = new System.Drawing.Size(165, 23);
        this.cmdSetTextConstraint.TabIndex = 3;
        this.cmdSetTextConstraint.Text = "Constrain Label and Set Text";
        this.cmdSetTextConstraint.UseVisualStyleBackColor = true;
        this.cmdSetTextConstraint.Click += new System.EventHandler(this.cmdSetTextConstraint_Click);
        // 
        // Form1
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.AutoSize = true;
        this.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;
        this.ClientSize = new System.Drawing.Size(247, 336);
        this.Controls.Add(this.cmdSetTextConstraint);
        this.Controls.Add(this.cmdSetText);
        this.Controls.Add(this.textBox1);
        this.Controls.Add(this.groupBox1);
        this.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
        this.Name = "Form1";
        this.Text = "Form1";
        this.groupBox1.ResumeLayout(false);
        this.groupBox1.PerformLayout();
        this.ResumeLayout(false);
        this.PerformLayout();

    }
    private void cmdSetText_Click(object sender, EventArgs e)
    {
        label1.MaximumSize = new Size(0,0);
        label1.Text = "";
        label1.Text = textBox1.Text;
    }

    private void cmdSetTextConstraint_Click(object sender, EventArgs e)
    {
        label1.MaximumSize = new Size(200,0);
        label1.Text = textBox1.Text;
    }

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

}



           
          


Label auto resize


   


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 System.Windows.Forms.GroupBox groupBox1;
    private System.Windows.Forms.Label label1;
    private System.Windows.Forms.TextBox textBox1;
    private System.Windows.Forms.Button cmdSetText;
    private System.Windows.Forms.Button cmdSetTextConstraint;

  public Form1() {
        InitializeComponent();
  }

    private void InitializeComponent(){
        this.groupBox1 = new System.Windows.Forms.GroupBox();
        this.label1 = new System.Windows.Forms.Label();
        this.textBox1 = new System.Windows.Forms.TextBox();
        this.cmdSetText = new System.Windows.Forms.Button();
        this.cmdSetTextConstraint = new System.Windows.Forms.Button();
        this.groupBox1.SuspendLayout();
        this.SuspendLayout();
        // 
        // groupBox1
        // 
        this.groupBox1.AutoSize = true;
        this.groupBox1.Controls.Add(this.label1);
        this.groupBox1.Location = new System.Drawing.Point(15, 133);
        this.groupBox1.Margin = new System.Windows.Forms.Padding(20);
        this.groupBox1.Name = "groupBox1";
        this.groupBox1.Size = new System.Drawing.Size(200, 100);
        this.groupBox1.TabIndex = 0;
        this.groupBox1.TabStop = false;
        this.groupBox1.Text = "groupBox1";
        // 
        // label1
        // 
        this.label1.AutoSize = true;
        this.label1.Location = new System.Drawing.Point(17, 25);
        this.label1.Margin = new System.Windows.Forms.Padding(5);
        this.label1.Name = "label1";
        this.label1.Size = new System.Drawing.Size(35, 13);
        this.label1.TabIndex = 0;
        this.label1.Text = "label1";
        // 
        // textBox1
        // 
        this.textBox1.Location = new System.Drawing.Point(15, 12);
        this.textBox1.Multiline = true;
        this.textBox1.Name = "textBox1";
        this.textBox1.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
        this.textBox1.Size = new System.Drawing.Size(200, 47);
        this.textBox1.TabIndex = 1;
        this.textBox1.Text = "The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the l" +
            "azy dog.";
        // 
        // cmdSetText
        // 
        this.cmdSetText.Location = new System.Drawing.Point(50, 65);
        this.cmdSetText.Name = "cmdSetText";
        this.cmdSetText.Size = new System.Drawing.Size(165, 23);
        this.cmdSetText.TabIndex = 2;
        this.cmdSetText.Text = "Set Text";
        this.cmdSetText.UseVisualStyleBackColor = true;
        this.cmdSetText.Click += new System.EventHandler(this.cmdSetText_Click);
        // 
        // cmdSetTextConstraint
        // 
        this.cmdSetTextConstraint.Location = new System.Drawing.Point(50, 91);
        this.cmdSetTextConstraint.Name = "cmdSetTextConstraint";
        this.cmdSetTextConstraint.Size = new System.Drawing.Size(165, 23);
        this.cmdSetTextConstraint.TabIndex = 3;
        this.cmdSetTextConstraint.Text = "Constrain Label and Set Text";
        this.cmdSetTextConstraint.UseVisualStyleBackColor = true;
        this.cmdSetTextConstraint.Click += new System.EventHandler(this.cmdSetTextConstraint_Click);
        // 
        // Form1
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.AutoSize = true;
        this.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;
        this.ClientSize = new System.Drawing.Size(247, 336);
        this.Controls.Add(this.cmdSetTextConstraint);
        this.Controls.Add(this.cmdSetText);
        this.Controls.Add(this.textBox1);
        this.Controls.Add(this.groupBox1);
        this.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
        this.Name = "Form1";
        this.Text = "Form1";
        this.groupBox1.ResumeLayout(false);
        this.groupBox1.PerformLayout();
        this.ResumeLayout(false);
        this.PerformLayout();

    }
    private void cmdSetText_Click(object sender, EventArgs e)
    {
        label1.MaximumSize = new Size(0,0);
        label1.Text = "";
        label1.Text = textBox1.Text;
    }

    private void cmdSetTextConstraint_Click(object sender, EventArgs e)
    {
        label1.MaximumSize = new Size(200,0);
        label1.Text = textBox1.Text;
    }

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

}


           
          


Set Label Background, foreground color and border style


   

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 System.Windows.Forms.Button learnMoreButton;
      private System.Windows.Forms.Label label2;
      private System.Windows.Forms.Label label1;
      
      public Form1() {
        InitializeComponent();
      }

      private void InitializeComponent()
      {
         this.learnMoreButton = new System.Windows.Forms.Button();
         this.label2 = new System.Windows.Forms.Label();
         this.label1 = new System.Windows.Forms.Label();
         this.SuspendLayout();
         // 
         // learnMoreButton
         // 
         this.learnMoreButton.Font = new System.Drawing.Font( "Microsoft Sans Serif", 14F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ( ( byte ) ( 0 ) ) );
         this.learnMoreButton.Location = new System.Drawing.Point( 17, 81 );
         this.learnMoreButton.Name = "learnMoreButton";
         this.learnMoreButton.Size = new System.Drawing.Size( 120, 59 );
         this.learnMoreButton.TabIndex = 11;
         this.learnMoreButton.Text = "Learn More";
         // 
         // label2
         // 
         this.label2.BackColor = System.Drawing.Color.LightYellow;
         this.label2.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
         this.label2.Font = new System.Drawing.Font( "Microsoft Sans Serif", 11.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ( ( byte ) ( 0 ) ) );
         this.label2.ForeColor = System.Drawing.Color.MidnightBlue;
         this.label2.Location = new System.Drawing.Point( 17, 154 );
         this.label2.Name = "label2";
         this.label2.Size = new System.Drawing.Size( 273, 25 );
         this.label2.TabIndex = 10;
         this.label2.Text = "text";
         this.label2.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
         // 
         // label1
         // 
         this.label1.BackColor = System.Drawing.Color.LightYellow;
         this.label1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
         this.label1.Font = new System.Drawing.Font( "Microsoft Sans Serif", 20.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ( ( byte ) ( 0 ) ) );
         this.label1.ForeColor = System.Drawing.Color.MidnightBlue;
         this.label1.Location = new System.Drawing.Point( 17, 17 );
         this.label1.Name = "label1";
         this.label1.Size = new System.Drawing.Size( 273, 47 );
         this.label1.TabIndex = 9;
         this.label1.Text = "test";
         this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
         // 
         // VisualInheritanceForm
         // 
         this.AutoScaleDimensions = new System.Drawing.SizeF( 6F, 13F );
         this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
         this.ClientSize = new System.Drawing.Size( 307, 201 );
         this.Controls.Add( this.learnMoreButton );
         this.Controls.Add( this.label2 );
         this.Controls.Add( this.label1 );
         this.Name = "VisualInheritanceForm";
         this.Text = "Visual Inheritance";
         this.ResumeLayout( false );

      }

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

}



           
          


Marquee Label Host


   

/*
User Interfaces in C#: Windows Forms and Custom Controls
by Matthew MacDonald

Publisher: Apress
ISBN: 1590590457
*/
using System.Drawing;

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace MarqueeLabelHost
{
    /// <summary>
    /// Summary description for MarqueeLabelHost.
    /// </summary>
    public class MarqueeLabelHost : System.Windows.Forms.Form
    {
        internal System.Windows.Forms.GroupBox GroupBox1;
        internal System.Windows.Forms.Label Label2;
        internal System.Windows.Forms.TrackBar tbInterval;
        internal System.Windows.Forms.Label Label1;
        internal System.Windows.Forms.TrackBar tbAmount;
        private MarqueeLabel marqueeLabel1;
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.Container components = null;

        public MarqueeLabelHost()
        {
            //
            // Required for Windows Form Designer support
            //
            InitializeComponent();

            //
            // TODO: Add any constructor code after InitializeComponent call
            //
        }

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        protected override void Dispose( bool disposing )
        {
            if( disposing )
            {
                if (components != null) 
                {
                    components.Dispose();
                }
            }
            base.Dispose( disposing );
        }

        #region Windows Form Designer generated code
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.GroupBox1 = new System.Windows.Forms.GroupBox();
            this.Label2 = new System.Windows.Forms.Label();
            this.tbInterval = new System.Windows.Forms.TrackBar();
            this.Label1 = new System.Windows.Forms.Label();
            this.tbAmount = new System.Windows.Forms.TrackBar();
            this.marqueeLabel1 = new MarqueeLabel();
            this.GroupBox1.SuspendLayout();
            ((System.ComponentModel.ISupportInitialize)(this.tbInterval)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.tbAmount)).BeginInit();
            this.SuspendLayout();
            // 
            // GroupBox1
            // 
            this.GroupBox1.Anchor = (((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
                | System.Windows.Forms.AnchorStyles.Left) 
                | System.Windows.Forms.AnchorStyles.Right);
            this.GroupBox1.Controls.AddRange(new System.Windows.Forms.Control[] {
                                                                                    this.Label2,
                                                                                    this.tbInterval,
                                                                                    this.Label1,
                                                                                    this.tbAmount});
            this.GroupBox1.FlatStyle = System.Windows.Forms.FlatStyle.System;
            this.GroupBox1.Location = new System.Drawing.Point(24, 176);
            this.GroupBox1.Name = "GroupBox1";
            this.GroupBox1.Size = new System.Drawing.Size(336, 132);
            this.GroupBox1.TabIndex = 4;
            this.GroupBox1.TabStop = false;
            // 
            // Label2
            // 
            this.Label2.Location = new System.Drawing.Point(12, 76);
            this.Label2.Name = "Label2";
            this.Label2.Size = new System.Drawing.Size(80, 23);
            this.Label2.TabIndex = 6;
            this.Label2.Text = "Scroll Interval:";
            // 
            // tbInterval
            // 
            this.tbInterval.Location = new System.Drawing.Point(96, 72);
            this.tbInterval.Maximum = 500;
            this.tbInterval.Minimum = 10;
            this.tbInterval.Name = "tbInterval";
            this.tbInterval.Size = new System.Drawing.Size(228, 45);
            this.tbInterval.TabIndex = 5;
            this.tbInterval.TickFrequency = 10;
            this.tbInterval.Value = 100;
            this.tbInterval.Scroll += new System.EventHandler(this.tbInterval_Scroll);
            // 
            // Label1
            // 
            this.Label1.Location = new System.Drawing.Point(12, 20);
            this.Label1.Name = "Label1";
            this.Label1.Size = new System.Drawing.Size(80, 23);
            this.Label1.TabIndex = 4;
            this.Label1.Text = "Scroll Amount:";
            // 
            // tbAmount
            // 
            this.tbAmount.Location = new System.Drawing.Point(96, 16);
            this.tbAmount.Maximum = 20;
            this.tbAmount.Name = "tbAmount";
            this.tbAmount.Size = new System.Drawing.Size(228, 45);
            this.tbAmount.TabIndex = 3;
            this.tbAmount.Value = 1;
            this.tbAmount.Scroll += new System.EventHandler(this.tbAmount_Scroll);
            // 
            // marqueeLabel1
            // 
            this.marqueeLabel1.Anchor = ((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
                | System.Windows.Forms.AnchorStyles.Right);
            this.marqueeLabel1.Font = new System.Drawing.Font("Verdana", 26.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
            this.marqueeLabel1.ForeColor = System.Drawing.Color.Navy;
            this.marqueeLabel1.Location = new System.Drawing.Point(0, 12);
            this.marqueeLabel1.Name = "marqueeLabel1";
            this.marqueeLabel1.ScrollTimeInterval = 100;
            this.marqueeLabel1.Size = new System.Drawing.Size(384, 156);
            this.marqueeLabel1.TabIndex = 5;
            this.marqueeLabel1.Tag = "";
            this.marqueeLabel1.Text = "This scrolls!";
            // 
            // MarqueeLabelHost
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 14);
            this.ClientSize = new System.Drawing.Size(380, 318);
            this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                                                          this.marqueeLabel1,
                                                                          this.GroupBox1});
            this.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
            this.Name = "MarqueeLabelHost";
            this.Text = "MarqueeLabelHost";
            this.GroupBox1.ResumeLayout(false);
            ((System.ComponentModel.ISupportInitialize)(this.tbInterval)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.tbAmount)).EndInit();
            this.ResumeLayout(false);

        }
        #endregion

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main() 
        {
            Application.Run(new MarqueeLabelHost());
        }

        private void tbInterval_Scroll(object sender, System.EventArgs e)
        {
            marqueeLabel1.ScrollTimeInterval = tbInterval.Value;
        }

        private void tbAmount_Scroll(object sender, System.EventArgs e)
        {
            marqueeLabel1.ScrollPixelAmount = tbAmount.Value;
        }
    }
    /// <summary>
    /// Summary description for MarqueeLabel.
    /// </summary>
    public class MarqueeLabel : System.Windows.Forms.UserControl
    {
        private System.ComponentModel.IContainer components;

        public MarqueeLabel()
        {
            // This call is required by the Windows.Forms Form Designer.
            InitializeComponent();

            // TODO: Add any initialization after the InitForm call

        }

        /// <summary> 
        /// Clean up any resources being used.
        /// </summary>
        protected override void Dispose( bool disposing )
        {
            if( disposing )
            {
                if(components != null)
                {
                    components.Dispose();
                }
            }
            base.Dispose( disposing );
        }

        #region Component Designer generated code
        /// <summary> 
        /// Required method for Designer support - do not modify 
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.components = new System.ComponentModel.Container();
            this.tmrScroll = new System.Windows.Forms.Timer(this.components);
            // 
            // tmrScroll
            // 
            this.tmrScroll.Tick += new System.EventHandler(this.tmrScroll_Tick);
            // 
            // MarqueeLabel
            // 
            this.Name = "MarqueeLabel";
            this.Size = new System.Drawing.Size(360, 104);
            this.Load += new System.EventHandler(this.MarqueeLabel_Load);

        }
        #endregion

        private string text;
        private int scrollAmount = 10;
        internal System.Windows.Forms.Timer tmrScroll;
        private int position = 0;

        private void MarqueeLabel_Load(object sender, System.EventArgs e)
        {
            this.ResizeRedraw = true;
            if (!this.DesignMode)
            {
                tmrScroll.Enabled = true;
            }

        }

        private void tmrScroll_Tick(object sender, System.EventArgs e)
        {
            position += scrollAmount;

            // Force a refresh.
            this.Invalidate();

        }

        [Browsable(true), 
        DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
        public override string Text
        {
            get
            {
                return text;
                }
            set
            {
                text = value;
                this.Invalidate();
            }
        }

        public int ScrollTimeInterval
        {
            get
            {
                return tmrScroll.Interval;
            }
            set
            {
                tmrScroll.Interval = value;
            }
        }

        [DefaultValue(10)] 
        public int ScrollPixelAmount
        {
            get
            {
                return scrollAmount;
            }
            set
            {
                scrollAmount = value;
            }
        }

        protected override void OnPaintBackground(System.Windows.Forms.PaintEventArgs e)
        {
            // Do nothing.
            // To prevent flicker, we will draw both the background and the text
            // to a buffered image, and draw it to the control all at once.
        }

        protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
        {
            // The following line avoids a design-time error that would
            // otherwise occur when the control is first loaded (but does not yet
            // have a defined size).
            if (e.ClipRectangle.Width == 0)
            {
                return;
            }

            base.OnPaint(e);
            if (position > this.Width)
            {
                // Reset the text to scroll back onto the control.
                position = -(int)e.Graphics.MeasureString(text, this.Font).Width;
            }

            // Create the drawing area in memory.
            // Double buffering is used to prevent flicker.
            Bitmap blt = new Bitmap(e.ClipRectangle.Width, e.ClipRectangle.Height);
            Graphics g = Graphics.FromImage(blt);

            g.FillRectangle(new SolidBrush(this.BackColor), e.ClipRectangle);
            g.DrawString(text, this.Font, new SolidBrush(this.ForeColor), position, 0);

            // Render the finished image on the form.
            e.Graphics.DrawImageUnscaled(blt, 0, 0);

            g.Dispose();
        }


    }


}



           
          


Use Label and Timer to create a Clock

   
 

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

public class Form1 : System.Windows.Forms.Form {
    public System.Timers.Timer timer1;
    private System.Windows.Forms.Label label1;
    public Form1() {
        this.timer1 = new System.Timers.Timer();
        this.label1 = new System.Windows.Forms.Label();
        ((System.ComponentModel.ISupportInitialize)(this.timer1)).BeginInit();
        this.SuspendLayout();
        // 
        // timer1
        // 
        this.timer1.Enabled = true;
        this.timer1.SynchronizingObject = this;
        this.timer1.Elapsed += new System.Timers.ElapsedEventHandler(this.OnTimerElapsed);
        // 
        // label1
        // 
        this.label1.Font = new System.Drawing.Font("Microsoft Sans Serif", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
        this.label1.ForeColor = System.Drawing.SystemColors.Highlight;
        this.label1.Location = new System.Drawing.Point(24, 8);
        this.label1.Name = "label1";
        this.label1.Size = new System.Drawing.Size(224, 48);
        this.label1.TabIndex = 0;
        this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
        // 
        // Form1
        // 
        this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
        this.ClientSize = new System.Drawing.Size(292, 69);
        this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                                                      this.label1});
        this.Text = "My Clock";
        this.Load += new System.EventHandler(this.Form1_Load);
        ((System.ComponentModel.ISupportInitialize)(this.timer1)).EndInit();
        this.ResumeLayout(false);

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

    private void Form1_Load(object sender, System.EventArgs e) {
        timer1.Interval = 1000;
        timer1.Start();
        timer1.Enabled = true;
    }

    private void OnTimerElapsed(object sender, System.Timers.ElapsedEventArgs e) {
        label1.Text = DateTime.Now.ToString();
    }
}