Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
ServiceContainer Class
Collapse the table of content
Expand the table of content

ServiceContainer Class

Provides a simple implementation of the IServiceContainer interface. This class cannot be inherited.

Namespace: System.ComponentModel.Design
Assembly: System (in system.dll)

public ref class ServiceContainer : IServiceContainer, IServiceProvider, IDisposable
public class ServiceContainer implements IServiceContainer, IServiceProvider, 
	IDisposable
public class ServiceContainer implements IServiceContainer, IServiceProvider, 
	IDisposable
Not applicable.

The ServiceContainer object can be used to store and provide services. ServiceContainer implements the IServiceContainer interface.

The ServiceContainer object can be created using a constructor that adds a parent IServiceContainer through which services can be optionally added to or removed from all parent IServiceContainer objects, including the immediate parent IServiceContainer. To add or remove a service from all IServiceContainer implementations that are linked to this IServiceContainer through parenting, call the AddService or RemoveService method overload that accepts a Boolean value indicating whether to promote the service request.

NoteNote:

The HostProtectionAttribute attribute applied to this class has the following Resources property value: SharedState. The HostProtectionAttribute does not affect desktop applications (which are typically started by double-clicking an icon, typing a command, or entering a URL in a browser). For more information, see the HostProtectionAttribute class or SQL Server Programming and Host Protection Attributes.

The following example program demonstrates service chaining and the resultant service availability of a chain of linked ServiceContainer objects. The program provides a user interface that allows you to see the availability of services within a chain of linked services, and uses the AddService, GetService, and RemoveService methods as well as linked service containers.

#using <System.Windows.Forms.dll>
#using <System.dll>
#using <System.Drawing.dll>

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

ref class ServiceObjectControl;

// Example form provides UI for demonstrating service sharing behavior
// of a network of IServiceContainer/IServiceProvider controls.
public ref class ServiceForm: public System::Windows::Forms::Form
{
private:

   // Root service container control for tree.
   ServiceObjectControl^ root;

   // Button for clearing any provided services and resetting tree states.
   System::Windows::Forms::Button^ reset_button;

   // Color list used to color code controls.
   array<Color>^colorkeys;

   // Strings used to reflect text service.
   array<String^>^keystrings;

public:
   ServiceForm()
   {
      InitializeComponent();
      CreateServiceControlTree();
      colorkeys = gcnew array<Color>(6);
      colorkeys[ 0 ] = Color::Beige;
      colorkeys[ 1 ] = Color::SeaShell;
      colorkeys[ 2 ] = Color::LightGreen;
      colorkeys[ 3 ] = Color::LightBlue;
      colorkeys[ 4 ] = Color::Khaki;
      colorkeys[ 5 ] = Color::CadetBlue;
      array<String^>^stringstemp = {"No service use","Service not accessible","Service provided",
            "Service obtained","Service accessible","No further access"};
      keystrings = stringstemp;
   }

private:
   void CreateServiceControlTree();
   void InitializeComponent();

internal:
   void ResetServiceTree( Object^ sender, EventArgs^ e );
public:
   void UpdateServiceCoverage();

protected:
   virtual void OnPaint( System::Windows::Forms::PaintEventArgs^ e ) override;
};

// Example service type contains a text string, sufficient
// to demonstrate service sharing.
public ref class TextService
{
public:
   String^ text;
   TextService()
      : text( String::Empty )
   {}

   TextService( String^ text )
   {
      this->text = text;
   }
};

public enum class TextServiceState
{
   ServiceNotObtained, ServiceObtained, ServiceProvided, ServiceNotFound
};

