次の方法で共有


IReferenceService インターフェイス

名前や型でプロジェクト内のオブジェクトへの参照を取得したり、指定したオブジェクトの名前を取得したり、デザイナ プロジェクト内で指定したオブジェクトの親を見つけたりするためのインターフェイスを提供します。

名前空間: System.ComponentModel.Design
アセンブリ: System (system.dll 内)

構文

'宣言
Public Interface IReferenceService
'使用
Dim instance As IReferenceService
public interface IReferenceService
public interface class IReferenceService
public interface IReferenceService
public interface IReferenceService

解説

IReferenceService インターフェイスには、次のメソッドを提供します。

  • GetReference メソッドは、指定した名前のコンポーネントを返します。指定した名前のコンポーネントが見つからなかった場合は、null 参照 (Visual Basic では Nothing) を返します。

  • GetName メソッドは、指定したコンポーネントに関連付けられている名前を返します。

  • GetComponent メソッドは、指定したコンポーネントの親コンテナを返します。

  • GetReferences メソッドは、すべてのプロジェクト コンポーネント、または任意で指定された型のすべてのプロジェクト コンポーネントへの参照の配列を返します。

使用例

IReferenceService インターフェイスの GetReferences メソッドを使用して、現在選択されているコンポーネントの型を持つ、現在のデザイン モード プロジェクト内のコンポーネントの一覧を取得するコントロールの例を次に示します。

Imports System
Imports System.Collections
Imports System.ComponentModel
Imports System.ComponentModel.Design
Imports System.Drawing
Imports System.Data
Imports System.Windows.Forms

Namespace IReferenceServiceExample

    ' This control displays the name and type of the primary selection 
    ' component in design mode, if there is one,
    ' and uses the IReferenceService interface to display the names of 
    ' any components of the type of the primary selected component.    
    ' This control uses the IComponentChangeService to monitor for 
    ' selection changed events.    
    Public Class IReferenceServiceControl
        Inherits System.Windows.Forms.UserControl
        ' Indicates the name of the type of the selected component, or "None selected."
        Private selected_typename As String
        ' Indicates the name of the base type of the selected component, or "None selected."
        Private selected_basetypename As String
        ' Indicates the name of the selected component.
        Private selected_componentname As String
        ' Contains the names of components of the type of the selected 
        ' component in design mode.
        Private typeComponents() As String
        ' Contains the names of components of the base type of the selected component in design mode.
        Private basetypeComponents() As String
        ' Reference to the IComponentChangeService for the current component.
        Private selectionService As ISelectionService

        Public Sub New()
            ' Initializes the control properties.
            Me.BackColor = Color.White
            Me.SetStyle(ControlStyles.ResizeRedraw, True)
            Me.Name = "IReferenceServiceControl"
            Me.Size = New System.Drawing.Size(500, 250)
            ' Initializes the data properties.
            typeComponents = New String(0) {}
            basetypeComponents = New String(0) {}
            selected_typename = "None selected."
            selected_basetypename = "None selected."
            selected_componentname = "None selected."
            selectionService = Nothing
        End Sub

        ' Registers and unregisters design-mode services when 
        ' the component is sited and unsited.
        Public Overrides Property Site() As System.ComponentModel.ISite
            Get
                ' Returns the site for the control.
                Return MyBase.Site
            End Get
            Set(ByVal Value As System.ComponentModel.ISite)
                ' The site is set to null when a component is cut or 
                ' removed from a design-mode site.

                ' If an event handler has already been linked with 
                ' an ISelectionService, remove the handler.
                If Not (selectionService Is Nothing) Then
                    RemoveHandler selectionService.SelectionChanged, AddressOf Me.OnSelectionChanged
                End If

                ' Sites the control.
                MyBase.Site = Value

                ' Obtains an ISelectionService interface to register 
                ' the selection changed event handler with.
                selectionService = CType(Me.GetService(GetType(ISelectionService)), ISelectionService)
                If Not (selectionService Is Nothing) Then
                    AddHandler selectionService.SelectionChanged, AddressOf Me.OnSelectionChanged
                    ' Updates the display for the current selection, if any.
                    DisplayComponentsOfSelectedComponentType()
                End If
            End Set
        End Property

        ' Updates the display according to the primary selected component, 
        ' if any, and the names of design-mode components that the 
        ' IReferenceService returns references for when queried for 
        ' references to components of the primary selected component's 
        ' type and base type.
        Private Sub DisplayComponentsOfSelectedComponentType()
            ' If a component is selected...
            If Not (selectionService.PrimarySelection Is Nothing) Then
                ' Sets the selected type name and selected component name 
                ' to the type and name of the primary selected component.
                selected_typename = selectionService.PrimarySelection.GetType().FullName
                selected_basetypename = selectionService.PrimarySelection.GetType().BaseType.FullName
                selected_componentname = CType(selectionService.PrimarySelection, IComponent).Site.Name

                ' Obtain an IReferenceService and obtain references to 
                ' each component in the design-mode project.
                ' of the selected component's type and base type.
                Dim rs As IReferenceService = CType(Me.GetService(GetType(IReferenceService)), IReferenceService)
                If Not (rs Is Nothing) Then
                    ' Get references to design-mode components of the 
                    ' primary selected component's type.
                    Dim comps As Object() = CType(rs.GetReferences(selectionService.PrimarySelection.GetType()), Object())
                    typeComponents = New String(comps.Length) {}
                    Dim i As Integer
                    For i = 0 To comps.Length - 1
                        typeComponents(i) = CType(comps(i), IComponent).Site.Name
                    Next i
                    ' Get references to design-mode components with a base type 
                    ' of the primary selected component's base type.
                    comps = CType(rs.GetReferences(selectionService.PrimarySelection.GetType().BaseType), Object())
                    basetypeComponents = New String(comps.Length) {}               
                    For i = 0 To comps.Length - 1
                        basetypeComponents(i) = CType(comps(i), IComponent).Site.Name
                    Next i
                End If
            Else
                selected_typename = "None selected."
                selected_basetypename = "None selected."
                selected_componentname = "None selected."
                typeComponents = New String(0) {}
                basetypeComponents = New String(0) {}
            End If
            Me.Refresh()
        End Sub 'DisplayComponentsOfSelectedComponentType

        Private Sub OnSelectionChanged(ByVal sender As Object, ByVal e As EventArgs)
            DisplayComponentsOfSelectedComponentType()
        End Sub 'OnSelectionChanged

        Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
            e.Graphics.DrawString("IReferenceService Example Control", New Font(FontFamily.GenericMonospace, 9), New SolidBrush(Color.Blue), 5, 5)

            e.Graphics.DrawString("Primary Selected Component from IComponentChangeService:", New Font(FontFamily.GenericMonospace, 8), New SolidBrush(Color.Red), 5, 20)
            e.Graphics.DrawString("Name:      " + selected_componentname, New Font(FontFamily.GenericMonospace, 8), New SolidBrush(Color.Black), 10, 32)
            e.Graphics.DrawString("Type:      " + selected_typename, New Font(FontFamily.GenericMonospace, 8), New SolidBrush(Color.Black), 10, 44)
            e.Graphics.DrawString("Base Type: " + selected_basetypename, New Font(FontFamily.GenericMonospace, 8), New SolidBrush(Color.Black), 10, 56)
            e.Graphics.DrawLine(New Pen(New SolidBrush(Color.Black), 1), 5, 77, Me.Width - 10, 77)

            e.Graphics.DrawString("Components of Type from IReferenceService:", New Font(FontFamily.GenericMonospace, 8), New SolidBrush(Color.Red), 5, 85)
            If selected_typename <> "None selected." Then
                Dim i As Integer
                For i = 0 To typeComponents.Length - 1
                    e.Graphics.DrawString(typeComponents(i), New Font(FontFamily.GenericMonospace, 8), New SolidBrush(Color.Black), 20, 97 + i * 12)
                Next i
            End If
            e.Graphics.DrawString("Components of Base Type from IReferenceService:", New Font(FontFamily.GenericMonospace, 8), New SolidBrush(Color.Red), 5, 109 + typeComponents.Length * 12)
            If selected_typename <> "None selected." Then
                Dim i As Integer
                For i = 0 To basetypeComponents.Length - 1
                    e.Graphics.DrawString(basetypeComponents(i), New Font(FontFamily.GenericMonospace, 8), New SolidBrush(Color.Black), 20, 121 + typeComponents.Length * 12 + i * 12)
                Next i
            End If
        End Sub 'OnPaint

    End Class 'IReferenceServiceControl
