다음을 통해 공유


UserControl 클래스

다른 컨트롤을 만드는 데 사용할 수 있는 빈 컨트롤을 제공합니다.

네임스페이스: System.Windows.Forms
어셈블리: System.Windows.Forms(system.windows.forms.dll)

구문

‘선언
<ComVisibleAttribute(True)> _
<ClassInterfaceAttribute(ClassInterfaceType.AutoDispatch)> _
Public Class UserControl
    Inherits ContainerControl
‘사용 방법
Dim instance As UserControl
[ComVisibleAttribute(true)] 
[ClassInterfaceAttribute(ClassInterfaceType.AutoDispatch)] 
public class UserControl : ContainerControl
[ComVisibleAttribute(true)] 
[ClassInterfaceAttribute(ClassInterfaceType::AutoDispatch)] 
public ref class UserControl : public ContainerControl
/** @attribute ComVisibleAttribute(true) */ 
/** @attribute ClassInterfaceAttribute(ClassInterfaceType.AutoDispatch) */ 
public class UserControl extends ContainerControl
ComVisibleAttribute(true) 
ClassInterfaceAttribute(ClassInterfaceType.AutoDispatch) 
public class UserControl extends ContainerControl

설명

ContainerControl을 확장하면, 사용자 정의 컨트롤에 필요한 모든 표준 위치 지정 및 니모닉 처리 코드가 UserControl로 상속됩니다.

그러면 UserControl을 사용하여 응용 프로그램이나 조직 내의 여러 위치에서 사용할 수 있는 컨트롤을 만들 수 있습니다. 전자 메일 주소(예제 부분 참조), 전화 번호 및 우편 번호와 같이 사용자가 입력해야 하는 공용 데이터가 유효한지 확인하는 데 필요한 모든 코드를 포함시킬 수 있습니다. 사용자 정의 컨트롤은 국가/지역, 구/군/시, 시/도 및 사무실 위치와 같이 거의 모든 응용 프로그램에서 공통적으로 사용하는 정적 항목이 있는 ComboBox 또는 ListBox를 미리 로드하는 데도 사용할 수 있습니다. 사용자 지정 컨트롤 제작에 대한 자세한 내용은 .NET Framework에서 사용자 지정 Windows Forms 컨트롤 개발을 참조하십시오.

참고

사용자 정의 컨트롤의 여러 클래스를 포함하는 네임스페이스를 만들어 하나의 DLL로 컴파일하는 것을 고려할 수 있습니다. 이 DLL은 응용 프로그램이나 조직 내의 모든 응용 프로그램에서 참조되고 배포될 수 있습니다. 이를 통해 많은 응용 프로그램에서 사용자 정의 컨트롤을 참조할 수 있으며 사용자 정의 컨트롤의 포함된 요소를 레이아웃하거나 코딩하는 시간을 절약할 수 있습니다. 또한 사용자 정의 컨트롤을 사용하면 응용 프로그램 내에서 또는 응용 프로그램 간에 일관성을 유지할 수 있습니다. 예를 들어, 모든 주소 정보 입력 블록이 동일한 모양과 동작을 나타낼 수 있습니다. 이러한 일관성을 통해 응용 프로그램은 보다 훌륭하고 전문적인 모습을 갖추게 됩니다.

폼 내부, 다른 UserControl 내부, Internet Explorer의 웹 페이지 내부 또는 폼에 호스팅된 WebBrowser 컨트롤 내부에 Windows Forms UserControl 파생 클래스를 호스팅할 수 있습니다.

참고

UserControlWebBrowser 컨트롤 내부에 호스팅할 경우에는 META 태그 값 MSThemeCompatible을 사용하여 비주얼 스타일을 해제할 수 없습니다. 비주얼 스타일에 대한 자세한 내용은 비주얼 스타일을 사용하여 컨트롤 렌더링을 참조하십시오.

Smartphone 응용 프로그램에서 이 컨트롤을 사용하려면 Smartphone용 Windows Mobile 버전 5.0 소프트웨어가 있어야 합니다.

예제

