次の方法で共有


UserControl コンストラクタ

UserControl クラスの新しいインスタンスを初期化します。

Public Sub New()
[C#]
public UserControl();
[C++]
public: UserControl();
[JScript]
public function UserControl();

解説

一般的に、 UserControl のインスタンスは作成しません。独自のユーザー コントロール クラスを作成するには、 UserControl クラスから継承します。

使用例

[Visual Basic, C#, C++] UserControl の派生クラス MyCustomerInfoUserControl のインスタンスを作成する例を次に示します。この派生クラスは、 UserControl クラスの概要トピックの例で作成したものです。このユーザー コントロールは Panel コントロールに追加され、その Dock プロパティは DockStyle.Fill に設定されます。その後で、 PanelForm に追加されます。

 
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)
            Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
            ' 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

[C#] 
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);
         this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
         // 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

[C++] 
public __gc class MyUserControlHost : public System::Windows::Forms::Form 
{
   // Create the controls.
private:
   System::ComponentModel::IContainer* components;
   System::Windows::Forms::Panel* panel1;
   UserControls::MyCustomerInfoUserControl* myUserControl;

   // Define the constructor.
public:
   MyUserControlHost() 
   {
      this->InitializeComponent();
   }

   // 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 = System::Drawing::Point(5, 5);
      // Add the user control to the Panel.
      panel1->Controls->Add(myUserControl);
      this->AutoScaleBaseSize = System::Drawing::Size(5, 13);
      // Size the Form to accommodate the Panel.
      this->ClientSize = System::Drawing::Size(
         panel1->Size.Width + 10, panel1->Size.Height + 10);
      this->Text = S"Please enter the information below...";
      // Add the Panel to the Form.
      this->Controls->Add(panel1);
   }   
}; // End Class

[System::STAThreadAttribute]
int main() 
{
   System::Windows::Forms::Application::Run(new MyUserControlHost());
}

[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ

参照

UserControl クラス | UserControl メンバ | System.Windows.Forms 名前空間