Mega Code Archive

 
Categories / C# Tutorial / GUI Windows Forms
 

Extends UserControl to create your own control

using System.Drawing; using System; using System.Collections; using System.ComponentModel; using System.Drawing; using System.Data; using System.Windows.Forms;   public class PhoneControl : UserControl   {         public String Phone      {             get        {         return "("+areaCodeBox.Text+") "+phoneNoBox1.Text+"-" +phoneNoBox2.Text;       }     }     private Label areaCodeLabel;     private Label phoneNumberLabel;     private TextBox phoneNoBox1;     private TextBox phoneNoBox2;     private TextBox areaCodeBox;     private System.ComponentModel.Container components = null;     public PhoneControl()     {       this.areaCodeLabel = new Label();       this.areaCodeBox = new TextBox();       this.phoneNoBox1 = new TextBox();       this.phoneNumberLabel = new Label();       this.phoneNoBox2 = new TextBox();       this.SuspendLayout();       //        this.areaCodeLabel.Location = new Point(8, 16);       this.areaCodeLabel.Name = "areaCodeLabel";       this.areaCodeLabel.Size = new Size(72, 20);       this.areaCodeLabel.TabIndex = 0;       this.areaCodeLabel.Text = "Area Code";       //        this.areaCodeBox.Location = new Point(96, 16);       this.areaCodeBox.MaxLength = 3;       this.areaCodeBox.Name = "areaCodeBox";       this.areaCodeBox.Size = new Size(32, 20);       this.areaCodeBox.TabIndex = 1;       this.areaCodeBox.Text = "";       //        this.phoneNoBox1.Location = new Point(96, 40);       this.phoneNoBox1.MaxLength = 3;       this.phoneNoBox1.Name = "phoneNoBox1";       this.phoneNoBox1.Size = new Size(32, 20);       this.phoneNoBox1.TabIndex = 2;       this.phoneNoBox1.Text = "";       //        this.phoneNumberLabel.Location = new Point(8, 40);       this.phoneNumberLabel.Name = "phoneNumberLabel";       this.phoneNumberLabel.Size = new Size(80, 20);       this.phoneNumberLabel.TabIndex = 2;       this.phoneNumberLabel.Text = "Phone Number";       //        this.phoneNoBox2.Location = new Point(144, 40);       this.phoneNoBox2.MaxLength = 4;       this.phoneNoBox2.Name = "phoneNoBox2";       this.phoneNoBox2.Size = new Size(40, 20);       this.phoneNoBox2.TabIndex = 3;       this.phoneNoBox2.Text = "";       //        this.Controls.Add(this.phoneNoBox2);       this.Controls.Add(this.phoneNoBox1);       this.Controls.Add(this.phoneNumberLabel);       this.Controls.Add(this.areaCodeBox);       this.Controls.Add(this.areaCodeLabel);       this.Name = "PhoneControl";       this.Size = new Size(200, 72);       this.ResumeLayout(false);     }   }   public class Form1 : Form   {     private Button ShowButton;     private Label PhoneLabel;     private PhoneControl phoneControl;     private Label label1;     private System.ComponentModel.Container components = null;     public Form1()     {       this.phoneControl = new PhoneControl();       this.ShowButton = new Button();       this.PhoneLabel = new Label();       this.label1 = new Label();       this.SuspendLayout();       //        this.phoneControl.Location = new Point(96, 8);       this.phoneControl.Name = "phoneControl";       this.phoneControl.Size = new Size(200, 72);       this.phoneControl.TabIndex = 0;       //        // ShowButton       //        this.ShowButton.Location = new Point(48, 112);       this.ShowButton.Name = "ShowButton";       this.ShowButton.Size = new Size(192, 24);       this.ShowButton.TabIndex = 1;       this.ShowButton.Text = "Show Phone No";       this.ShowButton.Click += new System.EventHandler(this.ShowButton_Click);       //        // PhoneLabel       //        this.PhoneLabel.Location = new Point(8, 168);       this.PhoneLabel.Name = "PhoneLabel";       this.PhoneLabel.Size = new Size(272, 24);       this.PhoneLabel.TabIndex = 2;       this.PhoneLabel.TextAlign = ContentAlignment.MiddleCenter;       //        // label1       //        this.label1.Location = new Point(8, 0);       this.label1.Name = "label1";       this.label1.Size = new Size(80, 80);       this.label1.TabIndex = 3;       this.label1.Text = "Phone No:";       this.label1.TextAlign = ContentAlignment.MiddleCenter;       //        // Form1       //        this.AutoScaleBaseSize = new Size(5, 13);       this.ClientSize = new Size(292, 197);       this.Controls.Add(this.label1);       this.Controls.Add(this.PhoneLabel);       this.Controls.Add(this.ShowButton);       this.Controls.Add(this.phoneControl);       this.Name = "Form1";       this.Text = "Form1";       this.ResumeLayout(false);     }     [STAThread]     static void Main()      {       Application.Run(new Form1());     }     private void ShowButton_Click(object sender, System.EventArgs e)     {       PhoneLabel.Text = phoneControl.Phone;     }   }