This documentation is archived and is not being maintained.

UserControl Constructor

Initializes a new instance of the UserControl class.

Namespace: System.Windows.Forms
Assembly: System.Windows.Forms (in

UserControl ()
public UserControl ()
public function UserControl ()

You do not typically create an instance of UserControl. To create your own user control class, inherit from the UserControl class.

The following code example creates an instance of the UserControl derived class, MyCustomerInfoUserControl, which was created in the example section of the UserControl class overview. The user control is added to a Panel control and has its Dock property set to DockStyle.Fill. The Panel is then added to a Form.

public ref class MyUserControlHost: public System::Windows::Forms::Form

   // Create the controls.
   System::ComponentModel::IContainer^ components;
   System::Windows::Forms::Panel^ panel1;
   UserControls::MyCustomerInfoUserControl^ myUserControl;


   // Define the constructor.


   // Add a Panel control to a Form and host the UserControl in the Panel.
   void InitializeComponent()
      components = gcnew System::ComponentModel::Container;
      panel1 = gcnew System::Windows::Forms::Panel;
      myUserControl = gcnew UserControls::MyCustomerInfoUserControl;
      // Set the DockStyle of the UserControl to Fill.
      myUserControl->Dock = System::Windows::Forms::DockStyle::Fill;
      // Make the Panel the same size as the UserControl and give it a border.
      panel1->BorderStyle = System::Windows::Forms::BorderStyle::FixedSingle;
      panel1->Size = myUserControl->Size;
      panel1->Location = System::Drawing::Point( 5, 5 );
      // Add the user control to the Panel.
      panel1->Controls->Add( myUserControl );
      // Size the Form to accommodate the Panel.
      this->ClientSize = System::Drawing::Size( panel1->Size.Width + 10, panel1->Size.Height + 10 );
      this->Text = "Please enter the information below...";
      // Add the Panel to the Form.
      this->Controls->Add( panel1 );
// End Class

int main()
   System::Windows::Forms::Application::Run( gcnew MyUserControlHost );

package UserControls;

public class MyCustomerInfoUserControl extends System.Windows.Forms.UserControl
    // Create the controls.
    private System.Windows.Forms.ErrorProvider errorProvider1;
    private System.Windows.Forms.TextBox textName;
    private System.Windows.Forms.TextBox textAddress;
    private System.Windows.Forms.TextBox textCity;
    private System.Windows.Forms.TextBox textStateProvince;
    private System.Windows.Forms.TextBox textPostal;
    private System.Windows.Forms.TextBox textCountryRegion;
    private System.Windows.Forms.TextBox textEmail;
    private System.Windows.Forms.Label labelName;
    private System.Windows.Forms.Label labelAddress;
    private System.Windows.Forms.Label labelCityStateProvincePostal;
    private System.Windows.Forms.Label labelCountryRegion;
    private System.Windows.Forms.Label labelEmail;
    private System.ComponentModel.IContainer components;

    // Define the constructor.
    public MyCustomerInfoUserControl()
    } //MyCustomerInfoUserControl

    // Initialize the control elements.
    public void InitializeComponent()
        // Initialize the controls.
        components = new System.ComponentModel.Container();
        errorProvider1 = new System.Windows.Forms.ErrorProvider();
        textName = new System.Windows.Forms.TextBox();
        textAddress = new System.Windows.Forms.TextBox();
        textCity = new System.Windows.Forms.TextBox();
        textStateProvince = new System.Windows.Forms.TextBox();
        textPostal = new System.Windows.Forms.TextBox();
        textCountryRegion = new System.Windows.Forms.TextBox();
        textEmail = new System.Windows.Forms.TextBox();
        labelName = new System.Windows.Forms.Label();
        labelAddress = new System.Windows.Forms.Label();
        labelCityStateProvincePostal = new System.Windows.Forms.Label();
        labelCountryRegion = new System.Windows.Forms.Label();
        labelEmail = new System.Windows.Forms.Label();

        // Set the tab order, text alignment, size, 
        // and location of the controls.
        textName.set_Location(new System.Drawing.Point(120, 8));
        textName.set_Size(new System.Drawing.Size(232, 20));
        textAddress.set_Location(new System.Drawing.Point(120, 32));
        textAddress.set_Size(new System.Drawing.Size(232, 20));
        textCity.set_Location(new System.Drawing.Point(120, 56));
        textCity.set_Size(new System.Drawing.Size(96, 20));
        textStateProvince.set_Location(new System.Drawing.Point(216, 56));
        textStateProvince.set_Size(new System.Drawing.Size(56, 20));
        textPostal.set_Location(new System.Drawing.Point(272, 56));
        textPostal.set_Size(new System.Drawing.Size(80, 20));
        textCountryRegion.set_Location(new System.Drawing.Point(120, 80));
        textCountryRegion.set_Size(new System.Drawing.Size(232, 20));
        textEmail.set_Location(new System.Drawing.Point(120, 104));
        textEmail.set_Size(new System.Drawing.Size(232, 20));
        labelName.set_Location(new System.Drawing.Point(8, 8));
        labelName.set_Size(new System.Drawing.Size(112, 23));
        labelAddress.set_Location(new System.Drawing.Point(8, 32));
        labelAddress.set_Size(new System.Drawing.Size(112, 23));
            new System.Drawing.Point(8, 56));
        labelCityStateProvincePostal.set_Size(new System.Drawing.Size(112, 23));
        labelCityStateProvincePostal.set_Text("City, St/Prov. Postal:");
        labelCountryRegion.set_Location(new System.Drawing.Point(8, 80));
        labelCountryRegion.set_Size(new System.Drawing.Size(112, 23));
        labelEmail.set_Location(new System.Drawing.Point(8, 104));
        labelEmail.set_Size(new System.Drawing.Size(112, 23));

        // Add the Validating and Validated handlers for textEmail.
        textEmail.add_Validating(new System.ComponentModel.CancelEventHandler(
        textEmail.add_Validated(new System.EventHandler(textEmail_Validated));

        // Add the controls to the user control.
        get_Controls().AddRange(new System.Windows.Forms.Control[] {
            labelName, labelAddress, labelCityStateProvincePostal, 
            labelCountryRegion, labelEmail, textName, textAddress, textCity,
            textStateProvince, textPostal, textCountryRegion, textEmail    

        // Size the user control.
        set_Size(new System.Drawing.Size(375, 150));
    } //InitializeComponent

    private void MyValidatingCode() throws Exception
        // Confirm there is text in the control.
        if (textEmail.get_Text().length() == 0) {
            throw new Exception("Email address is a required field.");
        // Confirm that there is a "." and an "@" in the e-mail address.
        else {
            if (textEmail.get_Text().IndexOf(".") == -1 ||
                    textEmail.get_Text().IndexOf("@") == -1) {
                throw new Exception("Email address must be valid e-mail "
                    + "address format."
                    + "\nFor example: ''");
    } //MyValidatingCode

    // Validate the data input by the user into textEmail.
    private void textEmail_Validating(Object sender,
        System.ComponentModel.CancelEventArgs e)    
        try {
        catch (System.Exception ex) {
            // Cancel the event and select the text to be corrected by the user.
            textEmail.Select(0, textEmail.get_Text().length());

            // Set the ErrorProvider error with the text to display. 
            this.errorProvider1.SetError(textEmail, ex.get_Message());
    } //textEmail_Validating

    private void textEmail_Validated(Object sender, System.EventArgs e)
        //If all conditions have been met, clear the error provider of errors.
        errorProvider1.SetError(textEmail, "");
    } //textEmail_Validated
} //MyCustomerInfoUserControl     

package MyApplication ;

import System.*;
import System.Drawing.*;
import System.Windows.Forms.*;
import System.ComponentModel.*;
import UserControls.*;

public class MyUserControlHost extends System.Windows.Forms.Form
    // Create the controls.
    private System.ComponentModel.IContainer components;
    private System.Windows.Forms.Panel panel1;
    private UserControls.MyCustomerInfoUserControl myUserControl;

    // Define the constructor.
    public MyUserControlHost()
    } //MyUserControlHost

    /** @attribute System.STAThreadAttribute()
    public static void main(String[] args)
        System.Windows.Forms.Application.Run(new MyUserControlHost());
    } //main

    // Add a Panel control to a Form and host the UserControl in the Panel.
    private void InitializeComponent()
        components = new System.ComponentModel.Container();
        panel1 = new System.Windows.Forms.Panel();
        myUserControl = new UserControls.MyCustomerInfoUserControl();

        // Set the DockStyle of the UserControl to Fill.

        // Make the Panel the same size as the UserControl and give it a border.
        panel1.set_Location(new System.Drawing.Point(5, 5));

        // Add the user control to the Panel.

        // Size the Form to accommodate the Panel.
        this.set_ClientSize(new System.Drawing.Size(
            panel1.get_Size().get_Width() + 10,
            panel1.get_Size().get_Height() + 10));
        this.set_Text("Please enter the information below...");

        // Add the Panel to the Form.
    } //InitializeComponent
} //MyUserControlHost 

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0