// Example Form for entering a string.
private ref class StringInputDialog: public System::Windows::Forms::Form
{
private:
   System::Windows::Forms::Button^ ok_button;
   System::Windows::Forms::Button^ cancel_button;

public:
   System::Windows::Forms::TextBox^ inputTextBox;
   StringInputDialog( String^ text )
   {
      InitializeComponent();
      inputTextBox->Text = text;
   }

private:
   void InitializeComponent()
   {
      this->ok_button = gcnew System::Windows::Forms::Button;
      this->cancel_button = gcnew System::Windows::Forms::Button;
      this->inputTextBox = gcnew System::Windows::Forms::TextBox;
      this->SuspendLayout();
      this->ok_button->Anchor = static_cast<AnchorStyles>(System::Windows::Forms::AnchorStyles::Bottom |
            System::Windows::Forms::AnchorStyles::Right);
      this->ok_button->Location = System::Drawing::Point( 180, 43 );
      this->ok_button->Name = "ok_button";
      this->ok_button->TabIndex = 1;
      this->ok_button->Text = "OK";
      this->ok_button->DialogResult = System::Windows::Forms::DialogResult::OK;
      this->cancel_button->Anchor = static_cast<AnchorStyles>(System::Windows::Forms::AnchorStyles::Bottom |
            System::Windows::Forms::AnchorStyles::Right);
      this->cancel_button->Location = System::Drawing::Point( 260, 43 );
      this->cancel_button->Name = "cancel_button";
      this->cancel_button->TabIndex = 2;
      this->cancel_button->Text = "Cancel";
      this->cancel_button->DialogResult = System::Windows::Forms::DialogResult::Cancel;
      this->inputTextBox->Location = System::Drawing::Point( 6, 9 );
      this->inputTextBox->Name = "inputTextBox";
      this->inputTextBox->Size = System::Drawing::Size( 327, 20 );
      this->inputTextBox->TabIndex = 0;
      this->inputTextBox->Text = "";
      this->inputTextBox->Anchor = static_cast<AnchorStyles>((System::Windows::Forms::AnchorStyles::Top |
            System::Windows::Forms::AnchorStyles::Left) | System::Windows::Forms::AnchorStyles::Right);
      this->ClientSize = System::Drawing::Size( 342, 73 );
      array<System::Windows::Forms::Control^>^temp0 = {this->inputTextBox,this->cancel_button,this->ok_button};
      this->Controls->AddRange( temp0 );
      this->MinimumSize = System::Drawing::Size( 350, 100 );
      this->Name = "StringInputDialog";
      this->Text = "Text Service Provide String Dialog";
      this->ResumeLayout( false );
   }
};


// An example user control that uses ServiceContainer to add, remove,
// and access services through a linkable service container network.
public ref class ServiceObjectControl: public System::Windows::Forms::UserControl
{
public:

   // This example user control implementation provides a wrapper for
   // ServiceContainer, supporting a linked service container network.
   ServiceContainer^ serviceContainer;

private:

   // Parent form reference for main program function access.
   ServiceForm^ parent;

public:

   // String for label displayed on the control to indicate the
   // control's current service-related configuration state.
   String^ label;

private:

   // The current state of the control reflecting whether it has
   // obtained or provided a text service.
   TextServiceState state_;

public:

   property TextServiceState state 
   {
      TextServiceState get()
      {
         return state_;
      }

      void set( TextServiceState value )
      {
         if ( (TextServiceState)value == TextServiceState::ServiceProvided )
                  this->BackColor = Color::LightGreen;
         else
         if ( (TextServiceState)value == TextServiceState::ServiceNotObtained )
                  this->BackColor = Color::White;
         else
         if ( (TextServiceState)value == TextServiceState::ServiceObtained )
                  this->BackColor = Color::LightBlue;
         else
         if ( (TextServiceState)value == TextServiceState::ServiceNotFound )
                  this->BackColor = Color::SeaShell;

         state_ = value;
      }
   }
   ServiceObjectControl( ServiceObjectControl^ serviceContainerParent, System::Drawing::Size size,
         Point location, ServiceForm^ parent )
   {
      this->state_ = TextServiceState::ServiceNotObtained;
      this->BackColor = Color::Beige;
      this->label = String::Empty;
      this->Size = size;
      this->Location = location;
      this->parent = parent;
      if ( serviceContainerParent == nullptr )
            serviceContainer = gcnew ServiceContainer;
      else
            serviceContainer = gcnew ServiceContainer( serviceContainerParent->serviceContainer );
   }

protected:

   // Paint method override draws the label string on the control.
   virtual void OnPaint( System::Windows::Forms::PaintEventArgs^ e ) override
   {
      e->Graphics->DrawString( label, gcnew System::Drawing::Font( "Arial",8 ), gcnew SolidBrush( Color::Black ), 5, 5 );
   }

   // Process mouse-down behavior for click.
   virtual void OnMouseDown( System::Windows::Forms::MouseEventArgs^ e ) override
   {
      if ( e->Button == ::MouseButtons::Left )
      {
         if ( state_ != TextServiceState::ServiceProvided )
         {
            // Attempt to update text from service, and set color
            // state accordingly.
            TextService^ ts = dynamic_cast<TextService^>(serviceContainer->GetService( TextService::typeid ));
            if ( ts != nullptr )
            {
               this->label = ts->text;
               state = TextServiceState::ServiceObtained;
            }
            else
            {
               this->label = "Service Not Found";
               state = TextServiceState::ServiceNotFound;
            }
         }
      }

      if ( e->Button == ::MouseButtons::Right )
      {
         if ( state_ == TextServiceState::ServiceProvided )
         {
            // Remove service if the container provided it.
            if ( serviceContainer->GetService( TextService::typeid ) != nullptr )
            {
               serviceContainer->RemoveService( TextService::typeid, true );
               state = TextServiceState::ServiceNotObtained;
               this->label = "Service Removed";
            }
         }
         else
         {
            // Obtain string and provide text service.
            StringInputDialog^ form = gcnew StringInputDialog( "Test String" );
            form->StartPosition = FormStartPosition::CenterParent;
            if ( form->ShowDialog() == DialogResult::OK )
            {
               if ( serviceContainer->GetService( TextService::typeid ) != nullptr )
                              serviceContainer->RemoveService( TextService::typeid, true );
               parent->ResetServiceTree( this, gcnew EventArgs );
               serviceContainer->AddService( TextService::typeid, gcnew TextService( form->inputTextBox->Text ), true );
               state = TextServiceState::ServiceProvided;
               this->label = String::Format( "Provided Text: {0}", form->inputTextBox->Text );
            }
         }
      }

      parent->UpdateServiceCoverage();
   }

public:

   // Method accesses the TextService to test the visibility of the service
   // from the control, and sets the UI state accordingly.
   void ReflectServiceVisibility()
   {
      if ( state_ == TextServiceState::ServiceObtained )
      {
         if ( serviceContainer->GetService( TextService::typeid ) == nullptr )
                  this->BackColor = Color::CadetBlue;
      }
      else
      if ( state_ != TextServiceState::ServiceProvided )
      {
         if ( serviceContainer->GetService( TextService::typeid ) == nullptr )
         {
            this->BackColor = Color::White;
            return;
         }
         
         // Service available.
         if ( state_ == TextServiceState::ServiceNotFound )
                  this->BackColor = Color::Khaki;
         else
         if ( state_ == TextServiceState::ServiceNotObtained &&  !label->Equals( "Service Removed" ) )
                  this->BackColor = Color::Khaki;
      }
   }
};