다음 코드 예제에서는 사용자 정보를 가져오기 위해 여러 응용 프로그램에서 다시 사용할 수 있는 UserControl을 만듭니다. 이 예제에서는 여러 개의 Label 컨트롤과 TextBox 컨트롤 및 ErrorProvider 하나를 UserControl에 추가하여 사용자의 정보를 수집합니다. 또한 TextBoxValidating 이벤트에서 사용자의 전자 메일 주소가 유효한지 확인되며 해당 데이터가 유효하지 않을 경우 ErrorProvider 개체를 사용하여 사용자에게 피드백을 제공합니다. 이 코드는 다른 응용 프로그램에서 참조할 수 있도록 DLL로 컴파일하기 위한 것입니다.

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

Namespace UserControls

   Public Class MyCustomerInfoUserControl
      Inherits System.Windows.Forms.UserControl

      ' Create the controls.
      Private errorProvider1 As System.Windows.Forms.ErrorProvider
      Private textName As System.Windows.Forms.TextBox
      Private textAddress As System.Windows.Forms.TextBox
      Private textCity As System.Windows.Forms.TextBox
      Private textStateProvince As System.Windows.Forms.TextBox
      Private textPostal As System.Windows.Forms.TextBox
      Private textCountryRegion As System.Windows.Forms.TextBox
      Private WithEvents textEmail As System.Windows.Forms.TextBox
      Private labelName As System.Windows.Forms.Label
      Private labelAddress As System.Windows.Forms.Label
      Private labelCityStateProvincePostal As System.Windows.Forms.Label
      Private labelCountryRegion As System.Windows.Forms.Label
      Private labelEmail As System.Windows.Forms.Label
      Private components As System.ComponentModel.IContainer        
        
      ' Define the constructor.
      Public Sub New()
         InitializeComponent()
      End Sub        
        
      ' Initialize the control elements.
      Public Sub 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.Location = New System.Drawing.Point(120, 8)
         textName.Size = New System.Drawing.Size(232, 20)
         textName.TabIndex = 0

         textAddress.Location = New System.Drawing.Point(120, 32)
         textAddress.Size = New System.Drawing.Size(232, 20)
         textAddress.TabIndex = 1

         textCity.Location = New System.Drawing.Point(120, 56)
         textCity.Size = New System.Drawing.Size(96, 20)
         textCity.TabIndex = 2

         textStateProvince.Location = New System.Drawing.Point(216, 56)
         textStateProvince.Size = New System.Drawing.Size(56, 20)
         textStateProvince.TabIndex = 3

         textPostal.Location = New System.Drawing.Point(272, 56)
         textPostal.Size = New System.Drawing.Size(80, 20)
         textPostal.TabIndex = 4

         textCountryRegion.Location = New System.Drawing.Point(120, 80)
         textCountryRegion.Size = New System.Drawing.Size(232, 20)
         textCountryRegion.TabIndex = 5

         textEmail.Location = New System.Drawing.Point(120, 104)
         textEmail.Size = New System.Drawing.Size(232, 20)
         textEmail.TabIndex = 6

         labelName.Location = New System.Drawing.Point(8, 8)
         labelName.Size = New System.Drawing.Size(112, 23)
         labelName.Text = "Name:"
         labelName.TextAlign = System.Drawing.ContentAlignment.MiddleRight

         labelAddress.Location = New System.Drawing.Point(8, 32)
         labelAddress.Size = New System.Drawing.Size(112, 23)
         labelAddress.Text = "Address:"
         labelAddress.TextAlign = System.Drawing.ContentAlignment.MiddleRight

         labelCityStateProvincePostal.Location = New System.Drawing.Point(8, 56)
         labelCityStateProvincePostal.Size = New System.Drawing.Size(112, 23)
         labelCityStateProvincePostal.Text = "City, St/Prov. Postal:"
         labelCityStateProvincePostal.TextAlign = System.Drawing.ContentAlignment.MiddleRight

         labelCountryRegion.Location = New System.Drawing.Point(8, 80)
         labelCountryRegion.Size = New System.Drawing.Size(112, 23)
         labelCountryRegion.Text = "Country/Region:"
         labelCountryRegion.TextAlign = System.Drawing.ContentAlignment.MiddleRight

         labelEmail.Location = New System.Drawing.Point(8, 104)
         labelEmail.Size = New System.Drawing.Size(112, 23)
         labelEmail.Text = "email:"
         labelEmail.TextAlign = System.Drawing.ContentAlignment.MiddleRight
          
         ' Add the controls to the user control.
         Controls.AddRange(New System.Windows.Forms.Control() {labelName, _
           labelAddress, labelCityStateProvincePostal, labelCountryRegion, _
           labelEmail, textName, textAddress, textCity, textStateProvince, _
           textPostal, textCountryRegion, textEmail})
            
         ' Size the user control.
         Size = New System.Drawing.Size(375, 150)
      End Sub        

      Private Sub MyValidatingCode()
         ' Confirm there is text in the control.
         If textEmail.Text.Length = 0 Then
            Throw New Exception("Email address is a required field")
         Else
            ' Confirm that there is a "." and an "@" in the e-mail address.
            If textEmail.Text.IndexOf(".") = - 1 Or textEmail.Text.IndexOf("@") = - 1 Then
               Throw New Exception("Email address must be valid e-mail address format." + _
                 Microsoft.VisualBasic.ControlChars.Cr + "For example 'someone@example.com'")
            End If
         End If
      End Sub 

      ' Validate the data input by the user into textEmail.
      Private Sub textEmail_Validating(sender As Object, _
                                       e As System.ComponentModel.CancelEventArgs) Handles textEmail.Validating
         Try
            MyValidatingCode()
   
         Catch ex As Exception
            ' Cancel the event and select the text to be corrected by the user.
            e.Cancel = True
            textEmail.Select(0, textEmail.Text.Length)
      
            ' Set the ErrorProvider error with the text to display. 
            Me.errorProvider1.SetError(textEmail, ex.Message)
         End Try
      End Sub 


      Private Sub textEmail_Validated(sender As Object, _
                                      e As System.EventArgs) Handles textEmail.Validated
         ' If all conditions have been met, clear the error provider of errors.
         errorProvider1.SetError(textEmail, "")
      End Sub        

   End Class