End Namespace 'IReferenceServiceExample
using System;
using System.Collections;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Drawing;
using System.Data;
using System.Windows.Forms;

namespace IReferenceServiceExample
{
    // This control displays the name and type of the primary selection 
    // component in design mode, if there is one,
    // and uses the IReferenceService interface to display the names of 
    // any components of the type of the primary selected component.    
    // This control uses the IComponentChangeService to monitor for 
    // selection changed events.
    public class IReferenceServiceControl : System.Windows.Forms.UserControl
    {
            // Indicates the name of the type of the selected component, or "None selected.".
            private string selected_typename;
            // Indicates the name of the base type of the selected component, or "None selected."
            private string selected_basetypename;
            // Indicates the name of the selected component.
            private string selected_componentname;
            // Contains the names of components of the type of the selected 
            // component in design mode.
            private string[] typeComponents;
            // Contains the names of components of the base type of the selected component in design mode.
            private string[] basetypeComponents;
            // Reference to the IComponentChangeService for the current component.
            private ISelectionService selectionService;
        
            public IReferenceServiceControl()
            {
                // Initializes the control properties.
                this.BackColor = Color.White;
                this.SetStyle(ControlStyles.ResizeRedraw, true);
                this.Name = "IReferenceServiceControl";
                this.Size = new System.Drawing.Size(500, 250);                                  
                // Initializes the data properties.
                typeComponents = new string[0];
                basetypeComponents = new string[0];
                selected_typename = "None selected.";
                selected_basetypename = "None selected.";
                selected_componentname = "None selected.";
                selectionService = null;
            }

            // Registers and unregisters design-mode services when 
            // the component is sited and unsited.
            public override System.ComponentModel.ISite Site
            {
                get
                {
                    // Returns the site for the control.
                    return base.Site;
                }
                set
                {
                    // The site is set to null when a component is cut or 
                    // removed from a design-mode site.

                    // If an event handler has already been linked with 
                    // an ISelectionService, remove the handler.
                    if(selectionService != null)
                        selectionService.SelectionChanged -= new EventHandler(this.OnSelectionChanged);

                    // Sites the control.
                    base.Site = value;
                
                    // Obtains an ISelectionService interface to register 
                    // the selection changed event handler with.
                    selectionService = (ISelectionService)this.GetService(typeof(ISelectionService));
                    if( selectionService!= null )
                    {
                        selectionService.SelectionChanged += new EventHandler(this.OnSelectionChanged);
                        // Updates the display for the current selection, if any.
                        DisplayComponentsOfSelectedComponentType();
                    }
                }
            }

