DesignModeDialog Sample
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.
FAQ
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.
Comments
Anonymous
March 14, 2005
The comment has been removedAnonymous
March 15, 2005
You are absolutely correct, you could use the designer infrastructure in v1.1, it was just much more difficult to do. Tim’s sample is amazing because he figured out how to stitch everything together all on his own. His sample shows just how difficult it was in v1. Tim had to implement 6 different services as well as IDesignerHost and ISite. This took over 1200 lines of code. And as Tim points out, the documentation around these services and their interactions is very poorly documented.
In v2, all those services have a default implementations and are all instanced and ready to go when you create an instance of the DesignSurface class. The documentation around these services and how they all tie together has also improved in v2.
When I referred to creating your own selection UI and move and resize logic, I was discussing a poor-man’s way of achieving some designer functionality that many customers implemented in v1.1.Anonymous
March 20, 2005
Blog link of the week 11Anonymous
September 26, 2007
PropertyGrid PropertyGridcontrolisanexactreplicaofthePropertieswindowcontainedintheVi...Anonymous
January 21, 2009
PingBack from http://www.keyongtech.com/572354-how-to-cancel-this-event