Andre de Cavaignac

Let's blog it out...

April 2007 - Posts

Introducing Gatsb: Mobile photographs and social networking

I'm excited to announce the first public release of Gatsb will be going live today!  Gatsb is a social and mobile networking app I've been meaning to build for a long time and have finally gotten around to.  It allows its users to share photographs taken with a cellular phone with eachother through a central website.

You can tell Gatsb where you are, and then take a picture and send it as MMS, or just send an SMS text message.  Gatsb will post your message at the venue you specify.

 I will be demoing Gatsb at the New York Tech Meetup this Tuesday and encourage you to come see me!

Winforms DesignMode Property: When is it True or False

Here is a little tidbit that I learned back when I was doing lots of control design, but I recently realised not many other people know when a coworker asked me about it.

When designing controls in .NET Windows Forms, there is a property DesignMode which is very tempting to use, especially in a case when you're loading data from a database or web service into the control.  When you do something like this, you generally will get an exception when you're in design mode (for one reason or another).  DesignMode is a property that will seemingly solve this problem, however many people get suprised when DesignMode begins to return false, even though they are still in Visual Studio.

The DesignMode property is only "true" when the control that is checking it is the control being designed.  If the control is on a form or another control, then DesignMode will return false.

For example:  If I override ComboBox and check DesignMode to load data into the combo box only when the program is running, and then I put this control on my form, an exception will be thrown when the form is being designed in Visual Studio because it can't find the SQL connection string in my app.config.

How can this problem be resolved?
To solve this problem, we can walk the chain of controls from our control up the parents, ensuring that none of the controls are in design mode.  Take a look at the sample code below.

        /// <summary>
        /// Gets if the control is in design mode, or if any of its
        /// parents are in design mode.
        /// </summary>
        public bool IsDesignerHosted
        {
            get
            {
                Control ctrl = this;
                while (ctrl != null)
                {
                    if (ctrl.Site == null)
                        return false;
                    if (ctrl.Site.DesignMode == true)
                        return true;
                    ctrl = ctrl.Parent;
                }
                return false;
            }
        }

The code above walks up the controls looking to see if any of them are DesignMode.  Note that Control.DesignMode is a protected property, and therefore cannot be accessed directly.  In this case we check Site.DesignMode instead.

IMPORTANT NOTE:

This should not be used in a constructor.  Use of this code in a constructor will be useless because the control has not yet been added to its parent.  Use the OnVisibleChanged method, and if neccisary, check an "initialized" field you create to ensure that your code only runs once.  This will ensure the control was first placed onto a form.

More Posts