C# 如何复制.NET面板的内容

C# 如何复制.NET面板的内容,c#,forms,panel,draw,C#,Forms,Panel,Draw,我在System.Windows.Forms.Form中有一个System.Windows.Forms.Panel。在面板中,我用myGraphic.DrawLine(myPen、myMouseDownPoint、myMouseUpPoint)绘制线条。到目前为止效果还不错 现在我想在另一个System.Windows.Forms.panel中显示面板的内容。 但这不起作用 我尝试的是: this.panel1 = MyForm1.panel1; this.panel1.Refresh();

我在
System.Windows.Forms.Form
中有一个
System.Windows.Forms.Panel
。在面板中,我用
myGraphic.DrawLine(myPen、myMouseDownPoint、myMouseUpPoint)
绘制线条。到目前为止效果还不错

现在我想在另一个
System.Windows.Forms.panel
中显示面板的内容。 但这不起作用

我尝试的是:

this.panel1 = MyForm1.panel1;
this.panel1.Refresh();
我如何解决这个问题

  • 您将无法将完全相同的
    面板
    实例添加到多个表单中。它们被设计成一个,而且只有一个,
    表单
  • 您没有从其他窗体正确访问面板;它不是(我希望无论如何)静态的,并且没有其他形式的实例
  • 最好的方法是创建一个实用函数来生成一个
    面板
    ,两个
    表单
    实例都可以调用该实用方法

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

    这里有两个要点。第一,每次调用该方法时,您都在创建一个新面板。如果每个
    表单
    调用该方法,则将有两个单独的面板,而不是一个共享面板。此外,两个
    表单
    都不需要访问另一个表单(从设计角度看,这听起来不应该)。对他们来说,打电话给第三方来做这项工作更有建筑意义。

    对不起,忘了提到,第二个面板在第二个System.Windows.Forms.Form中。我试过这个。panel1=MyForm1.panel1;这个.panel1.Refresh();请使用下面的问题添加该信息。