void ServiceForm::CreateServiceControlTree()
{
   // Create root service control.
   ServiceObjectControl^ control1 = gcnew ServiceObjectControl( nullptr,System::Drawing::Size( 300, 40 ),Point(10,80),this );
   root = control1;

   // Create first tier - pass parent with service object control 1
   ServiceObjectControl^ control2 = gcnew ServiceObjectControl( control1,System::Drawing::Size( 200, 30 ),Point(50,160),this );
   ServiceObjectControl^ control3 = gcnew ServiceObjectControl( control1,System::Drawing::Size( 200, 30 ),Point(50,240),this );

   // Create second tier A - pass parent with service object control 2
   ServiceObjectControl^ control4 = gcnew ServiceObjectControl( control2,System::Drawing::Size( 180, 20 ),Point(300,145),this );
   ServiceObjectControl^ control5 = gcnew ServiceObjectControl( control2,System::Drawing::Size( 180, 20 ),Point(300,185),this );

   // Create second tier B - pass parent with service object control 3
   ServiceObjectControl^ control6 = gcnew ServiceObjectControl( control3,System::Drawing::Size( 180, 20 ),Point(300,225),this );
   ServiceObjectControl^ control7 = gcnew ServiceObjectControl( control3,System::Drawing::Size( 180, 20 ),Point(300,265),this );

   // Add controls.
   array<Control^>^temp1 = {control1,control2,control3,control4,control5,control6,control7};
   this->Controls->AddRange( temp1 );
}

void ServiceForm::ResetServiceTree( Object^ /*sender*/, EventArgs^ /*e*/ )
{
   // Remove the service from the service tree.
   if ( root->serviceContainer->GetService( TextService::typeid ) != nullptr )
      root->serviceContainer->RemoveService( TextService::typeid, true );

   // Set all controls to "not obtained" and clear their labels.
   for ( int i = 0; i < Controls->Count; i++ )
      if (  !Controls[ i ]->Equals( reset_button ) )
      {
         (dynamic_cast<ServiceObjectControl^>(Controls[ i ]))->state = TextServiceState::ServiceNotObtained;
         (dynamic_cast<ServiceObjectControl^>(Controls[ i ]))->label = String::Empty;
         (dynamic_cast<ServiceObjectControl^>(Controls[ i ]))->BackColor = Color::Beige;
      }
}