            // Updates the display according to the primary selected component, 
            // if any, and the names of design-mode components that the 
            // IReferenceService returns references for when queried for 
            // references to components of the primary selected component's 
            // type and base type.
            private void DisplayComponentsOfSelectedComponentType()
            {
                // If a component is selected...
                if( selectionService.PrimarySelection != null )
                {              
                    // Sets the selected type name and selected component name to the type and name of the primary selected component.
                    selected_typename = selectionService.PrimarySelection.GetType().FullName;
                    selected_basetypename = selectionService.PrimarySelection.GetType().BaseType.FullName;
                    selected_componentname = ((IComponent)selectionService.PrimarySelection).Site.Name;                

                    // Obtain an IReferenceService and obtain references to 
                    // each component in the design-mode project
                    // of the selected component's type and base type.
                    IReferenceService rs = (IReferenceService)this.GetService(typeof(IReferenceService));
                    if( rs != null )
                    {
                        // Get references to design-mode components of the 
                        // primary selected component's type.
                        object[] comps = (object[])rs.GetReferences( selectionService.PrimarySelection.GetType() );
                        typeComponents = new string[comps.Length];
                        for(int i=0; i<comps.Length; i++)
                            typeComponents[i] = ((IComponent)comps[i]).Site.Name;
   
                        // Get references to design-mode components with a base type 
                        // of the primary selected component's base type.
                        comps = (object[])rs.GetReferences( selectionService.PrimarySelection.GetType().BaseType );
                        basetypeComponents = new string[comps.Length];
                        for(int i=0; i<comps.Length; i++)
                            basetypeComponents[i] = ((IComponent)comps[i]).Site.Name;
                    }
                }
                else
                {
                    selected_typename = "None selected.";
                    selected_basetypename = "None selected.";
                    selected_componentname = "None selected.";
                    typeComponents = new string[0];
                    basetypeComponents = new string[0];
                }
                this.Refresh();
            }

            private void OnSelectionChanged(object sender, EventArgs e)
            {
                DisplayComponentsOfSelectedComponentType();
            }

            protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
            {
                e.Graphics.DrawString("IReferenceService Example Control", new Font(FontFamily.GenericMonospace, 9), new SolidBrush(Color.Blue), 5, 5);

                e.Graphics.DrawString("Primary Selected Component from IComponentChangeService:", new Font(FontFamily.GenericMonospace, 8), new SolidBrush(Color.Red), 5, 20);
                e.Graphics.DrawString("Name:      "+selected_componentname, new Font(FontFamily.GenericMonospace, 8), new SolidBrush(Color.Black), 10, 32);
                e.Graphics.DrawString("Type:      "+selected_typename, new Font(FontFamily.GenericMonospace, 8), new SolidBrush(Color.Black), 10, 44);
                e.Graphics.DrawString("Base Type: "+selected_basetypename, new Font(FontFamily.GenericMonospace, 8), new SolidBrush(Color.Black), 10, 56);
                e.Graphics.DrawLine(new Pen(new SolidBrush(Color.Black), 1), 5, 77, this.Width-5, 77);
            
                e.Graphics.DrawString("Components of Type from IReferenceService:", new Font(FontFamily.GenericMonospace, 8), new SolidBrush(Color.Red), 5, 85);                        
                if( selected_typename != "None selected." )
                    for(int i=0; i<typeComponents.Length; i++)
                        e.Graphics.DrawString(typeComponents[i], new Font(FontFamily.GenericMonospace, 8), new SolidBrush(Color.Black), 20, 97+(i*12));
            
                e.Graphics.DrawString("Components of Base Type from IReferenceService:", new Font(FontFamily.GenericMonospace, 8), new SolidBrush(Color.Red), 5, 109+(typeComponents.Length*12));
                if( selected_typename != "None selected." )
                    for(int i=0; i<basetypeComponents.Length; i++)
                        e.Graphics.DrawString(basetypeComponents[i], new Font(FontFamily.GenericMonospace, 8), new SolidBrush(Color.Black), 20, 121+(typeComponents.Length*12)+(i*12));
            }
    }
}
#using <System.Windows.Forms.dll>
#using <System.Data.dll>
#using <System.Drawing.dll>
#using <System.dll>

using namespace System;
using namespace System::Collections;
using namespace System::ComponentModel;
using namespace System::ComponentModel::Design;
using namespace System::Drawing;
using namespace System::Data;
using namespace System::Windows::Forms;

