UserControl クラスとは? わかりやすく解説

UserControl クラス

ASP.NET Web アプリケーションホストであるサーバーから要求され.ascx ファイル表します。このファイルユーザー コントロールとも呼ばれます。このファイルWeb フォーム ページから呼び出してください。このページから呼び出さないと、解析エラー発生します

名前空間: System.Web.UI
アセンブリ: System.Web (system.web.dll 内)
構文構文

Public Class UserControl
    Inherits TemplateControl
    Implements IAttributeAccessor, INamingContainer, IUserControlDesignerAccessor
public class UserControl : TemplateControl,
 IAttributeAccessor, INamingContainer, IUserControlDesignerAccessor
public ref class UserControl : public
 TemplateControl, IAttributeAccessor, INamingContainer, IUserControlDesignerAccessor
public class UserControl extends TemplateControl
 implements IAttributeAccessor, INamingContainer, 
    IUserControlDesignerAccessor
public class UserControl extends
 TemplateControl implements IAttributeAccessor, INamingContainer, 
    IUserControlDesignerAccessor
解説解説

UserControl クラスは、.ascx 拡張子を持つファイル関連付けられています。これらのファイルは、実行時UserControl オブジェクトとしてコンパイルされ、サーバー メモリキャッシュされます

ある .ascx ファイルを他のファイル宣言し後者Web フォーム ページ挿入することにより、ユーザー コントロール入れ子できます

ユーザー コントロールASP.NET Web フォーム ページ格納されており、Web 開発者は通常使用する Web UI簡単に取り込むことができますユーザー コントロールは、Page オブジェクト似た方法インスタンス化およびキャッシュされます。ただし、ユーザー コントロールは、ページとは異なり独立して呼び出すことはできません。ユーザー コントロールは、ページまたはページ格納している他のユーザー コントロールからだけ呼び出すことができます

分離コード技法使用してユーザー コントロール作成する必要がある場合は、このクラスから派生させます。この技法使用して Web フォーム ページ開発している場合お勧めます。

ユーザー コントロール作成方法については、「ASP.NET ユーザー コントロール」を参照してください

使用例使用例

UserControl クラス継承しASP.NET 分離コード クラスとして使用できるクラス (SimpleControl) の定義例次に示します。このクラスでは、TextBoxLabelButton3 つの Web サーバー コントロール使用しますまた、TextBox.Text プロパティの値を他の 2 つ文字列連結して Label.Text プロパティ設定する myButton_Click メソッド定義してます。

Imports System
Imports System.Web.UI
Imports System.Web.UI.WebControls


Public Class SimpleControl
   Inherits UserControl
   Public name As TextBox
   Public output As Label
   Public myButton As Button
   
   
   Public Sub myButton_Click(sender As
 Object, e As EventArgs)

      output.Text = "Hello, " + name.Text + "."

   End Sub  

End Class
using System;
using System.Web.UI;
using System.Web.UI.WebControls;

public class SimpleControl:UserControl
{

  public TextBox name;
  public Label output;
  public Button myButton;

  public void myButton_Click(object sender,
 EventArgs e)
  { 
    output.Text = "Hello, " + name.Text + ".";

  }
 
}
import System.*;
import System.Web.UI.*;
import System.Web.UI.WebControls.*;

public class SimpleControl extends UserControl
{
    public TextBox name;
    public Label output;
    public Button myButton;

    public void myButton_Click(Object sender,
 EventArgs e)
    {
        output.set_Text("Hello, " + name.get_Text() + ".");
    } //myButton_Click 
} //SimpleControl

.ascx ファイル含まれるマークアップの例次に示します。前の例の SimpleControl クラスを、この .ascx ファイルマークアップ分離コード クラスとして使用できます

<%@ control inherits = "SimpleControl"
 src = "SimpleControl.vb" %>

<table style="background-color: yellow; font: 10pt verdana;border-width:1;border-style:solid;border-color:black;"
 cellspacing=15>
<tr>
<td><b>Enter your name here: </b></td>
<td><ASP:TextBox id="name" runat="server"/></td>
</tr>
<tr>
<td><b><ASP:Label id="output" runat="server"/></b></td>
</tr>
<tr>
<td></td>
<td><asp:button text="Submit" OnClick="myButton_Click"
 runat="server" /></td>
</tr>
</table>
<%@ control inherits = "SimpleControl" src = "SimpleControl.cs"
 %>

<table style="background-color:yellow;font: 10pt verdana;border-width:1;border-style:solid;border-color:black;"
 cellspacing=15>
