次の方法で共有


方法 : コンポーネント コンテナを拡張する

更新 : 2007 年 11 月

コンポーネント コンテナは、完全に拡張可能です。Container クラスの継承、プロパティやメソッドの追加、規則を強化するためのカスタム機能の追加、基本メソッドのオーバーライド、コンテナに含めるその他のカスタム機能の追加などができます。コンテナおよびコンテナの拡張の詳細については、「コンテナ、Site、およびコンポーネント」を参照してください。

Container は、他の基本クラスの場合と同じように拡張できます。基本クラスのプロパティを継承するクラスを作成し、拡張対象の基本メソッドをオーバーライドし、必要なプロパティやメソッドを追加します。新しいクラスは標準の Container と同じように使用できます。また、エンコードした新しい機能をすべて使用できます。

Container 基本クラスを拡張するには

  1. Container クラスを継承する新しいクラスを宣言します。

    Public Class myContainer
       Inherits System.ComponentModel.Container
    End Class
    
    class myContainer: System.ComponentModel.Container
    {
    }
    
    class myContainer
       extends System.ComponentModel.Container
    {
    }
    
  2. 基本クラスのメソッドをオーバーライドして新しい機能を追加します。Add メソッドがどのようにオーバーライドされるかを次の例に示します。

    93e0zx0a.alert_note(ja-jp,VS.90).gifメモ :

    実際には、Container には Add のオーバーロードが 2 つあります。この例では、それぞれに対してオーバーライドを用意できます。

    ' Because Add is overloaded, this line includes the Overloads keyword.
    Public Overloads Overrides Sub Add(ByVal component As _
       System.ComponentModel.IComponent)
       ' Determines if the component can be added to the container.
       If TypeOf component Is Widget Then
          ' Calls the base Add function to add the component.
          MyBase.Add(component)
       Else
          ' Throws an exception.
          Throw New NonWidgetException()
       End If
    End Sub
    
    public override void Add(System.ComponentModel.IComponent component)
    {
       if (component is Widget)
          base.Add(component);
       else 
       {
          throw(new NonWidgetException());
       }
    }
    
    public void Add(System.ComponentModel.IComponent component)
       throws NonWidgetException
    {
       if ( component instanceof Widget  ) {
          super.Add(component);
       }
       else
       {
          throw new NonWidgetException() ;
       }
    }
    
  3. 新しいコンテナに含める新しいプロパティまたはメソッドを追加します。詳細については、「クラス、プロパティ、フィールド、およびメソッド」を参照してください。

参照

処理手順

方法 : コンポーネント コンテナを作成する

概念

コンテナ、Site、およびコンポーネント

コンテナとコンポーネントの間のやり取り

参照

Container