// This control displays the name and type of the primary selection 
// component in design mode, if there is one,
// and uses the IReferenceService interface to display the names of 
// any components of the type of the primary selected component.    
// This control uses the IComponentChangeService to monitor for 
// selection changed events.
public ref class IReferenceServiceControl: public System::Windows::Forms::UserControl
{
private:

   // Indicates the name of the type of the selected component, or "None selected.".
   String^ selected_typename;

   // Indicates the name of the base type of the selected component, or "None selected."
   String^ selected_basetypename;

   // Indicates the name of the selected component.
   String^ selected_componentname;

   // Contains the names of components of the type of the selected 
   // component in design mode.
   array<String^>^typeComponents;

   // Contains the names of components of the base type of the selected component in design mode.
   array<String^>^basetypeComponents;

   // Reference to the IComponentChangeService for the current component.
   ISelectionService^ selectionService;

public:
   IReferenceServiceControl()
   {
      // Initializes the control properties.
      this->BackColor = Color::White;
      this->SetStyle( ControlStyles::ResizeRedraw, true );
      this->Name = "IReferenceServiceControl";
      this->Size = System::Drawing::Size( 500, 250 );

      // Initializes the data properties.
      typeComponents = gcnew array<String^>(0);
      basetypeComponents = gcnew array<String^>(0);
      selected_typename = "None selected.";
      selected_basetypename = "None selected.";
      selected_componentname = "None selected.";
      selectionService = nullptr;
   }


   property System::ComponentModel::ISite^ Site 
   {
      // Registers and unregisters design-mode services when 
      // the component is sited and unsited.
      virtual System::ComponentModel::ISite^ get() override
      {
         // Returns the site for the control.
         return __super::Site;
      }

      virtual void set( System::ComponentModel::ISite^ value ) override
      {
         // The site is set to null when a component is cut or 
         // removed from a design-mode site.
         // If an event handler has already been linked with 
         // an ISelectionService, remove the handler.
         if ( selectionService != nullptr )
                  selectionService->SelectionChanged -= gcnew EventHandler( this, &IReferenceServiceControl::OnSelectionChanged );

         // Sites the control.
         __super::Site = value;

         // Obtains an ISelectionService interface to register 
         // the selection changed event handler with.
         selectionService = dynamic_cast<ISelectionService^>(this->GetService( ISelectionService::typeid ));
         if ( selectionService != nullptr )
         {
            selectionService->SelectionChanged += gcnew EventHandler( this, &IReferenceServiceControl::OnSelectionChanged );

            // Updates the display for the current selection, if any.
            DisplayComponentsOfSelectedComponentType();
         }
      }
   }

private:

   // Updates the display according to the primary selected component, 
   // if any, and the names of design-mode components that the 
   // IReferenceService returns references for when queried for 
   // references to components of the primary selected component's 
   // type and base type.
   void DisplayComponentsOfSelectedComponentType()
   {
      // If a component is selected...
      if ( selectionService->PrimarySelection != nullptr )
      {
         // Sets the selected type name and selected component name to the type and name of the primary selected component.
         selected_typename = selectionService->PrimarySelection->GetType()->FullName;
         selected_basetypename = selectionService->PrimarySelection->GetType()->BaseType->FullName;
         selected_componentname = (dynamic_cast<IComponent^>(selectionService->PrimarySelection))->Site->Name;

         // Obtain an IReferenceService and obtain references to 
         // each component in the design-mode project
         // of the selected component's type and base type.
         IReferenceService^ rs = dynamic_cast<IReferenceService^>(this->GetService( IReferenceService::typeid ));
         if ( rs != nullptr )
         {
            // Get references to design-mode components of the 
            // primary selected component's type.
            array<Object^>^comps = (array<Object^>^)rs->GetReferences( selectionService->PrimarySelection->GetType() );
            typeComponents = gcnew array<String^>(comps->Length);
            for ( int i = 0; i < comps->Length; i++ )
               typeComponents[ i ] = (dynamic_cast<IComponent^>(comps[ i ]))->Site->Name;

            // Get references to design-mode components with a base type 
            // of the primary selected component's base type.
            comps = (array<Object^>^)rs->GetReferences( selectionService->PrimarySelection->GetType()->BaseType );
            basetypeComponents = gcnew array<String^>(comps->Length);
            for ( int i = 0; i < comps->Length; i++ )
               basetypeComponents[ i ] = (dynamic_cast<IComponent^>(comps[ i ]))->Site->Name;
         }
      }
      else
      {
         selected_typename = "None selected.";
         selected_basetypename = "None selected.";
         selected_componentname = "None selected.";
         typeComponents = gcnew array<String^>(0);
         basetypeComponents = gcnew array<String^>(0);
      }

      this->Refresh();
   }

   void OnSelectionChanged( Object^ /*sender*/, EventArgs^ /*e*/ )
   {
      DisplayComponentsOfSelectedComponentType();
   }


protected:
   virtual void OnPaint( System::Windows::Forms::PaintEventArgs^ e ) override
   {
      e->Graphics->DrawString( "IReferenceService Example Control", gcnew System::Drawing::Font( FontFamily::GenericMonospace,9 ), gcnew SolidBrush( Color::Blue ), 5, 5 );
      e->Graphics->DrawString( "Primary Selected Component from IComponentChangeService:", gcnew System::Drawing::Font( FontFamily::GenericMonospace,8 ), gcnew SolidBrush( Color::Red ), 5, 20 );
      e->Graphics->DrawString( String::Format( "Name:      {0}", selected_componentname ), gcnew System::Drawing::Font( FontFamily::GenericMonospace,8 ), gcnew SolidBrush( Color::Black ), 10, 32 );
      e->Graphics->DrawString( String::Format( "Type:      {0}", selected_typename ), gcnew System::Drawing::Font( FontFamily::GenericMonospace,8 ), gcnew SolidBrush( Color::Black ), 10, 44 );
      e->Graphics->DrawString( String::Format( "Base Type: {0}", selected_basetypename ), gcnew System::Drawing::Font( FontFamily::GenericMonospace,8 ), gcnew SolidBrush( Color::Black ), 10, 56 );
      e->Graphics->DrawLine( gcnew Pen( gcnew SolidBrush( Color::Black ),1 ), 5, 77, this->Width - 5, 77 );
      e->Graphics->DrawString( "Components of Type from IReferenceService:", gcnew System::Drawing::Font( FontFamily::GenericMonospace,8 ), gcnew SolidBrush( Color::Red ), 5, 85 );
      if (  !selected_typename->Equals( "None selected." ) )
            for ( int i = 0; i < typeComponents->Length; i++ )
         e->Graphics->DrawString( typeComponents[ i ], gcnew System::Drawing::Font( FontFamily::GenericMonospace,8 ), gcnew SolidBrush( Color::Black ), 20.f, 97.f + (i * 12) );

      e->Graphics->DrawString( "Components of Base Type from IReferenceService:", gcnew System::Drawing::Font( FontFamily::GenericMonospace,8 ), gcnew SolidBrush( Color::Red ), 5.f, 109.f + (typeComponents->Length * 12) );
      if (  !selected_typename->Equals( "None selected." ) )
            for ( int i = 0; i < basetypeComponents->Length; i++ )
         e->Graphics->DrawString( basetypeComponents[ i ], gcnew System::Drawing::Font( FontFamily::GenericMonospace,8 ), gcnew SolidBrush( Color::Black ), 20.f, 121.f + (typeComponents->Length * 12) + (i * 12) );
   }
};
package IReferenceServiceExample; 

