I’ve been getting a few questions and comments about this sample I wrote a few months back so I figured I’d talk about it here.
When we shipped v1.0 of the .NET Framework and VS.NET 2002, many customers asked for a way to put their forms into “design mode” at runtime. What they wanted was a way for the end user to change the position (and perhaps size and set other properties) of controls and then set the app back to its normal “run mode” state.
In v1 and v1.1, there really wasn’t an easy way to accomplish this, you could write your own selection UI and then hook the mouse down, move and up events and provide code for moving and resizing, and perhaps also tack on a property grid if you wanted to change other properties. The problem with this technique is that it wasn’t using the existing designer infrastructure so there was no reuse of existing framework functionality.
In Whidbey we refactored our designer infrastructure such that it’s much easier to consume and use in other applications. I wanted to write a sample highlighting this that wasn’t just another designer. An excellent example of this already exists.
The DesignModeDialog sample addresses both of these points. It’s a component, much like the common dialogs, that has a ShowDialog method. When invoked, ShowDialog takes a snapshot of the parent form, clones all the controls and creates a new designer form with the controls and a property grid to edit select properties. The list of editable properties is a property on the DesignModeDialog which can be changed too. When the design form is closed, the changes are read and put back into the original controls on the running form.
Q: When I open the sample in a Beta 2 CTP build, I get compile errors about Collection<T> not being a known type. Why is this?
A: Since Beta 1, the Base Class Libraries (BCL) team has moved Collection<T> into the System.Collections.ObjectModel namespace. Add a using statement for this namespace to the classes which can’t resolve Collection<T>.
Q: Is there a way to persist changes so that they appear the next time the app is run?
A: I didn’t implement a way to serialize the changes. This will need to be implemented on top of the sample. The new settings API should be helpful in providing persistence.
Q: I want to use the designer infrastructure in my app, but I can’t see any of the classes in System.ComponentModel.Design. What’s up?
A: You need to add a reference to System.Design.dll to your project. This is where all the System.ComponentModel.Design and System.Windows.Forms.Design classes live. Also, if you are creating a custom UITypeEditor, you’ll need to add a reference to System.Drawing.Design.dll.
Q: Does ASP.NET support a similar designer reuse architecture?
A: No, it does not. The ASP.NET designer is also built on top of the Component Model’s designer infrastructure, but the classes are all internal to VS.
Q: Will there be a Beta 2 version of this sample?
A: Yes, this sample will be updated when the final version of Beta 2 is released.