void ServiceForm::UpdateServiceCoverage()
{
   // Have each control set state to reflect service availability.
   for ( int i = 0; i < Controls->Count; i++ )
      if (  !Controls[ i ]->Equals( reset_button ) )
            (dynamic_cast<ServiceObjectControl^>(Controls[ i ]))->ReflectServiceVisibility();
}

void ServiceForm::InitializeComponent()
{
   this->reset_button = gcnew System::Windows::Forms::Button;
   this->SuspendLayout();

   //
   // reset_button
   //
   this->reset_button->Location = System::Drawing::Point( 392, 88 );
   this->reset_button->Name = "reset_button";
   this->reset_button->TabIndex = 0;
   this->reset_button->TabStop = false;
   this->reset_button->Text = "Reset";
   this->reset_button->Click += gcnew System::EventHandler( this, &ServiceForm::ResetServiceTree );

   //
   // ServiceForm
   //
   this->ClientSize = System::Drawing::Size( 512, 373 );
   array<System::Windows::Forms::Control^>^temp2 = {this->reset_button};
   this->Controls->AddRange( temp2 );
   this->MinimumSize = System::Drawing::Size( 520, 400 );
   this->Name = "ServiceForm";
   this->Text = "Service Container Architecture Example";
   this->ResumeLayout( false );
}

void ServiceForm::OnPaint( System::Windows::Forms::PaintEventArgs^ e )
{
   e->Graphics->DrawString( "The following tree diagram represents a hierarchy of linked service containers in controls.",
         gcnew System::Drawing::Font( "Arial",9 ), gcnew SolidBrush( Color::Black ), 4, 4 );
   e->Graphics->DrawString( "This example demonstrates the propagation behavior of services through a linked service object tree.",
         gcnew System::Drawing::Font( "Arial",8 ), gcnew SolidBrush( Color::Black ), 4, 26 );
   e->Graphics->DrawString( "Right-click a component to add or replace a text service, or to remove it if the component provided it.",
         gcnew System::Drawing::Font( "Arial",8 ), gcnew SolidBrush( Color::Black ), 4, 38 );
   e->Graphics->DrawString( "Left-click a component to update text from the text service if available.",
         gcnew System::Drawing::Font( "Arial",8 ), gcnew SolidBrush( Color::Black ), 4, 50 );

   // Draw lines to represent tree branches.
   e->Graphics->DrawLine( gcnew Pen( gcnew SolidBrush( Color::Black ),1 ), 20, 125, 20, 258 );
   e->Graphics->DrawLine( gcnew Pen( gcnew SolidBrush( Color::Black ),1 ), 21, 175, 45, 175 );
   e->Graphics->DrawLine( gcnew Pen( gcnew SolidBrush( Color::Black ),1 ), 21, 258, 45, 258 );
   e->Graphics->DrawLine( gcnew Pen( gcnew SolidBrush( Color::Black ),1 ), 255, 175, 285, 175 );
   e->Graphics->DrawLine( gcnew Pen( gcnew SolidBrush( Color::Black ),1 ), 255, 258, 285, 258 );
   e->Graphics->DrawLine( gcnew Pen( gcnew SolidBrush( Color::Black ),1 ), 285, 155, 285, 195 );
   e->Graphics->DrawLine( gcnew Pen( gcnew SolidBrush( Color::Black ),1 ), 285, 238, 285, 278 );
   e->Graphics->DrawLine( gcnew Pen( gcnew SolidBrush( Color::Black ),1 ), 285, 155, 290, 155 );
   e->Graphics->DrawLine( gcnew Pen( gcnew SolidBrush( Color::Black ),1 ), 285, 195, 290, 195 );
   e->Graphics->DrawLine( gcnew Pen( gcnew SolidBrush( Color::Black ),1 ), 285, 238, 290, 238 );
   e->Graphics->DrawLine( gcnew Pen( gcnew SolidBrush( Color::Black ),1 ), 285, 278, 290, 278 );

   // Draw color key.
   e->Graphics->DrawRectangle( gcnew Pen( gcnew SolidBrush( Color::Black ),1 ), 20, 305, 410, 60 );
   int y = 0;
   for ( int i = 0; i < 3; i++ )
   {
      e->Graphics->FillRectangle( gcnew SolidBrush( colorkeys[ y ] ), 25 + (i * 140), 310, 20, 20 );
      e->Graphics->DrawRectangle( gcnew Pen( gcnew SolidBrush( Color::Black ),1 ), 25 + (i * 140), 310, 20, 20 );
      e->Graphics->DrawString( keystrings[ y ], gcnew System::Drawing::Font( "Arial",8 ), gcnew SolidBrush( Color::Black ),
            (float)50 + (i * 140), 315 );
      y++;
      e->Graphics->FillRectangle( gcnew SolidBrush( colorkeys[ y ] ), 25 + (i * 140), 340, 20, 20 );
      e->Graphics->DrawRectangle( gcnew Pen( gcnew SolidBrush( Color::Black ),1 ), 25 + (i * 140), 340, 20, 20 );
      e->Graphics->DrawString( keystrings[ y ], gcnew System::Drawing::Font( "Arial",8 ), gcnew SolidBrush( Color::Black ),
            (float)50 + (i * 140), 345 );
      y++;
   }
}

