how to create clone of Form

N

Neeraj

hi
I want to make clone of current Form which is currently seen.I Used
attribute [Serializable] above the class declaration which extends
Form. Impleament the ICloneable interface and Make method which code is

public Object Clone()
{
return this.MemberwiseClone();
}
public static object DeepClone(
object obj)
{

object clone = null;
using (MemoryStream ms =
new MemoryStream())
{
BinaryFormatter bf =
new BinaryFormatter();
bf.Serialize(ms, obj);
ms.Position = 0;
clone = bf.Deserialize(ms);
}
return clone ;
}

but it generate SerializationException
which i show

Type 'System.Windows.Forms.Form' in Assembly 'System.Windows.Forms,
Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' is
not marked as serializable.

At
bf.Serialize(ms, obj);
that Line.
any one have any idea regard this please share this.

with regards.
Neeraj
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

It's clear in the message Form is not serializable.

What is the purpose of this? If you just want to create another similar form
in the current app then you need to implement it by yourself. I have no idea
how difficult it will be, especially if you have a lot of controls , You
will have to iterate in the Controls collection and create the same controls
with the same properties.
If you want to recreate it in another machine ( or process ) instead of
creating the controls you should send the properties values and in the
receiving end you will have to reconstruct the form.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top