import System.*;
import System.Collections.*;
import System.ComponentModel.*;
import System.ComponentModel.Design.*;
import System.Drawing.*;
import System.Data.*;
import System.Windows.Forms.*;

// This control displays the name and type of the primary selection 
// component in design mode, if there is one,
// and uses the IReferenceService interface to display the names of 
// any components of the type of the primary selected component.    
// This control uses the IComponentChangeService to monitor for 
// selection changed events.
public class IReferenceServiceControl extends System.Windows.Forms.UserControl
{
    // Indicates the name of the type of the selected 
    //component, or "None selected.".
    private String selected_TypeName;

    // Indicates the name of the base type of the selected 
    //component, or "None selected."
    private String selected_BaseTypeName;

    // Indicates the name of the selected component.
    private String selected_ComponentName;

    // Contains the names of components of the type of the selected 
    // component in design mode.
    private String typeComponents[];

    // Contains the names of components of the base type of the 
    //selected component in design mode.
    private String basetypeComponents[];

    // Reference to the IComponentChangeService for the current component.
    private ISelectionService selectionService;

    public IReferenceServiceControl()
    {
        // Initializes the control properties.
        this.set_BackColor(Color.get_White());
        this.SetStyle(ControlStyles.ResizeRedraw, true);
        this.set_Name("IReferenceServiceControl");
        this.set_Size(new System.Drawing.Size(500, 250));

        // Initializes the data properties.
        typeComponents = new String[0];
        basetypeComponents = new String[0];
        selected_TypeName = "None selected.";
        selected_BaseTypeName = "None selected.";
        selected_ComponentName = "None selected.";
        selectionService = null;
    } //IReferenceServiceControl