[STAThread]
int main()
{
   Application::Run( gcnew ServiceForm );
}

package ServiceArchitectureExample; 

import System.*;
import System.Drawing.*;
import System.Collections.*;
import System.ComponentModel.*;
import System.ComponentModel.Design.*;
import System.Windows.Forms.*;
import System.Windows.Forms.Design.*;
// Example form provides UI for demonstrating service sharing behavior 
// of a network of IServiceContainer/IServiceProvider controls.

public class ServiceForm extends System.Windows.Forms.Form
{
    // Root service container control for tree.
    private ServiceObjectControl root;
    // Button for clearing any provided services and resetting tree states.
    private System.Windows.Forms.Button resetButton;
    // Color list used to color code controls.
    private Color colorKeys[];
    // Strings used to reflect text service.
    private String keyStrings[];

    public ServiceForm()
    {
        InitializeComponent();
        CreateServiceControlTree();
        colorKeys = new Color[] { Color.get_Beige(), Color.get_SeaShell(),
            Color.get_LightGreen(), Color.get_LightBlue(), Color.get_Khaki(), 
            Color.get_CadetBlue() };
        keyStrings = new String[] { "No service use", "Service not accessible",
            "Service provided", "Service obtained", "Service accessible",
            "No further access" };
    } //ServiceForm

    private void CreateServiceControlTree()
    {
        // Create root service control.
        ServiceObjectControl control1 = new ServiceObjectControl(null, 
            new Size(300, 40), new Point(10, 80), this);
        root = control1;
        // Create first tier - pass parent with service object control 1
        ServiceObjectControl control2 = new ServiceObjectControl(control1,
            new Size(200, 30), new Point(50, 160), this);
        ServiceObjectControl control3 = new ServiceObjectControl(control1,
            new Size(200, 30), new Point(50, 240), this);
        // Create second tier A - pass parent with service object control 2
        ServiceObjectControl control4 = new ServiceObjectControl(control2,
            new Size(180, 20), new Point(300, 145), this);
        ServiceObjectControl control5 = new ServiceObjectControl(control2, 
            new Size(180, 20), new Point(300, 185), this);
        // Create second tier B - pass parent with service object control 3
        ServiceObjectControl control6 = new ServiceObjectControl(control3,
            new Size(180, 20), new Point(300, 225), this);
        ServiceObjectControl control7 = new ServiceObjectControl(control3, 
            new Size(180, 20), new Point(300, 265), this);
        // Add controls.
        this.get_Controls().AddRange(new Control[] { control1, control2, 
            control3, control4, control5, control6, control7 });
    } //CreateServiceControlTree

    public void ResetServiceTree(Object sender, EventArgs e)
    {
        // Remove the service from the service tree.
        if (root.serviceContainer.GetService(TextService.class.ToType()) != null) {
            root.serviceContainer.RemoveService(TextService.class.ToType(), true);
        }
        // Set all controls to "not obtained" and clear their labels.
        for (int i = 0; i < get_Controls().get_Count(); i++) {
            if (!(get_Controls().get_Item(i).Equals(resetButton))) {
                ((ServiceObjectControl)(get_Controls().get_Item(i))).set_state(
                    TextServiceState.ServiceNotObtained);
                ((ServiceObjectControl)(get_Controls().get_Item(i))).label = "";
                ((ServiceObjectControl)(get_Controls().get_Item(i))).
                    set_BackColor(Color.get_Beige());
            }
        }
    } //ResetServiceTree

    public void UpdateServiceCoverage()
    {
        // Have each control set state to reflect service availability.
        for (int i = 0; i < get_Controls().get_Count(); i++) {
            if (!(get_Controls().get_Item(i).Equals(resetButton))) {
                ((ServiceObjectControl)(get_Controls().get_Item(i))).
                    ReflectServiceVisibility();
            }
        }
    } //UpdateServiceCoverage