End Namespace
using System;
using System.Windows.Forms;
using System.Drawing;
using System.ComponentModel;

namespace UserControls 
{
   public class MyCustomerInfoUserControl : 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();
      }
 
      // 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.Location = new System.Drawing.Point(120, 8);
         textName.Size = new System.Drawing.Size(232, 20);
         textName.TabIndex = 0;

         textAddress.Location = new System.Drawing.Point(120, 32);
         textAddress.Size = new System.Drawing.Size(232, 20);
         textAddress.TabIndex = 1;

         textCity.Location = new System.Drawing.Point(120, 56);
         textCity.Size = new System.Drawing.Size(96, 20);
         textCity.TabIndex = 2;

         textStateProvince.Location = new System.Drawing.Point(216, 56);
         textStateProvince.Size = new System.Drawing.Size(56, 20);
         textStateProvince.TabIndex = 3;

         textPostal.Location = new System.Drawing.Point(272, 56);
         textPostal.Size = new System.Drawing.Size(80, 20);
         textPostal.TabIndex = 4;

         textCountryRegion.Location = new System.Drawing.Point(120, 80);
         textCountryRegion.Size = new System.Drawing.Size(232, 20);
         textCountryRegion.TabIndex = 5;

         textEmail.Location = new System.Drawing.Point(120, 104);
         textEmail.Size = new System.Drawing.Size(232, 20);
         textEmail.TabIndex = 6;

         labelName.Location = new System.Drawing.Point(8, 8);
         labelName.Size = new System.Drawing.Size(112, 23);
         labelName.Text = "Name:";
         labelName.TextAlign = System.Drawing.ContentAlignment.MiddleRight;

         labelAddress.Location = new System.Drawing.Point(8, 32);
         labelAddress.Size = new System.Drawing.Size(112, 23);
         labelAddress.Text = "Address:";
         labelAddress.TextAlign = System.Drawing.ContentAlignment.MiddleRight;

         labelCityStateProvincePostal.Location = new System.Drawing.Point(8, 56);
         labelCityStateProvincePostal.Size = new System.Drawing.Size(112, 23);
         labelCityStateProvincePostal.Text = "City, St/Prov. Postal:";
         labelCityStateProvincePostal.TextAlign = System.Drawing.ContentAlignment.MiddleRight;

         labelCountryRegion.Location = new System.Drawing.Point(8, 80);
         labelCountryRegion.Size = new System.Drawing.Size(112, 23);
         labelCountryRegion.Text = "Country/Region:";
         labelCountryRegion.TextAlign = System.Drawing.ContentAlignment.MiddleRight;

         labelEmail.Location = new System.Drawing.Point(8, 104);
         labelEmail.Size = new System.Drawing.Size(112, 23);
         labelEmail.Text = "email:";
         labelEmail.TextAlign = System.Drawing.ContentAlignment.MiddleRight;

         // Add the Validating and Validated handlers for textEmail.
         textEmail.Validating += new System.ComponentModel.CancelEventHandler(textEmail_Validating);
         textEmail.Validated += new System.EventHandler(textEmail_Validated);

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

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


      private void MyValidatingCode()
      {
         // Confirm there is text in the control.
         if (textEmail.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.Text.IndexOf(".") == -1 || textEmail.Text.IndexOf("@") == -1)
         {
            throw new Exception("Email address must be valid e-mail address format." +
             "\nFor example: 'someone@example.com'");
         }
      }


      // Validate the data input by the user into textEmail.
      private void textEmail_Validating(object sender, System.ComponentModel.CancelEventArgs e)
      { 
         try
         {
            MyValidatingCode();
         }

         catch(Exception ex)
         {
            // Cancel the event and select the text to be corrected by the user.
            e.Cancel = true;
            textEmail.Select(0, textEmail.Text.Length);

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


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

   } // End Class   
} // End Namespace
#using <System.dll>
#using <System.Drawing.dll>
#using <System.Windows.Forms.dll>