<tr>
<td><b>Enter your name here: </b></td>
<td><ASP:TextBox id="name" runat="server"/></td>
</tr>
<tr>
<td><b><ASP:Label id="output" runat="server"/></b></td>
</tr>
<tr>
<td></td>
<td><asp:button id="myButton" text="Submit" OnClick="myButton_Click"
 runat="server" /></td>
</tr>
</table>
<%@ control inherits = "SimpleControl" src = "SimpleControl.jsl"
 %>

<table style="background-color:yellow;font: 10pt verdana;border-width:1;border-style:solid;border-color:black;"
 cellspacing=15>
<tr>
<td><b>Enter your name here: </b></td>
<td><ASP:TextBox id="name" runat="server"/></td>
</tr>
<tr>
<td><b><ASP:Label id="output" runat="server"/></b></td>
</tr>
<tr>
<td></td>
<td><asp:button id="myButton" text="Submit" OnClick="myButton_Click"
 runat="server" /></td>
</tr>
</table>
.NET Framework のセキュリティ.NET Frameworkセキュリティ
継承階層継承階層
System.Object
   System.Web.UI.Control
     System.Web.UI.TemplateControl
      System.Web.UI.UserControl
         System.Web.UI.MasterPage
         System.Web.UI.MobileControls.MobileUserControl
スレッド セーフスレッド セーフ
この型の public static (Visual Basic では Shared) メンバはすべて、スレッド セーフです。インスタンス メンバ場合は、スレッド セーフであるとは限りません。
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照

UserControl クラス

ほかのコントロール作成するために使用できる空のコントロール提供します

名前空間: System.Windows.Forms
アセンブリ: System.Windows.Forms (system.windows.forms.dll 内)
構文構文

<ComVisibleAttribute(True)> _
<ClassInterfaceAttribute(ClassInterfaceType.AutoDispatch)> _
Public Class UserControl
    Inherits ContainerControl
[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
解説解説

UserControl は、ContainerControl拡張したクラスであり、ユーザー コントロールで必要となる標準位置設定コードおよびニーモニック処理コードをすべて継承してます。

UserControl使用すると、1 つアプリケーションまたは構成内の複数の場所使用できるコントロール作成できます電子メール アドレス (後の例参照)、電話番号郵便番号など、ユーザー入力してもらう共通データ検証するために必要となるコード含めることもできますユーザー コントロール別の効率的な使用方法として、多くアプリケーション共通して使用される静的な項目 (国や地域市町村名都道府県名、オフィスの所在地など) を含む ComboBox または ListBox簡単にプリロードしておくことができますカスタム コントロール作成方法詳細については、「.NET Framework使用したカスタム Windows フォーム コントロール開発」を参照してください

メモメモ

ユーザー コントロールクラスいくつか含んでいる名前空間作成し、その名前空間コンパイルして 1 つDLL作成できます。この DLL は、1 つアプリケーションまたは 1 つ構成内のすべてのアプリケーション参照したり、配布したできます。これにより、多くアプリケーションで同じユーザー コントロール参照できるため、ユーザー コントロール格納する要素レイアウトコーディング必要な時間を短縮できますユーザー コントロール使用すると、たとえば、すべてのアドレス情報入力ブロック外観動作同じにするなど、アプリケーション内またアプリケーション間で一貫性維持することもできます一貫性維持することで、アプリケーションはより洗練され、その外観本格的なものになります

Windows フォームUserControl派生クラスは、フォーム内、別の UserControl 上、Internet Explorer 内の Web ページ上、またはフォーム上にホストされている WebBrowser コントロール内にホストできます

Smartphone アプリケーションでこのコントロール使用するには、Windows Mobile Version 5.0 software for Smartphones を使用する必要があります

使用例使用例

ユーザー情報取得するために、複数アプリケーション再利用できる UserControl作成するコード例次に示します。この例では、いくつかの Label コントロールTextBox コントロール、および ErrorProvider を UserControl追加してユーザー情報収集しますまた、ユーザー電子メール アドレスTextBox の Validating イベント検証しデータ検証失敗した場合は、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) メンバはすべて、スレッド セーフです。インスタンス メンバ場合は、スレッド セーフであるとは限りません。
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照



英和和英テキスト翻訳>> Weblio翻訳
英語⇒日本語日本語⇒英語
  

辞書ショートカット

すべての辞書の索引

「UserControl クラス」の関連用語

UserControl クラスのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



UserControl クラスのページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2024 Microsoft.All rights reserved.

©2024 GRAS Group, Inc.RSS