Export (0) Print
Expand All
2 out of 2 rated this helpful - Rate this topic

UserControl Constructor

Initializes a new instance of the UserControl class.

Namespace:  System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)
public 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.

using System;
using System.Drawing;
using System.Windows.Forms;
using System.ComponentModel;
using UserControls;

namespace MyApplication 
{

   public class MyUserControlHost : 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();
      }

      [System.STAThreadAttribute()]
      public static void Main() 
      {
         System.Windows.Forms.Application.Run(new MyUserControlHost());
      }

      // 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.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. 
         this.ClientSize = new 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
} // End Namespace

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

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

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 3.5, 2.0
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.