using namespace System;
using namespace System::Windows::Forms;
using namespace System::Drawing;
using namespace System::ComponentModel;

namespace UserControls
{
   public ref class MyCustomerInfoUserControl: public System::Windows::Forms::UserControl
   {
   private:

      // Create the controls.
      System::Windows::Forms::ErrorProvider^ errorProvider1;
      System::Windows::Forms::TextBox^ textName;
      System::Windows::Forms::TextBox^ textAddress;
      System::Windows::Forms::TextBox^ textCity;
      System::Windows::Forms::TextBox^ textStateProvince;
      System::Windows::Forms::TextBox^ textPostal;
      System::Windows::Forms::TextBox^ textCountryRegion;
      System::Windows::Forms::TextBox^ textEmail;
      System::Windows::Forms::Label ^ labelName;
      System::Windows::Forms::Label ^ labelAddress;
      System::Windows::Forms::Label ^ labelCityStateProvincePostal;
      System::Windows::Forms::Label ^ labelCountryRegion;
      System::Windows::Forms::Label ^ labelEmail;
      System::ComponentModel::IContainer^ components;

   public:

      // Define the constructor.
      MyCustomerInfoUserControl()
      {
         InitializeComponent();
      }

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

         // Set the tab order, text alignment, size, and location of the controls.
         textName->Location = System::Drawing::Point( 120, 8 );
         textName->Size = System::Drawing::Size( 232, 20 );
         textName->TabIndex = 0;
         textAddress->Location = System::Drawing::Point( 120, 32 );
         textAddress->Size = System::Drawing::Size( 232, 20 );
         textAddress->TabIndex = 1;
         textCity->Location = System::Drawing::Point( 120, 56 );
         textCity->Size = System::Drawing::Size( 96, 20 );
         textCity->TabIndex = 2;
         textStateProvince->Location = System::Drawing::Point( 216, 56 );
         textStateProvince->Size = System::Drawing::Size( 56, 20 );
         textStateProvince->TabIndex = 3;
         textPostal->Location = System::Drawing::Point( 272, 56 );
         textPostal->Size = System::Drawing::Size( 80, 20 );
         textPostal->TabIndex = 4;
         textCountryRegion->Location = System::Drawing::Point( 120, 80 );
         textCountryRegion->Size = System::Drawing::Size( 232, 20 );
         textCountryRegion->TabIndex = 5;
         textEmail->Location = System::Drawing::Point( 120, 104 );
         textEmail->Size = System::Drawing::Size( 232, 20 );
         textEmail->TabIndex = 6;
         labelName->Location = System::Drawing::Point( 8, 8 );
         labelName->Size = System::Drawing::Size( 112, 23 );
         labelName->Text = "Name:";
         labelName->TextAlign = System::Drawing::ContentAlignment::MiddleRight;
         labelAddress->Location = System::Drawing::Point( 8, 32 );
         labelAddress->Size = System::Drawing::Size( 112, 23 );
         labelAddress->Text = "Address:";
         labelAddress->TextAlign = System::Drawing::ContentAlignment::MiddleRight;
         labelCityStateProvincePostal->Location = System::Drawing::Point( 8, 56 );
         labelCityStateProvincePostal->Size = System::Drawing::Size( 112, 23 );
         labelCityStateProvincePostal->Text = "City, St/Prov. Postal:";
         labelCityStateProvincePostal->TextAlign = System::Drawing::ContentAlignment::MiddleRight;
         labelCountryRegion->Location = System::Drawing::Point( 8, 80 );
         labelCountryRegion->Size = System::Drawing::Size( 112, 23 );
         labelCountryRegion->Text = "Country/Region:";
         labelCountryRegion->TextAlign = System::Drawing::ContentAlignment::MiddleRight;
         labelEmail->Location = System::Drawing::Point( 8, 104 );
         labelEmail->Size = System::Drawing::Size( 112, 23 );
         labelEmail->Text = "email:";
         labelEmail->TextAlign = System::Drawing::ContentAlignment::MiddleRight;

         // Add the Validating and Validated handlers for textEmail.
         textEmail->Validating += gcnew System::ComponentModel::CancelEventHandler( this, &MyCustomerInfoUserControl::textEmail_Validating );
         textEmail->Validated += gcnew System::EventHandler( this, &MyCustomerInfoUserControl::textEmail_Validated );

         // Add the controls to the user control.
         array<System::Windows::Forms::Control^>^temp0 = {labelName,labelAddress,labelCityStateProvincePostal,labelCountryRegion,labelEmail,textName,textAddress,textCity,textStateProvince,textPostal,textCountryRegion,textEmail};
         Controls->AddRange( temp0 );

         // Size the user control.
         Size = System::Drawing::Size( 375, 150 );
      }

   private:
      void MyValidatingCode()
      {
         // Confirm there is text in the control.
         if ( textEmail->Text->Length == 0 )
         {
            throw gcnew Exception( "Email address is a required field." );
         }
         // Confirm that there is a "." and an "@" in the e-mail address.
         else

         // Confirm that there is a "." and an "@" in the e-mail address.
         if ( textEmail->Text->IndexOf( "." ) == -1 || textEmail->Text->IndexOf( "@" ) == -1 )
         {
            throw gcnew Exception( "Email address must be valid e-mail address format.\nFor example: 'someone@example.com'" );
         }
      }

      // Validate the data input by the user into textEmail.
      void textEmail_Validating( Object^ /*sender*/, System::ComponentModel::CancelEventArgs^ e )
      {
         try
         {
            MyValidatingCode();
         }
         catch ( Exception^ ex ) 
         {
            // Cancel the event and select the text to be corrected by the user.
            e->Cancel = true;
            textEmail->Select(0,textEmail->Text->Length);
            
            // Set the ErrorProvider error with the text to display. 
            this->errorProvider1->SetError( textEmail, ex->Message );
         }
      }

      void textEmail_Validated( Object^ /*sender*/, System::EventArgs^ /*e*/ )
      {
         //If all conditions have been met, clear the error provider of errors.
         errorProvider1->SetError( textEmail, "" );
      }
   };
}

// End Class   
// End Namespace
package UserControls; 

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

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 (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
} //End Class MyCustomerInfoUserControl 

상속 계층 구조

System.Object
   System.MarshalByRefObject
     System.ComponentModel.Component
       System.Windows.Forms.Control
         System.Windows.Forms.ScrollableControl
           System.Windows.Forms.ContainerControl
            System.Windows.Forms.UserControl
               System.Web.UI.Design.WebControls.ParameterEditorUserControl

스레드로부터의 안전성

이 형식의 모든 public static(Visual Basic의 경우 Shared) 멤버는 스레드로부터 안전합니다. 인터페이스 멤버는 스레드로부터 안전하지 않습니다.

플랫폼

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

.NET Framework에서 모든 플래폼의 모든 버전을 지원하지는 않습니다. 지원되는 버전의 목록은 시스템 요구 사항을 참조하십시오.

버전 정보

.NET Framework

2.0, 1.1, 1.0에서 지원

.NET Compact Framework

2.0에서 지원

참고 항목

참조

UserControl 멤버
System.Windows.Forms 네임스페이스
ContainerControl 클래스
Form 클래스