    // Registers and unregisters design-mode services when 
    // the component is sited and unsited.
    /** @property 
     */
    public System.ComponentModel.ISite get_Site()
    {
        // Returns the site for the control.
        return super.get_Site();
    } //get_Site

    /** @property 
     */
    public void set_Site(System.ComponentModel.ISite value)
    {
        // The site is set to null when a component is cut or 
        // removed from a design-mode site.
        // If an event handler has already been linked with 
        // an ISelectionService, remove the handler.
        if (selectionService != null) {
            selectionService.remove_SelectionChanged(
                new EventHandler(this.OnSelectionChanged));
        }
        // Sites the control.
        super.set_Site(value);

        // Obtains an ISelectionService interface to register 
        // the selection changed event handler with.
        selectionService = ((ISelectionService)(
            this.GetService(ISelectionService.class.ToType())));
        if (selectionService != null) {
            selectionService.add_SelectionChanged(
                new EventHandler(this.OnSelectionChanged));

            // Updates the display for the current selection, if any.
            DisplayComponentsOfSelectedComponentType();
        }
    } //set_Site

    // Updates the display according to the primary selected component, 
    // if any, and the names of design-mode components that the 
    // IReferenceService returns references for when queried for 
    // references to components of the primary selected component's 
    // type and base type.
    private void DisplayComponentsOfSelectedComponentType()
    {
        // If a component is selected...
        if (selectionService.get_PrimarySelection() != null) {
            // Sets the selected type name and selected component name
            //to the type and name of the primary selected component.
            selected_TypeName = selectionService.get_PrimarySelection().
                GetType().get_FullName();
            selected_BaseTypeName = selectionService.get_PrimarySelection().
                GetType().get_BaseType().get_FullName();
            selected_ComponentName = ((IComponent)(selectionService.
                get_PrimarySelection())).get_Site().get_Name();

            // Obtain an IReferenceService and obtain references to 
            // each component in the design-mode project
            // of the selected component's type and base type.
            IReferenceService rs = ((IReferenceService)(
                this.GetService(IReferenceService.class.ToType())));
            if (rs != null) {
                // Get references to design-mode components of the 
                // primary selected component's type.
                Object[] comps = ((Object[])(rs.GetReferences(
                    selectionService.get_PrimarySelection().GetType())));

                typeComponents = new String[comps.length];
                for (int i = 0; i < comps.length; i++) {
                    typeComponents.set_Item(i,(
                        (IComponent)(comps.get_Item(i))).get_Site().get_Name());
                }
                // Get references to design-mode components with a base type 
                // of the primary selected component's base type.
                comps = ((Object[])(rs.GetReferences(selectionService.
                    get_PrimarySelection().GetType().get_BaseType())));
                basetypeComponents = new String[comps.length];
                for (int i = 0; i < comps.length; i++) {
                    basetypeComponents.set_Item(i, (
                        (IComponent)(comps.get_Item(i))).get_Site().get_Name());
                }
            }
        }
        else {
            selected_TypeName = "None selected.";
            selected_BaseTypeName = "None selected.";
            selected_ComponentName = "None selected.";
            typeComponents = new String[0];
            basetypeComponents = new String[0];
        }
        this.Refresh();
    } //DisplayComponentsOfSelectedComponentType