    #region Windows Form Designer generated code
    private void InitializeComponent()
    {
        this.resetButton = new System.Windows.Forms.Button();
        this.SuspendLayout();
        // 
        // resetButton
        // 
        this.resetButton.set_Location(new System.Drawing.Point(392, 88));
        this.resetButton.set_Name("resetButton");
        this.resetButton.set_TabIndex(0);
        this.resetButton.set_TabStop(false);
        this.resetButton.set_Text("Reset");
        this.resetButton.add_Click(new System.EventHandler(this.ResetServiceTree));
        // 
        // ServiceForm
        // 
        this.set_ClientSize(new System.Drawing.Size(512, 373));
        this.get_Controls().AddRange(new System.Windows.Forms.Control[] { this.
            resetButton });
        this.set_MinimumSize(new System.Drawing.Size(520, 400));
        this.set_Name("ServiceForm");
        this.set_Text("Service Container Architecture Example");
        this.ResumeLayout(false);
    } //InitializeComponent
    #endregion

    /** @attribute STAThread()
     */
    public static void main(String[] args)
    {
        Application.Run(new ServiceForm());
    } //main

    protected void OnPaint(System.Windows.Forms.PaintEventArgs e)
    {
        e.get_Graphics().DrawString("The following tree diagram represents a "
            + "hierarchy of linked service containers in controls.", new Font(
            "Arial", 9), new SolidBrush(Color.get_Black()), 4, 4);
        e.get_Graphics().DrawString("This example demonstrates the propagation"
            + "behavior of services through a linked service object tree.", 
            new Font("Arial", 8), new SolidBrush(Color.get_Black()), 4, 26);
        e.get_Graphics().DrawString("Right-click a component to add or replace"
            + "a text service, or to remove it if the component provided it.", 
            new Font("Arial", 8), new SolidBrush(Color.get_Black()), 4, 38);
        e.get_Graphics().DrawString("Left-click a component to update text from"
            + "the text service if available.", new Font("Arial", 8), 
            new SolidBrush(Color.get_Black()), 4, 50);
        // Draw lines to represent tree branches.
        e.get_Graphics().DrawLine(new Pen(new SolidBrush(Color.get_Black()), 1),
            20, 125, 20, 258);
        e.get_Graphics().DrawLine(new Pen(new SolidBrush(Color.get_Black()), 1),
            21, 175, 45, 175);
        e.get_Graphics().DrawLine(new Pen(new SolidBrush(Color.get_Black()), 1),
            21, 258, 45, 258);
        e.get_Graphics().DrawLine(new Pen(new SolidBrush(Color.get_Black()), 1),
            255, 175, 285, 175);
        e.get_Graphics().DrawLine(new Pen(new SolidBrush(Color.get_Black()), 1),
            255, 258, 285, 258);
        e.get_Graphics().DrawLine(new Pen(new SolidBrush(Color.get_Black()), 1),
            285, 155, 285, 195);
        e.get_Graphics().DrawLine(new Pen(new SolidBrush(Color.get_Black()), 1),
            285, 238, 285, 278);
        e.get_Graphics().DrawLine(new Pen(new SolidBrush(Color.get_Black()), 1),
            285, 155, 290, 155);
        e.get_Graphics().DrawLine(new Pen(new SolidBrush(Color.get_Black()), 1),
            285, 195, 290, 195);
        e.get_Graphics().DrawLine(new Pen(new SolidBrush(Color.get_Black()), 1), 
            285, 238, 290, 238);
        e.get_Graphics().DrawLine(new Pen(new SolidBrush(Color.get_Black()), 1), 
            285, 278, 290, 278);
        // Draw color key.
        e.get_Graphics().DrawRectangle(new Pen(new SolidBrush(Color.get_Black()),
            1), 20, 305, 410, 60);
        int y = 0;
        for (int i = 0; i < 3; i++) {
            e.get_Graphics().FillRectangle(new SolidBrush((Color)colorKeys.
                get_Item(y)), 25 + i * 140, 310, 20, 20);
            e.get_Graphics().DrawRectangle(new Pen(new SolidBrush(Color.
                get_Black()), 1), 25 + i * 140, 310, 20, 20);
            e.get_Graphics().DrawString((String)keyStrings.get_Item(y), 
                new Font("Arial", 8), new SolidBrush(Color.get_Black()),
                (float)(50 + i * 140), 315f);
            y++;
            e.get_Graphics().FillRectangle(new SolidBrush((Color)colorKeys.
                get_Item(y)), 25 + i * 140, 340, 20, 20);
            e.get_Graphics().DrawRectangle(new Pen(new SolidBrush(Color.
                get_Black()), 1), 25 + i * 140, 340, 20, 20);
            e.get_Graphics().DrawString((String)keyStrings.get_Item(y),
                new Font("Arial", 8), new SolidBrush(Color.get_Black()), (float)(
                50 + i * 140), 345f);
            y++;
        }
    } //OnPaint
} //ServiceForm

// An example user control that uses ServiceContainer to add, remove,
// and access services through a linkable service container network.
public class ServiceObjectControl extends System.Windows.Forms.UserControl
{
    // This example user control implementation provides a wrapper for 
    // ServiceContainer, supporting a linked service container network.    
    public ServiceContainer serviceContainer;
    // Parent form reference for main program function access.
    private ServiceForm parent;
    // String for label displayed on the control to indicate the 
    // control's current service-related configuration state.
    public String label;
    // The current state of the control reflecting whether it has 
    // obtained or provided a text service.
    private int state_;
    /** @property 
     */
    public int get_state()
    {
        return state_;
    }//get_state

    /** @property
     */
    public void set_state(int value)
    {
        if (value == TextServiceState.ServiceProvided) {
            this.set_BackColor(Color.get_LightGreen());
        }
        else {
            if (value == TextServiceState.ServiceNotObtained) {
                this.set_BackColor(Color.get_White());
            }
            else {
                if (value == TextServiceState.ServiceObtained) {
                    this.set_BackColor(Color.get_LightBlue());
                } 
                else {
                    if (value == TextServiceState.ServiceNotFound) {
                        this.set_BackColor(Color.get_SeaShell());
                    }
                }
            }
        }
        state_ = value;
    }//set_state

    public ServiceObjectControl(ServiceObjectControl serviceContainerParent,
        Size size, Point location, ServiceForm parent)
    {
        this.state_ = TextServiceState.ServiceNotObtained;
        this.set_BackColor(Color.get_Beige());
        this.label = String.Empty;
        this.set_Size(size);
        this.set_Location(location);
        this.parent = parent;
        if (serviceContainerParent == null) {
            serviceContainer = new ServiceContainer();
        }
        else {
            serviceContainer = new ServiceContainer(serviceContainerParent.
                serviceContainer);
        }
    } //ServiceObjectControl

    // Paint method override draws the label string on the control.
    protected void OnPaint(System.Windows.Forms.PaintEventArgs e)
    {
        e.get_Graphics().DrawString(label, new Font("Arial", 8), new SolidBrush(
            Color.get_Black()), 5, 5);
    } //OnPaint

    // Process mouse-down behavior for click.
    protected void OnMouseDown(System.Windows.Forms.MouseEventArgs e)
    {
        if (e.get_Button().Equals(get_MouseButtons().Left)) {
            if (state_ != TextServiceState.ServiceProvided) {
                // Attempt to update text from service, and set color 
                // state accordingly.
                TextService ts = (TextService)(serviceContainer.GetService(
                    TextService.class.ToType()));
                if (ts != null) {
                    this.label = ts.text;
                    set_state(TextServiceState.ServiceObtained);
                }
                else {
                    this.label = "Service Not Found";
                    set_state(TextServiceState.ServiceNotFound);
                }
            }
        }
        if (e.get_Button().Equals(get_MouseButtons().Right)) {
            if (state_ == TextServiceState.ServiceProvided) {
                // Remove service if the container provided it.
                if (serviceContainer.GetService(TextService.class.
                    ToType()) != null) {
                    serviceContainer.RemoveService(TextService.class.ToType(),
                        true);
                    set_state(TextServiceState.ServiceNotObtained);
                    this.label = "Service Removed";
                }
            }
            else {
                // Obtain string and provide text service.
                StringInputDialog form = new StringInputDialog("Test String");
                form.set_StartPosition(FormStartPosition.CenterParent);
                if (form.ShowDialog().Equals(DialogResult.OK)) {
                    if (serviceContainer.GetService(TextService.class.
                        ToType()) != null) {
                        serviceContainer.RemoveService(TextService.class.
                            ToType(), true);
                    }
                    parent.ResetServiceTree(this, new EventArgs());
                    serviceContainer.AddService(TextService.class.ToType(),
                        new TextService(form.inputTextBox.get_Text()), true);
                    set_state(TextServiceState.ServiceProvided);
                    this.label = "Provided Text: " + form.inputTextBox.get_Text();
                }
            }
        }
        parent.UpdateServiceCoverage();
    } //OnMouseDown

