A component does not recognize if it is in design mode!, a bug?

Hi

I just discovered something interesting while I was creating my library

If you are using the last release of Microsoft Visual Studio.NET 2003, create a new component, and in the component constructor add the following:

if(this.DesignMode) System.Windows.Forms.MessageBox.Show("Design Mode On");

Now, compile the code, place the component on a form, close the form and open it again.

The results are: nothing will happen :):)

The same code will work every ware else in the component in design mode, except the constructor, in the constructor it will work if it get changed to the following:

if(!this.DesignMode) System.Windows.Forms.MessageBox.Show("Design Mode On");

And this means “show a message if it is not in design time”, when in reality it is!

If anyone have access to the bug reporting DB of VS.NET 2003, and that is a bug, please report it.


Answer this question

A component does not recognize if it is in design mode!, a bug?

  • Guillermo Serrato - MSFT

    The DesignMode property depends on a Site being assigned to the component.  And, you cannot guarantee that at any given point within the constructor that the Site is assigned.  An appropriate workaround may be to do the following (a little complicated, though):

    1-  Add a property (bool) to your component.
    2-  Add an event to your component to be raised when the prop value changes.
    3-  Create a designer for your component.
    4-  Override Initialize, assign the property on your component to True
    5-  Catch the event in your component and execute your dependent workload there.

    HTH!

  • abc4567

    ISite is only used at Design Time, and it is essential for storing information about a component and its design time state. Obviously it is a property which needs to be assigned to, so by definition there's no chance of it having been assigned to in the constructor.

    The DesignTime property simply reflects the DesignTime state of the ISite assigned to the component. If one has not yet been assigned, it returns false.

  • Jerrill

    Thanks for the response, I've done something similar, and I had some problems; but from your replay I just figure out the following (inside the component):


    public override ISite Site
    {
    get
    {
    return base.Site;
    }
    set
    {
    base.Site = value;
    if(this.DesignMode)
    {
    // code
    }
    }
    }


    I still have no idea what ISite is doing exactly, but the code seams to work on initialization :) :)

    Thanks again


  • Raj

    This thread is pretty much solved, but I thought I'd spew a bit on what ISite does, both at design time and at run time.  ISite is just a communication mechanism between a component and a container.  The site indicates that the component is "owned" by the container, and the container can offer services through the site that a component can use.  In a framework with a bunch of classes, this mechanism is seldom used, but it becomes very interesting when writing an application.  The designer really is just a specialized application, and at design time we use this ISite mechanism a lot.

    To see when ISite becomes interesting at runtime, drop a timer on a form and look at the code generated. The timer will be created with the following code:

    timer1 = new Timer(components);

    The components parameter is a container, and this constructor on Timer sites the timer to a container.  This is important, because when you close the form there is nothing that would Dispose the timer (and it would continue to tick forever, because each tick "wakes" it up so it won't GC).

    Another way sites are used at runtime is for a primitive form of styles:  if you take all of your forms and site them to a container, and that container offers AmbientProperties as a service, you can control the look and feel of all the forms at once.  This is how Visual Studio gets all of its dialogs to use the same font.

  • Mincemaker

    I *think* that the sole purpose of ISite is to facilitate communication between a component and it's container.  In the case of the designer, ISite is how a component communicates with the ComponentTray.  Anything other than that, I really don't know.

  • A component does not recognize if it is in design mode!, a bug?