0

Today I would like to create a copy/paste function for a software I develop.

I have a Panel that contains Controls and I want to copy/paste.

I have a selection tool that permit the user to select different Controls and add this Controls to a List. I have called it "SelectionActuelle". Then, when the user clicks on "Copy". I would like to add every controls that SelectionActuelle contains into a new List called "PressePapier".

But when I do, it copies the same pointer reference, and I dont want.

I throught that add a Control to another List should copy it and create a new instance but it doesn't. I tried this example HERE but it doesn't work.

What I have now is only 6 lines (it doesn't work !!) to try to make a copy of the List :

private void bt_copier_Click(object sender, EventArgs e)
    {
        PressePapier.Clear();

        foreach(Control ctr in SelectionActuelle)
        {
            PressePapier.Add(ctr);
        }            
        bt_coller.Enabled = true;
    }

How can I simply copy Control to make my Copy/Paste tool ? So is it possible (I think yes but we never know) ?

Have a good day, Julien

Community
  • 1
  • 1
  • I also tried [this](http://stackoverflow.com/questions/3473597/it-is-possible-to-copy-all-the-properties-of-a-certain-control-c-window-forms) with no success – Julien Gidel Dec 14 '16 at 10:36
  • `PressePapier = PressePapier.ToList();` this will make copy of list but I'm not sure if this can help you or not. because there is not enough detail in your question. – M.kazem Akhgary Dec 14 '16 at 10:36
  • What kind of details do you want ? I will give you more if you need :) – Julien Gidel Dec 14 '16 at 10:37
  • _But when I do, it copies the same pointer reference, and I dont want._ you need to explain why you don't want same reference and what problems it causes that you don't want. – M.kazem Akhgary Dec 14 '16 at 10:38
  • A control can only appear ones on the gui. You should create a deep-copy. You might wanna try: `PressePapier.Add((Control)Activator.Create(ctr.GetType()));` But you might need to copy the properties also. – Jeroen van Langen Dec 14 '16 at 10:42
  • When I do _PressePapier.Add(ctr);_ it copies the same Control that SelectionActuelle contains (I assume that .NET copies the pointer reference). My goal is to **create a Copy/Paste tool**. So I don't want the same Control, but the same Properties(type, size, etc) in a **new** Control. – Julien Gidel Dec 14 '16 at 10:43
  • Implement the IClonable interface, create a new instance of the control, and copy the property values manually. If you have other reference types in your properties, make sure they are deep copied also. If there are too many properties to do it manually, use reflection like in the other SO answer you posted. What exactly is the problem with this solution? – Chris Schmitz Dec 14 '16 at 10:48
  • You should foreach the selected controls and check it's type, just generate a new instance of that control with it's properties. – Jeroen van Langen Dec 14 '16 at 10:48
  • Thanks for your answer, i will check every tips ! – Julien Gidel Dec 14 '16 at 10:58

1 Answers1

0

Thanks to you, I found out that my structure was not great and I changed it to a simpler structure and more reutilisable.

I will apply this :

You should foreach the selected controls and check it's type, just generate a new instance of that control with it's properties.

But Dr. Coconut, my problem was that .NET does not appreciate some expressions in the code even if I have adapted it... I don't know why. Question of framework version ?