    // Method accesses the TextService to test the visibility of the service 
    // from the control, and sets the UI state accordingly.
    public void ReflectServiceVisibility()
    {
        if (state_ == TextServiceState.ServiceObtained) {
            if (serviceContainer.GetService(TextService.class.ToType()) == null) {
                this.set_BackColor(Color.get_CadetBlue());
            }
        }
        else {
            if (state_ != TextServiceState.ServiceProvided) {
                if (serviceContainer.GetService(TextService.class.
                    ToType()) == null) {
                    this.set_BackColor(Color.get_White());
                    return;
                }
                // Service available.                  
                if (state_ == TextServiceState.ServiceNotFound) {
                    this.set_BackColor(Color.get_Khaki());
                }
                else {
                    if (state_ == TextServiceState.
                        ServiceNotObtained && label != "Service Removed") {
                        this.set_BackColor(Color.get_Khaki());
                    }
                }
            }
        }
    } //ReflectServiceVisibility
} //ServiceObjectControl

// Example service type contains a text string, sufficient 
// to demonstrate service sharing.
public class TextService
{
    public String text;

    public TextService()
    {
         this("");
    } //TextService
    public TextService(String text)
    {
        this.text = text;
    } //TextService
} //TextService

public class TextServiceState
{
    private int member;

    TextServiceState()
    {
        member = 0;
    }// TextServiceState

    TextServiceState(int n)
    {
        member = n;
    }//TextServiceState

    public static int ServiceNotObtained = 0;
    public static int ServiceObtained = 1;
    public static int ServiceProvided = 2;
    public static int ServiceNotFound = 3;
} //TextServiceState

// Example Form for entering a string.
class StringInputDialog extends System.Windows.Forms.Form
{
    private System.Windows.Forms.Button okButton;
    private System.Windows.Forms.Button cancelButton;
    public System.Windows.Forms.TextBox inputTextBox;

    public StringInputDialog(String text)
    {
        InitializeComponent();
        inputTextBox.set_Text(text);
    } //StringInputDialog

    private void InitializeComponent()
    {
        this.okButton = new System.Windows.Forms.Button();
        this.cancelButton = new System.Windows.Forms.Button();
        this.inputTextBox = new System.Windows.Forms.TextBox();
        this.SuspendLayout();
        this.okButton.set_Anchor(System.Windows.Forms.AnchorStyles.Bottom 
            | System.Windows.Forms.AnchorStyles.Right);
        this.okButton.set_Location(new System.Drawing.Point(180, 43));
        this.okButton.set_Name("okButton");
        this.okButton.set_TabIndex(1);
        this.okButton.set_Text("OK");
        this.okButton.set_DialogResult(System.Windows.Forms.DialogResult.OK);
        this.cancelButton.set_Anchor(System.Windows.Forms.AnchorStyles.
            Bottom | System.Windows.Forms.AnchorStyles.Right);
        this.cancelButton.set_Location(new System.Drawing.Point(260, 43));
        this.cancelButton.set_Name("cancelButton");
        this.cancelButton.set_TabIndex(2);
        this.cancelButton.set_Text("Cancel");
        this.cancelButton.set_DialogResult(System.Windows.Forms.DialogResult.
            Cancel);
        this.inputTextBox.set_Location(new System.Drawing.Point(6, 9));
        this.inputTextBox.set_Name("inputTextBox");
        this.inputTextBox.set_Size(new System.Drawing.Size(327, 20));
        this.inputTextBox.set_TabIndex(0);
        this.inputTextBox.set_Text("");
        this.inputTextBox.set_Anchor(System.Windows.Forms.AnchorStyles.
            Top | System.Windows.Forms.AnchorStyles.Left | System.Windows.Forms.
            AnchorStyles.Right);
        this.set_ClientSize(new System.Drawing.Size(342, 73));
        this.get_Controls().AddRange(new System.Windows.Forms.Control[] { this.
            inputTextBox, this.cancelButton, this.okButton });
        this.set_MinimumSize(new System.Drawing.Size(350, 100));
        this.set_Name("StringInputDialog");
        this.set_Text("Text Service Provide String Dialog");
        this.ResumeLayout(false);
    } //InitializeComponent
} //StringInputDialog

System.Object
  System.ComponentModel.Design.ServiceContainer

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 98, Windows Server 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

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

Community Additions

ADD
Show:
© 2015 Microsoft