    private void OnSelectionChanged(Object sender, EventArgs e)
    {
        DisplayComponentsOfSelectedComponentType();
    } //OnSelectionChanged

    protected void OnPaint(System.Windows.Forms.PaintEventArgs e)
    {
        e.get_Graphics().DrawString("IReferenceService Example Control",
            new Font(FontFamily.get_GenericMonospace(), 9),
            new SolidBrush(Color.get_Blue()), 5, 5);
        e.get_Graphics().DrawString(
            "Primary Selected Component from IComponentChangeService:", 
            new Font(FontFamily.get_GenericMonospace(), 8), 
            new SolidBrush(Color.get_Red()), 5, 20);
        e.get_Graphics().DrawString("Name:      " + selected_ComponentName,
            new Font(FontFamily.get_GenericMonospace(), 8),
            new SolidBrush(Color.get_Black()), 10, 32);
        e.get_Graphics().DrawString("Type:      " + selected_TypeName,
            new Font(FontFamily.get_GenericMonospace(), 8), 
            new SolidBrush(Color.get_Black()), 10, 44);
        e.get_Graphics().DrawString("Base Type: " + selected_BaseTypeName, 
            new Font(FontFamily.get_GenericMonospace(), 8), 
            new SolidBrush(Color.get_Black()), 10, 56);
        e.get_Graphics().DrawLine(new Pen(new SolidBrush(Color.get_Black()), 1)
            , 5, 77, this.get_Width() - 5, 77);
        e.get_Graphics().DrawString(
            "Components of Type from IReferenceService:", 
            new Font(FontFamily.get_GenericMonospace(), 8), 
            new SolidBrush(Color.get_Red()), 5, 85);
        if (selected_TypeName != "None selected.") {
            for (int i = 0; i < typeComponents.length; i++) {
                e.get_Graphics().DrawString(System.Convert.ToString(
                    typeComponents.get_Item(i)), 
                    new Font(FontFamily.get_GenericMonospace(), 8), 
                    new SolidBrush(Color.get_Black()), 20, 97 + i * 12);
            }
        }
        e.get_Graphics().DrawString(
            "Components of Base Type from IReferenceService:", 
            new Font(FontFamily.get_GenericMonospace(), 8), 
            new SolidBrush(Color.get_Red()), 5,
            109 + typeComponents.length * 12);
        if (selected_TypeName != "None selected.") {
            for (int i = 0; i < basetypeComponents.length; i++)    {
                e.get_Graphics().DrawString(System.Convert.ToString(
                    basetypeComponents.get_Item(i)), 
                    new Font(FontFamily.get_GenericMonospace(), 8), 
                    new SolidBrush(Color.get_Black()), 20, 
                    121 + typeComponents.length * 12 + i * 12);
            }
        }
    } //OnPaint
} //IReferenceServiceControl

プラットフォーム

Windows 98, Windows 2000 SP4, Windows Millennium Edition, 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

参照

関連項目

IReferenceService メンバ
System.ComponentModel.Design 名前空間