简体   繁体   中英

How to copy the content of a .NET panel

I've got a System.Windows.Forms.Panel in a System.Windows.Forms.Form . In the panel I draw lines with myGraphic.DrawLine(myPen, myMouseDownPoint, myMouseUpPoint) . It works fine so far.

Now I want to show the content of the panel in another System.Windows.Forms.Panel . But this doesn't work.

What I tried was:

this.panel1 = MyForm1.panel1;
this.panel1.Refresh();

How can I resolve this issue?

  1. You won't be able to add the exact same Panel instance to several forms. They are designed to go on one, and only one, Form .
  2. You are not properly accessing the panel from the other form; it's not (I hope anyway) static, and you don't have an instance of the other form.

What would be best is to make a utility function to generate a Panel , and both Form instances can call that utility method.

public static class UtilityMethods
{
    public static Panel CreatePanel()
    {
        Panel panel = new Panel();
        //do stuff to panel, draw your lines, etc.
        return panel
    }
}

There are two important points here. The first is that each time you call the method you're creating a new panel. If each Form calls the method there will be two separate panels, not one shared panel. Additionally neither Form will need to be accessing the other (it doesn't sound like they should be, from a design point of view). It makes much more architectural sense for them to both call some 3rd party to do this work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM