Windows forms and the DesignMode property

In .Net Windows Forms, any form or control that derives from a Component has a DesignMode property that tells you whether the control is being rendered in design mode in Visual Studio. This is useful if your control has different behaviour at runtime than at design time (for example, if you show a connection dialog when a form opens but you don’t want it to happen in design mode inside Visual Studio).

The DesignMode property has its quirks though. Using Lutz Roeder’s Reflector reveals the implementation of the DesignMode property:

protected bool DesignMode
{
    get
    {
        if (this.site != null)
        {
            return this.site.DesignMode;
        }
        return false;
    }
}

This shows that design time support is not hooked up until the control is sited. Siting happens after the control is created, but before any properties are set, so if you check the DesignMode property in the constructor of a control, it will always be false.

There is also a bug with the DesignMode property whereby a custom control inside a custom control will always report its DesignMode property as false. Microsoft has more details of this in knowledge base article KB839202 and in their Visual Studio feedback website.

A workaround to both of the above problems it to use the following:

Application.ExecutablePath.ToLower().IndexOf("devenv.exe") > -1

A bit crude, but it works.


Extracting MSI files

The latest trend for even the simplest of tools is to provide the executables as an MSI file, requiring an installation to run the application. Lots of applications simply do not require an installation and will work if the .exe contained in the MSI file is run.

Scott Willeke has made a nice little tool called Less MSIerables (aka lessmsi) that can extract the contents of an MSI file directly to disk with no installation. Extracting the contents and running the .exe has worked successfully on every simple application I’ve tried so far.


Setting a NULL field in SQL Server Management Studio

SQL Server Management Studio provides a simple results view to show the data contained in a table. With this view, it’s possible to edit the data in the table but there is no obvious method of setting a nullable field to null - emptying the cell simply sets the value to empty, which may or may not work depending on the type of the column.

To set a null value, you have to use the Ctrl-0 (Control + Zero) shortcut. Why is there no option in the GUI to do this?


Microsoft icons

Microsoft’s Brad Abrams and Somasegar both posted about a set of standard Microsoft icons that ship with Visual Studio 2005. They can be found in a zip file in the Visual Studio 2005 installation directory (usually C:\Program Files\Microsoft Visual Studio 8\Common7\VS2005ImageLibrary\VS2005ImageLibrary.zip). There are over 600 icons in total with a mixture of Windows, Office and Visual Studio icons, and are licenced for reuse in your own applications.


Sharing Strong Name Keys Across Multiple Projects In VS2005

I’ve recently been doing some work in Visual Studio 2005 which consisted of several projects in a single solution that I’d converted from Visual Studio 2003 format. Since the AssemblyKeyFile attribute has been deprecated, I began to add the shared strong name key to each project using the project properties view. The problem with this is that it copies the strong name key file to each project directory, making a shared key a bit pointless. This behaviour doesn’t happen (as explained in more detail by Shawnfa) if the strong name key file is added to the project as a linked item before setting it in the project properties view. This leaves a single strong name key file on disk, but still includes it in each project as with Visual Studio 2003’s AssemblyKeyFile attribute.