Click here to Skip to main content
15,867,686 members
Articles / Programming Languages / C#
Article

Component and ISupportInitialize: A Quick Guide

Rate me:
Please Sign up or sign in to vote.
4.39/5 (8 votes)
10 Feb 20051 min read 54.5K   474   24   4
A quick guide about Components and ISupportInitialize intended to help intermediate programmers use the Visual Studio designer more effectively.

Sample Image

Introduction

Ever wanted to create a control like ErrorProvider that sits in the non-UI portion of the UI designer and does stuff? It's fairly easy. I'll show an example here of how to derive from Component and another helpful interface, ISupportInitialize.

Component

The reason ErrorProvider sits in the non-UI part of the designer is because it is a Component. A Component is the base class for all MarshalByRefObjects in the CLR. Other things that derive from Component are ToolTip, Control, HelpProvider, ImageList, and Menu.

The Component also exposes the site to a container and allows them to communicate. A Component can be placed "in any object that implements the IContainer interface, and can query and get services from its container".

Let's say, I wanted to write a component that would sit in the non-UI part of the designer (does anyone know what the heck that's called?) and after all the initialization is complete at run-time, it would fix the tab ordering automatically. Now, here the tab ordering algorithm is trivial, so let's assume it works. (Thanks to Scott McMaster, who wrote this article and was the inspiration for this article. Sorry about hacking up your TabOrderManager so bad.)

C#
using System;
using System.Collections;
using System.Diagnostics;
using System.Windows.Forms;
using System.ComponentModel;
using System.Collections.Specialized;

namespace ComponentTest
{
    public class TabOrderManager : Component, ISupportInitialize
    {
        public void SetTabOrder()
        {
            ...
        }

        // ISupportInitialize members
        public void BeginInit()
        {
        }

        public void EndInit()
        {
            this.SetTabOrder();
        }
    }
}

Once you slap this on your form (and assign your form to the ContainerControl property), you should notice that something funny has happened in InitializeComponent():

C#
private void InitializeComponent()
{
    this.tabOrderManager = new ComponentTest.TabOrderManager();
    ...
    ((System.ComponentModel.ISupportInitialize)(this.tabOrderManager)).BeginInit();
    this.SuspendLayout();
    ...
    this.Controls.Add(...);
    ((System.ComponentModel.ISupportInitialize)(this.tabOrderManager)).EndInit();
    this.ResumeLayout(false);
}

ISupportInitialize adds these things in InitializeComponent automatically. Normally, they are for transacted notification of when batch initialization has started and finished. Now, when the TabOrderManager.EndInit() is called, it sets the tab index of all the components on the form.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Software Developer
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Answerthe non-UI part of the designer (does anyone know what the heck that's called?) Pin
khovanskiian3-Dec-08 9:06
khovanskiian3-Dec-08 9:06 
GeneralEditor for ContainerControl Pin
twesterd8-Jun-06 17:12
twesterd8-Jun-06 17:12 
I like the solution, but I would like it much more if it included an editor assigned to an attibute for the ContainerControl property so that the end-user can select the container using the property designer.
QuestionBut how do I get access to the controls? Pin
Anonymous8-Jun-05 8:56
Anonymous8-Jun-05 8:56 
AnswerRe: But how do I get access to the controls? Pin
opedog18-Jul-05 7:51
opedog18-Jul-05 7:51 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.