Copying UserControl Variables to another instance of the UserControl [modified]
-
Greetings! I was wondering if I could get some direction to get me started in implementing a "copy/paste" of the properties of a UserControl. In my application, I have 10 instances of a particular UserControl. This UserControl has, say, 10 properties. I would like to be able to use the right-click menu that I have for this UserControl and be able to copy the "settings" of one instance to any other instance of the UserControl. From what I've read on MSDN, I understand that I have to make the UserControl class Serializable and that the variables that I do not copied can be excluded using the [NonSerializable] attribute, thus so:
[Serializable] public class ctlMyControl : ctlBaseControl { int nNum1; // Copied float fNum2; // Copied [NonSerializable] float fNum3; // Not Copied // The rest of the code }
My question is how does one implement the actualy copy and paste? What function(s) do I use to perform the copy and what function(s) do I use to paste? Your help is appreciated! Thanks! -- modified at 13:39 Thursday 6th July, 2006 -
Greetings! I was wondering if I could get some direction to get me started in implementing a "copy/paste" of the properties of a UserControl. In my application, I have 10 instances of a particular UserControl. This UserControl has, say, 10 properties. I would like to be able to use the right-click menu that I have for this UserControl and be able to copy the "settings" of one instance to any other instance of the UserControl. From what I've read on MSDN, I understand that I have to make the UserControl class Serializable and that the variables that I do not copied can be excluded using the [NonSerializable] attribute, thus so:
[Serializable] public class ctlMyControl : ctlBaseControl { int nNum1; // Copied float fNum2; // Copied [NonSerializable] float fNum3; // Not Copied // The rest of the code }
My question is how does one implement the actualy copy and paste? What function(s) do I use to perform the copy and what function(s) do I use to paste? Your help is appreciated! Thanks! -- modified at 13:39 Thursday 6th July, 2006Well, I'm not sure about the right-click menu, but serializing is a trick you can use to copy a complex object. Mark the class as
Serializable
and then use theSystem.Runtime.Serialization.Formatters.Binary.BinaryFormatter
. You binary serialize your object into aMemoryStream
and then deserialize it into a new object. Voila - a copy of your original without having to hand-code all the copying.
-
Well, I'm not sure about the right-click menu, but serializing is a trick you can use to copy a complex object. Mark the class as
Serializable
and then use theSystem.Runtime.Serialization.Formatters.Binary.BinaryFormatter
. You binary serialize your object into aMemoryStream
and then deserialize it into a new object. Voila - a copy of your original without having to hand-code all the copying.
Hi Dustin, Could you give me some example code or point me to some examples of how this could be done? Thanks for your time!
-
Hi Dustin, Could you give me some example code or point me to some examples of how this could be done? Thanks for your time!
Sure thing. I needed to be able to copy a
CodeCompileUnit
object. This represents an entire CodeDom graph which can get really huge. It's a Microsoft library, so I can't just edit the code myself, and digging through the entire graph to copy it would not only be tedious but potentially faulty. It's just not an option. Luckily, serialization is there:private CodeCompileUnit CopyCcu(CodeCompileUnit ccu) {
try {
BinaryFormatter bf = new BinaryFormatter();
MemoryStream ms = new MemoryStream();
bf.Serialize(ms, ccu);
ms.Position = 0L;
CodeCompileUnit newCcu = bf.Deserialize(ms) as CodeCompileUnit;
return newCcu;
}
catch (Exception exc) {
// Do something
}
}This is at least how you would get a copy of your
UserControl
. I'm lost on the whole right-click thing. Where are you doing that? In the Visual Studio designer?