Creating User Controls


Creating User Controls

ARE MENUS USEFUL?

When used carefully , context-sensitive menus can be very useful. Menus are typically associated with keyboard shortcuts and can allow the user to easily navigate a complex business application.


A set of controls provided by the .NET Framework is quite broad and, for most applications scenarios, perhaps sufficient. However, for scenarios when you will need to either tweak the prebuilt controls or create new controls by aggregating multiple simpler controls, the Windows Forms application model allows developers to create their own controls by subclassing the UserControl class. For instance, consider Listing 7.8, where a Phone control is created by using a set of TextBox and Label controls. The control also has its own property (Phone, read-only in this case) that provides the entered phone number. As shown in Figure 7.12, User control design and development is fully available within the Visual Studio .NET designer environment.

Listing 7.8 User Controls
 using System; using System.Collections; using System.ComponentModel; using System.Drawing; using System.Data; using System.Windows.Forms; namespace WindowsControlLibrary1 {    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()       {          InitializeComponent();       }       protected override void Dispose( bool disposing )       {          if( disposing )          {             if( components != null )                components.Dispose();          }          base.Dispose( disposing );       }       private void InitializeComponent()       {          this.areaCodeLabel = new Label();          this.areaCodeBox = new TextBox();          this.phoneNoBox1 = new TextBox();          this.phoneNumberLabel = new Label();          this.phoneNoBox2 = new TextBox();          this.SuspendLayout();          //          // areaCodeLabel          //          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";          //          // areaCodeBox          //          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 = "";          //          // phoneNoBox1          //          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 = "";          //          // phoneNumberLabel          //          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";          //          // phoneNoBox2          //          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 = "";          //          // PhoneControl          //          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.Load += new System.EventHandler(this.PhoneControl_Load);          this.ResumeLayout(false);       }    } } 
Figure 7.12. Phone User control.

Using User Controls

After they are developed, user controls can be used within a Windows Forms application, shown in Listing 7.9 (developed using any .NET supported programming language), by adding the reference of the control library to the application (Figure 7.13). In Visual Studio .NET, you can add a reference to a related Windows controls project, and the user controls will be available for use visually using drag and drop under the My User controls Toolbox palette.

Listing 7.9 Using User Controls
 using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; namespace UsingUserControls {    public class Form1 : Form    {       private Button ShowButton;       private Label PhoneLabel;       private WindowsControlLibrary1.PhoneControl phoneControl;       private Label label1;       private System.ComponentModel.Container components = null;       public Form1()       {          InitializeComponent();       }       protected override void Dispose( bool disposing )       {          if( disposing )          {             if (components != null)             {                components.Dispose();             }          }          base.Dispose( disposing );       }       private void InitializeComponent()       {          this.phoneControl = new WindowsControlLibrary1.PhoneControl();          this.ShowButton = new Button();          this.PhoneLabel = new Label();          this.label1 = new Label();          this.SuspendLayout();          //          // phoneControl          //          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 EventHandler(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;       }    } } 
Figure 7.13. Using Phone User controls.

Third-party User Controls

A key reason for the existence of the User Control framework is to create a marketplace of user controls. Third-party independent software vendors can develop controls based on their core expertise (for instance, reporting controls, charting controls, and so on) and then make them available either commercially or using the Open Source licensing model. Users can then use these prebuilt sets of controls in their own applications and concentrate their efforts on developing business applications rather than spending months of effort in developing core controls.

THIRD-PARTY .NET CONTROLS

The real benefit of using controls in .NET Windows forms applications is that if you can abstract your application to utilize a set of loosely connected controls, chances are that you don't have to build it. There is a large set of third-party controls that are already built, ready to be deployed. A catalog of these controls is Component Source, http://www.componentsource.com.




Microsoft.Net Kick Start
Microsoft .NET Kick Start
ISBN: 0672325748
EAN: 2147483647
Year: 2003
Pages: 195
Authors: Hitesh Seth

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net