Don't you need to call SetValue on a PropertyInfo value from the form you're trying to build (newForm) not the one you're duplicating? I don't know that you can rely on the GetProperties result being in the same sequence on the forms, so you've got work to do to handle the chance that the sequence is different. (Where is your reference to newForm in that code?) But don't answer that!
You'll be (much) better off serializing the source form to a (memory) stream, then de-serialize it to create the duplicate. Then you won't have to worry about having to deal with each property separately, dealing with each control on the form etc. Out of curiosity, why can't you show the user the form that's being designed? Why do you have to make a duplicate of it? At 09:30 AM 9/24/2004, Ragnvald Barth wrote >I am implementing a Windows Forms Designer, based on this article: >http://www.divil.co.uk/net/articles/designers/hosting.asp > >I am trying to figure out how to instantiate and show a form equal to the >form being designed. > >My first idea was something like this: >Form newForm = new Form(), >properties = TypeDescriptor.GetProperties(rootComponent) on the form being >designed, >foreach(PropertyDescriptor pd in properties) >{ > pd.SetValue(rootComponent, pd.GetValue(rootComponent); >} > >It doesnt work. On some properties I get this exception: >Object does not match target type. >Property: BackColor >Type: System.Drawing.Color >Object type: System.Drawing.Color >Object value: Color [Coral] > >There is definitely a type match, so the exception seems strange to me. > >I also have to filter on pd.DesignTimeOnly, pd.IsReadOnly, and >DesignerSerialisationVisiblity.Hidden... >But here again I run into some strange behavior: The forms Size is >hidden... Why? >If I add a button to the form, the buttons location is hidden, but the size >is not... Why? >These properties should all be visible, shouldn�t they? > >Perhaps I am far out and my implementation is totally wrong in the first >place. What is the right approach to this problem? > >Regards, >Ragnvald Barth J. Merrill / Analytical Software Corp =================================== This list is hosted by DevelopMentor� http://www.develop.com Some .NET courses you may be interested in: Essential .NET: building applications and components with CSharp August 30 - September 3, in Los Angeles http://www.develop.com/courses/edotnet View archives and manage your subscription(s) at http://discuss.develop.com
