Export (0) Print
Expand All

UserControl Constructor

Initializes a new instance of the UserControl class.

Namespace: System.Windows.Forms
Assembly: System.Windows.Forms (in system.windows.forms.dll)

'Declaration
Public Sub New
'Usage
Dim instance As New 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.

Imports System
Imports System.Drawing
Imports System.Windows.Forms
Imports System.ComponentModel
Imports Microsoft.VisualBasic
Imports UserControls

Namespace MyApplication    
    
    Public Class MyUserControlHost
        Inherits System.Windows.Forms.Form

        ' Create the controls.
        Private components As System.ComponentModel.IContainer
        Private panel1 As System.Windows.Forms.Panel
        Private myUserControl As UserControls.MyCustomerInfoUserControl
                
        ' Define the constructor.
        Public Sub New()
            Me.InitializeComponent()
        End Sub        
        
        <System.STAThreadAttribute()> _
        Public Shared Sub Main()
            System.Windows.Forms.Application.Run(New MyUserControlHost())
        End Sub        
        
        ' Add a Panel control to a Form and host the UserControl in the Panel.
        Private Sub InitializeComponent()
            components = New System.ComponentModel.Container()
            panel1 = New System.Windows.Forms.Panel()
            myUserControl = New 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 = New System.Drawing.Point(5, 5)
            ' Add the user control to the Panel.
            panel1.Controls.Add(myUserControl)
            ' Size the Form to accommodate the Panel.
            Me.ClientSize = New System.Drawing.Size(panel1.Size.Width + 10, panel1.Size.Height + 10)
            Me.Text = "Please enter the information below..."
            ' Add the Panel to the Form.
            Me.Controls.Add(panel1)
        End Sub
    End Class
End Namespace

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()
    {
        InitializeComponent();
    } //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));
        textName.set_TabIndex(0);
        textAddress.set_Location(new System.Drawing.Point(120, 32));
        textAddress.set_Size(new System.Drawing.Size(232, 20));
        textAddress.set_TabIndex(1);
        textCity.set_Location(new System.Drawing.Point(120, 56));
        textCity.set_Size(new System.Drawing.Size(96, 20));
        textCity.set_TabIndex(2);
        textStateProvince.set_Location(new System.Drawing.Point(216, 56));
        textStateProvince.set_Size(new System.Drawing.Size(56, 20));
        textStateProvince.set_TabIndex(3);
        textPostal.set_Location(new System.Drawing.Point(272, 56));
        textPostal.set_Size(new System.Drawing.Size(80, 20));
        textPostal.set_TabIndex(4);
        textCountryRegion.set_Location(new System.Drawing.Point(120, 80));
        textCountryRegion.set_Size(new System.Drawing.Size(232, 20));
        textCountryRegion.set_TabIndex(5);
        textEmail.set_Location(new System.Drawing.Point(120, 104));
        textEmail.set_Size(new System.Drawing.Size(232, 20));
        textEmail.set_TabIndex(6);
        labelName.set_Location(new System.Drawing.Point(8, 8));
        labelName.set_Size(new System.Drawing.Size(112, 23));
        labelName.set_Text("Name:");
        labelName.set_TextAlign(System.Drawing.ContentAlignment.MiddleRight);
        labelAddress.set_Location(new System.Drawing.Point(8, 32));
        labelAddress.set_Size(new System.Drawing.Size(112, 23));
        labelAddress.set_Text("Address:");
        labelAddress.set_TextAlign(System.Drawing.ContentAlignment.MiddleRight);
        labelCityStateProvincePostal.set_Location(
            new System.Drawing.Point(8, 56));
        labelCityStateProvincePostal.set_Size(new System.Drawing.Size(112, 23));
        labelCityStateProvincePostal.set_Text("City, St/Prov. Postal:");
        labelCityStateProvincePostal.set_TextAlign(
            System.Drawing.ContentAlignment.MiddleRight);
        labelCountryRegion.set_Location(new System.Drawing.Point(8, 80));
        labelCountryRegion.set_Size(new System.Drawing.Size(112, 23));
        labelCountryRegion.set_Text("Country/Region:");
        labelCountryRegion.set_TextAlign(
            System.Drawing.ContentAlignment.MiddleRight);
        labelEmail.set_Location(new System.Drawing.Point(8, 104));
        labelEmail.set_Size(new System.Drawing.Size(112, 23));
        labelEmail.set_Text("email:");
        labelEmail.set_TextAlign(System.Drawing.ContentAlignment.MiddleRight);

        // Add the Validating and Validated handlers for textEmail.
        textEmail.add_Validating(new System.ComponentModel.CancelEventHandler(
            textEmail_Validating));
        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: 'someone@example.com'");
            }
        }
    } //MyValidatingCode

    // Validate the data input by the user into textEmail.
    private void textEmail_Validating(Object sender,
        System.ComponentModel.CancelEventArgs e)    
    {
        try {
            MyValidatingCode();
        }
        catch (System.Exception ex) {
            // Cancel the event and select the text to be corrected by the user.
            e.set_Cancel(true);
            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()
    {
        this.InitializeComponent();
    } //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.
        myUserControl.set_Dock(System.Windows.Forms.DockStyle.Fill);

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

        // Add the user control to the Panel.
        panel1.get_Controls().Add(myUserControl);

        // 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.
        this.get_Controls().Add(panel1);
    } //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

Community Additions

ADD
Show:
© 2014 Microsoft