Click here to Skip to main content
Click here to Skip to main content

Component and ISupportInitialize: A Quick Guide

, 10 Feb 2005
Rate this:
Please Sign up or sign in to vote.
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.)

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():

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

Share

About the Author

opedog
Software Developer
United States United States
No Biography provided

Comments and Discussions

 
Answerthe non-UI part of the designer (does anyone know what the heck that's called?) Pinmemberkhovanskiian3-Dec-08 9:06 
GeneralEditor for ContainerControl Pinmembertwesterd8-Jun-06 17:12 
QuestionBut how do I get access to the controls? PinsussAnonymous8-Jun-05 8:56 
This will run at the same time, but how can I get a reference to the parent in order to set the tabs?

AnswerRe: But how do I get access to the controls? Pinmemberopedog18-Jul-05 7:51 

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

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

| Advertise | Privacy | Mobile
Web04 | 2.8.141015.1 | Last Updated 10 Feb 2005
Article Copyright 2005 by opedog
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid