TextBox value validation Demo


   

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
  {
    private System.Windows.Forms.Label lblName;
    private System.Windows.Forms.Label lblAddress;
    private System.Windows.Forms.Label lblOccupation;
    private System.Windows.Forms.Label lblAge;
    private System.Windows.Forms.Button btnOK;
    private System.Windows.Forms.Button btnHelp;
    private System.Windows.Forms.TextBox txtName;
    private System.Windows.Forms.TextBox txtAddress;
    private System.Windows.Forms.TextBox txtOccupation;
    private System.Windows.Forms.TextBox txtAge;

    public Form1()
    {
      InitializeComponent();
      this.btnOK.Enabled = false;

      this.txtAddress.Tag = false;
      this.txtAge.Tag = false;
      this.txtName.Tag = false;
      this.txtOccupation.Tag = false;

      this.txtName.Validating += new System.ComponentModel.CancelEventHandler(this.txtBoxEmpty_Validating);
      this.txtAddress.Validating += new System.ComponentModel.CancelEventHandler(this.txtBoxEmpty_Validating);
      this.txtOccupation.Validating += new System.ComponentModel.CancelEventHandler(this.txtOccupation_Validating);
      this.txtAge.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.txtAge_KeyPress);
      this.txtAge.Validating += new System.ComponentModel.CancelEventHandler(this.txtBoxEmpty_Validating);
      this.txtName.TextChanged += new System.EventHandler(this.txtBox_TextChanged);
      this.txtAddress.TextChanged += new System.EventHandler(this.txtBox_TextChanged);
      this.txtAge.TextChanged += new System.EventHandler(this.txtBox_TextChanged);
      this.txtOccupation.TextChanged += new System.EventHandler(this.txtBox_TextChanged);
    }
    private void InitializeComponent()
    {
      this.lblName = new System.Windows.Forms.Label();
      this.txtName = new System.Windows.Forms.TextBox();
      this.lblAge = new System.Windows.Forms.Label();
      this.btnOK = new System.Windows.Forms.Button();
      this.txtAge = new System.Windows.Forms.TextBox();
      this.txtAddress = new System.Windows.Forms.TextBox();
      this.lblAddress = new System.Windows.Forms.Label();
      this.lblOccupation = new System.Windows.Forms.Label();
      this.txtOccupation = new System.Windows.Forms.TextBox();
      this.btnHelp = new System.Windows.Forms.Button();
      this.SuspendLayout();
      // 
      // lblName
      // 
      this.lblName.Anchor = ((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
        | System.Windows.Forms.AnchorStyles.Left);
      this.lblName.Location = new System.Drawing.Point(8, 16);
      this.lblName.Name = "lblName";
      this.lblName.Size = new System.Drawing.Size(92, 23);
      this.lblName.TabIndex = 0;
      this.lblName.Text = "Name";
      // 
      // txtName
      // 
      this.txtName.Anchor = (((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
        | System.Windows.Forms.AnchorStyles.Left) 
        | System.Windows.Forms.AnchorStyles.Right);
      this.txtName.Location = new System.Drawing.Point(112, 16);
      this.txtName.Name = "txtName";
      this.txtName.Size = new System.Drawing.Size(176, 20);
      this.txtName.TabIndex = 5;
      this.txtName.Text = "";
      // 
      // lblAge
      // 
      this.lblAge.Anchor = ((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
        | System.Windows.Forms.AnchorStyles.Left);
      this.lblAge.Location = new System.Drawing.Point(8, 144);
      this.lblAge.Name = "lblAge";
      this.lblAge.Size = new System.Drawing.Size(92, 23);
      this.lblAge.TabIndex = 3;
      this.lblAge.Text = "Age";
      // 
      // btnOK
      // 
      this.btnOK.Anchor = (System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right);
      this.btnOK.Location = new System.Drawing.Point(296, 16);
      this.btnOK.Name = "btnOK";
      this.btnOK.TabIndex = 10;
      this.btnOK.Text = "OK";
      this.btnOK.Click += new System.EventHandler(this.btnOK_Click);
      // 
      // txtAge
      // 
      this.txtAge.Anchor = (((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
        | System.Windows.Forms.AnchorStyles.Left) 
        | System.Windows.Forms.AnchorStyles.Right);
      this.txtAge.Location = new System.Drawing.Point(112, 136);
      this.txtAge.Name = "txtAge";
      this.txtAge.Size = new System.Drawing.Size(176, 20);
      this.txtAge.TabIndex = 8;
      this.txtAge.Text = "";
      // 
      // txtAddress
      // 
      this.txtAddress.Anchor = ((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
        | System.Windows.Forms.AnchorStyles.Right);
      this.txtAddress.Location = new System.Drawing.Point(112, 40);
      this.txtAddress.Multiline = true;
      this.txtAddress.Name = "txtAddress";
      this.txtAddress.ScrollBars = System.Windows.Forms.ScrollBars.Both;
      this.txtAddress.Size = new System.Drawing.Size(176, 72);
      this.txtAddress.TabIndex = 6;
      this.txtAddress.Text = "";
      // 
      // lblAddress
      // 
      this.lblAddress.Anchor = ((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
        | System.Windows.Forms.AnchorStyles.Left);
      this.lblAddress.Location = new System.Drawing.Point(8, 40);
      this.lblAddress.Name = "lblAddress";
      this.lblAddress.Size = new System.Drawing.Size(92, 23);
      this.lblAddress.TabIndex = 1;
      this.lblAddress.Text = "Address";
      // 
      // lblOccupation
      // 
      this.lblOccupation.Anchor = ((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
        | System.Windows.Forms.AnchorStyles.Left);
      this.lblOccupation.Location = new System.Drawing.Point(8, 120);
      this.lblOccupation.Name = "lblOccupation";
      this.lblOccupation.Size = new System.Drawing.Size(92, 23);
      this.lblOccupation.TabIndex = 2;
      this.lblOccupation.Text = "Occupation";
      // 
      // txtOccupation
      // 
      this.txtOccupation.Anchor = (((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
        | System.Windows.Forms.AnchorStyles.Left) 
        | System.Windows.Forms.AnchorStyles.Right);
      this.txtOccupation.Location = new System.Drawing.Point(112, 112);
      this.txtOccupation.Name = "txtOccupation";
      this.txtOccupation.Size = new System.Drawing.Size(176, 20);
      this.txtOccupation.TabIndex = 7;
      this.txtOccupation.Text = "";
      // 
      // btnHelp
      // 
      this.btnHelp.Anchor = (System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right);
      this.btnHelp.CausesValidation = false;
      this.btnHelp.Location = new System.Drawing.Point(296, 48);
      this.btnHelp.Name = "btnHelp";
      this.btnHelp.TabIndex = 11;
      this.btnHelp.Text = "Help";
      this.btnHelp.Click += new System.EventHandler(this.btnHelp_Click);
      // 
      // Form1
      // 
      this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
      this.ClientSize = new System.Drawing.Size(376, 253);
      this.Controls.AddRange(new System.Windows.Forms.Control[] {this.btnHelp,
                                                                       this.btnOK,
                                                                       this.txtAge,
                                                                       this.txtOccupation,
                                     this.txtAddress,
                                     this.txtName,
                                     this.lblAge,
                                     this.lblOccupation,
                                     this.lblAddress,
                                     this.lblName});
      this.MinimumSize = new System.Drawing.Size(384, 280);
      this.Name = "Form1";
      this.Text = "TestBoxTest";
      this.ResumeLayout(false);

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

    private void btnOK_Click(object sender, System.EventArgs e)
    {
      string output;
      output = "Name: " + this.txtName.Text + "
";
      output += "Address: " + this.txtAddress.Text + "
";
      output += "Occupation: " + this.txtOccupation.Text + "
";
      output += "Age: " + this.txtAge.Text;

      Console.WriteLine(output);
    }

    private void btnHelp_Click(object sender, System.EventArgs e)
    {
      string output;

      output = "Name = Your name" + "
";
      output += "Address = Your address" + "
";
      output += "Occupation = Only allowed value is 'Employee' or empty" + "
";
      output += "Age = Your age";

      Console.WriteLine(output);
    }

    private void txtBoxEmpty_Validating(object sender, System.ComponentModel.CancelEventArgs e)
    {
      TextBox tb;

      tb = (TextBox)sender;

      if (tb.Text.Length == 0)
      {
        tb.BackColor = Color.Red;
        tb.Tag = false;
      }
      else
      {
        tb.BackColor = System.Drawing.SystemColors.Window;
        tb.Tag = true;
      }
      ValidateAll();
    }

    private void ValidateAll()
    {
      this.btnOK.Enabled = ((bool)(this.txtAddress.Tag) &&
        (bool)(this.txtAge.Tag) &&
        (bool)(this.txtName.Tag) &&
        (bool)(this.txtOccupation.Tag));
    }

    private void txtOccupation_Validating(object sender, System.ComponentModel.CancelEventArgs e)
    {
      TextBox tb = (TextBox)sender;

      if (tb.Text.CompareTo("Employee") == 0 || tb.Text.Length == 0)
      {
        tb.Tag = true;
        tb.BackColor = System.Drawing.SystemColors.Window;
      }
      else
      {
        tb.Tag = false;
        tb.BackColor = Color.Red;
      }

      ValidateAll();
    }

    private void txtAge_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
    {
      if ((e.KeyChar < 48 || e.KeyChar > 57) &amp;&amp; e.KeyChar != 8)
        e.Handled = true; // Remove the character
    }

    private void txtBox_TextChanged(object sender, System.EventArgs e)
    {
      TextBox tb = (TextBox)sender;

      if (tb.Text.Length == 0 &amp;&amp; tb != txtOccupation)
      {
        tb.Tag = false;
        tb.BackColor = Color.Red;
      }
      else if (tb == txtOccupation &amp;&amp; (tb.Text.Length != 0 &amp;&amp; tb.Text.CompareTo("Employee") != 0))
      {
        tb.Tag = false;
      }
      else
      {
        tb.Tag = true;
        tb.BackColor = SystemColors.Window;
      }

      ValidateAll();
    }
  }


           
          


The read-only property control.

   
 


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


public class TimerCounter : System.Windows.Forms.UserControl {
    private System.Windows.Forms.Timer timer1;
    private System.ComponentModel.IContainer components;
    private int fCounter = 0;
    public int Counter {
        get {
            return fCounter;
        }
    }
    public TimerCounter() {
        this.components = new System.ComponentModel.Container();
        this.timer1 = new System.Windows.Forms.Timer(this.components);
        this.timer1.Enabled = true;
        this.timer1.Interval = 1000;
        this.timer1.Tick += new System.EventHandler(this.OnTick);
        this.Name = "TimerCounter";
    }
    private void OnTick(object sender, System.EventArgs e) {
        fCounter++;
    }
}
public class Form1 : System.Windows.Forms.Form {
    private System.Windows.Forms.Label label1 = new System.Windows.Forms.Label();
    private System.Windows.Forms.Label CounterLabel;
    private System.Windows.Forms.Button Update;
    private System.ComponentModel.Container components = null;
    private TimerCounter counter;

    public Form1() {
        counter = new TimerCounter();
        this.CounterLabel = new System.Windows.Forms.Label();
        this.Update = new System.Windows.Forms.Button();
        this.SuspendLayout();
        this.label1.Location = new System.Drawing.Point(32, 24);
        this.label1.Size = new System.Drawing.Size(48, 23);
        this.label1.Text = "Counter: ";
        this.CounterLabel.Location = new System.Drawing.Point(96, 24);
        this.CounterLabel.Size = new System.Drawing.Size(32, 23);
        this.Update.Location = new System.Drawing.Point(80, 72);
        this.Update.Text = "Update";
        this.Update.Click += new System.EventHandler(this.Update_Click);
        this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
        this.ClientSize = new System.Drawing.Size(224, 133);
        this.Controls.AddRange(new System.Windows.Forms.Control[] {this.Update,this.CounterLabel,this.label1});
        this.ResumeLayout(false);
    }
    [STAThread]
    static void Main() {
        Application.Run(new Form1());
    }
    private void Update_Click(object sender, System.EventArgs e) {
        CounterLabel.Text = counter.Counter.ToString();
    }
}

    


Creating an event for a component.

   
 


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


public class CompoundControl : System.Windows.Forms.UserControl {
    public delegate Boolean ValueChangedEventHandler(int nValue);
    private System.Windows.Forms.Label label1;
    private System.Windows.Forms.ComboBox comboBox1;
    public event ValueChangedEventHandler Changed;

    public CompoundControl() {
        this.comboBox1 = new System.Windows.Forms.ComboBox();
        this.label1 = new System.Windows.Forms.Label();
        this.SuspendLayout();
        this.comboBox1.DropDownWidth = 121;
        this.comboBox1.Items.AddRange(new object[] {"A","B","C","F","G","N"});
        this.comboBox1.Location = new System.Drawing.Point(24, 48);
        this.comboBox1.Size = new System.Drawing.Size(200, 21);
        this.comboBox1.Text = "comboBox1";
        this.comboBox1.SelectedIndexChanged += new System.EventHandler(this.OnSelectionIndexChange);
        this.label1.Location = new System.Drawing.Point(16, 24);
        this.label1.Text = "Select An Entry";
        this.Controls.AddRange(new System.Windows.Forms.Control[] {
        this.comboBox1,
        this.label1});
        this.Size = new System.Drawing.Size(240, 96);
        this.ResumeLayout(false);
    }
    private void OnSelectionIndexChange(object sender,
        System.EventArgs e) {
        if (Changed != null)
            Changed(this.comboBox1.SelectedIndex);
    }
}


public class Form1 : System.Windows.Forms.Form {
    private System.ComponentModel.Container components = null;
    private CompoundControl compoundcomponent1 = null;

    public Form1() {
        this.compoundcomponent1 = new CompoundControl();
        this.compoundcomponent1.Location = new System.Drawing.Point(24, 50);
        this.compoundcomponent1.Name = "compound1";
        this.compoundcomponent1.Size = new System.Drawing.Size(250, 100);
        this.compoundcomponent1.Changed += new CompoundControl.ValueChangedEventHandler(OnChanged);
        this.components = new System.ComponentModel.Container();
        this.Size = new System.Drawing.Size(300, 300);
        this.Controls.AddRange(new System.Windows.Forms.Control[]{this.compoundcomponent1,});
    }
    private bool OnChanged(int nIndex) {
        MessageBox.Show(this, "New Index!" + nIndex);
        return true;
    }

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

    


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 );

      }

}


           
          


Draw a Custom Control

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

public class SimpleCustomControl : System.Windows.Forms.Control {
    public SimpleCustomControl() {
        InitializeComponent();
    }
    private void SimpleCustomControl_Paint(object sender, PaintEventArgs e) {
        Graphics g = e.Graphics;
        g.FillRectangle(Brushes.Yellow, ClientRectangle);
        g.DrawString("Hello, world", Font, Brushes.Black, 0, 0);
    }
    private void InitializeComponent() {
        this.SuspendLayout();
        this.Paint += new System.Windows.Forms.PaintEventHandler(this.SimpleCustomControl_Paint);
        this.ResumeLayout(false);
    }
}


public class Form1 : Form {
    public Form1() {
        InitializeComponent();
    }
    private void InitializeComponent() {
        this.simpleCustomControl1 = new SimpleCustomControl();
        this.SuspendLayout();

        this.simpleCustomControl1.Location = new System.Drawing.Point(12, 12);
        this.simpleCustomControl1.Size = new System.Drawing.Size(178, 110);
        this.simpleCustomControl1.Text = "simpleCustomControl1";

        this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 16F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(292, 268);
        this.Controls.Add(this.simpleCustomControl1);
        this.ResumeLayout(false);
    }
    private SimpleCustomControl simpleCustomControl1;
    [STAThread]
    static void Main() {
        Application.EnableVisualStyles();
        Application.Run(new Form1());
    }
}

    


subclass System.Windows.Forms.UserControl to create custom control

   
 


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

public class UserControl1 : System.Windows.Forms.UserControl {
    private System.ComponentModel.Container components = null;

    public UserControl1() {
        this.Name = "UserControl1";
        this.Paint += new
         System.Windows.Forms.PaintEventHandler(this.OnPaint);

    }


    private void OnPaint(object sender,
         System.Windows.Forms.PaintEventArgs e) {
        e.Graphics.DrawString("Hello world", Font,
           new SolidBrush(Color.Blue), ClientRectangle);
    }
}
public class Form1 : System.Windows.Forms.Form {
    private System.ComponentModel.Container components = null;
    private System.Windows.Forms.Label label1;
    private UserControl1 control1;

    public Form1() {
        this.control1 = new UserControl1();
        this.label1 = new System.Windows.Forms.Label();
        this.SuspendLayout();
        this.control1.Location = new System.Drawing.Point(32, 48);
        this.control1.Size = new System.Drawing.Size(80, 24);
        this.label1.Location = new System.Drawing.Point(32, 24);
        this.label1.Size = new System.Drawing.Size(144, 24);
        this.label1.Text = "Custom Control:";
        this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
        this.ClientSize = new System.Drawing.Size(292, 273);
        this.Controls.AddRange(new System.Windows.Forms.Control[] {
        this.label1,
        this.control1});
        this.ResumeLayout(false);
    }

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

    


UpDown Derived

/*
Professional Windows GUI Programming Using C#
by Jay Glynn, Csaba Torok, Richard Conway, Wahid Choudhury,
Zach Greenvoss, Shripad Kulkarni, Neil Whitlow

Publisher: Peer Information
ISBN: 1861007663
*/

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

namespace UpDown_Derived
{
///

/// Summary description for UpDownDerived.
///

public class UpDownDerived : System.Windows.Forms.Form
{
///

/// Required designer variable.
///

private System.ComponentModel.Container components = null;
private System.Windows.Forms.Label label1;
private MyUpDownControl mDC = null ;
public UpDownDerived()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

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

///

/// Clean up any resources being used.
///

protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
///

/// Required method for Designer support – do not modify
/// the contents of this method with the code editor.
///

private void InitializeComponent()
{
this.mDC = new UpDown_Derived.MyUpDownControl();
this.label1 = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// mDC
//
this.mDC.Items.Add(“FRANCE”);
this.mDC.Items.Add(“ITALY”);
this.mDC.Items.Add(“USA”);
this.mDC.Items.Add(“UK”);
this.mDC.Items.Add(“AUSTRALIA”);
this.mDC.Items.Add(“INDIA”);
this.mDC.Items.Add(“ZAMBIA”);
this.mDC.Items.Add(“MALASYIA”);
this.mDC.Location = new System.Drawing.Point(40, 40);
this.mDC.Name = “mDC”;
this.mDC.TabIndex = 0;
//
// label1
//
this.label1.Location = new System.Drawing.Point(16, 16);
this.label1.Name = “label1”;
this.label1.Size = new System.Drawing.Size(200, 16);
this.label1.TabIndex = 1;
this.label1.Text = “Derived DomainUpDown Controller”;
//
// UpDownDerived
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(248, 85);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.label1,
this.mDC});
this.Name = “UpDownDerived”;
this.Text = “My Domain Controller”;
this.Load += new System.EventHandler(this.UpDownDerived_Load);
this.ResumeLayout(false);

}
#endregion

///

/// The main entry point for the application.
///

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

private void UpDownDerived_Load(object sender, System.EventArgs e)
{
}
}
///

/// Summary description for MyUpDownControl.
///

public class MyUpDownControl : System.Windows.Forms.DomainUpDown
{
private int currentPos =0;
private string DisplayText =””;
public MyUpDownControl()
{
// Populate the control with items
Items.Add(“FRANCE”);
Items.Add(“ITALY”);
Items.Add(“USA”);
Items.Add(“UK”);
Items.Add(“AUSTRALIA”);
Items.Add(“INDIA”);
Items.Add(“ZAMBIA”);
Items.Add(“MALASYIA”);

}

public override void DownButton()
{
// Check if the Down Arrow is clicked
currentPos ++;
if ( currentPos >= Items.Count )
currentPos = 0 ;
UpdateEditText();
}

public override void UpButton()
{
// Check if the Up Arrow is clicked
currentPos — ;
if ( currentPos < 0 ) currentPos = Items.Count-1 ; UpdateEditText(); } protected override void UpdateEditText() { // Update the EditBox DisplayText = (string)this.Items[currentPos ]; this.Text = DisplayText; } public void Sort() { if ( this.Sorted ) this.Sorted= false ; else this.Sorted= true ; if ( this.Sorted ) this.Sort() ; UpdateEditText() ; } } } [/csharp]