|
Similar to the grouping controls, a user control enables you to group a set of controls. Unlike the grouping controls, a user control allows the group of controls to act as one control. You can create your own properties and events on this control that will affect the way in which the grouped items behave. To better illustrate this point, consider the OpenDialog control. This control has many subcontrols, such as the ListView, TextBox, and Label, and OK and Cancel buttons. When you use the OpenDialog control, you don't write event handlers for all of these controlsthey already exist. Similarly, a user control enables you to visually create your own controls by designing the control exactly as you would a form. You can then use the control by dragging and dropping it from the toolbox to a desired form. Listing 16.3 contains the source code for a simple Logon user control. Listing 16.3. Logon User Controlusing System; using System.Collections; using System.ComponentModel; using System.Drawing; using System.Data; using System.Windows.Forms; namespace UserControlExample { /// <summary> /// Summary description for UserControl1. /// </summary> public class LogonUserControl : System.Windows.Forms.UserControl { private System.Windows.Forms.Button okButton; private System.Windows.Forms.Button cancelButton; private System.Windows.Forms.Label label1; private System.Windows.Forms.Label label2; private System.Windows.Forms.TextBox userName; private System.Windows.Forms.TextBox password; /// <summary> /// Required designer variable. /// </summary> private System.ComponentModel.Container components = null; public LogonUserControl() { // This call is required by the Windows.Forms Form Designer. InitializeComponent(); // TODO: Add any initialization after the InitComponent 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 // Windows Forms designer code intentionally left out for clarity. #endregion } } |
|