Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Copying UserControl Variables to another instance of the UserControl [modified]

Copying UserControl Variables to another instance of the UserControl [modified]

Scheduled Pinned Locked Moved C#
questionjsonhelp
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    TheBlindWatchmaker
    wrote on last edited by
    #1

    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

    D 1 Reply Last reply
    0
    • T TheBlindWatchmaker

      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

      D Offline
      D Offline
      Dustin Metzgar
      wrote on last edited by
      #2

      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 the System.Runtime.Serialization.Formatters.Binary.BinaryFormatter. You binary serialize your object into a MemoryStream and then deserialize it into a new object. Voila - a copy of your original without having to hand-code all the copying.


      Logifusion[^]

      T 1 Reply Last reply
      0
      • D Dustin Metzgar

        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 the System.Runtime.Serialization.Formatters.Binary.BinaryFormatter. You binary serialize your object into a MemoryStream and then deserialize it into a new object. Voila - a copy of your original without having to hand-code all the copying.


        Logifusion[^]

        T Offline
        T Offline
        TheBlindWatchmaker
        wrote on last edited by
        #3

        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!

        D 1 Reply Last reply
        0
        • T TheBlindWatchmaker

          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!

          D Offline
          D Offline
          Dustin Metzgar
          wrote on last edited by
          #4

          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?


          Logifusion[^]

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups