简体   繁体   English

以两种Windows形式显示控件

[英]To Show a Control in two Windows Forms

I have inherited a control from Panel-Class. 我从面板类继承了一个控件。 I have added some events to this control. 我已经向该控件添加了一些事件。 I gave move - ability to this control and so on .. 我为该控件提供了移动功能,等等。

I have two display screens. 我有两个显示屏。 I have a main program where the inherited panel is displaying an image on a small area. 我有一个主程序,继承的面板在一个小区域上显示图像。 I want to show this panel fullscreen on a second. 我想全屏显示此面板。

I created a new form and use the same control... But i can not move both screens together. 我创建了一个新表单并使用相同的控件...但是我无法将两个屏幕一起移动。 What should I do ? 我该怎么办 ?

If you want to be able to manipulate both forms at the same time, show the second form with Show() instead of ShowDialog(). 如果希望能够同时操作两种形式,请使用Show()而不是ShowDialog()来显示第二种形式。 You can certainly pass the original panel to the second form and add it to the form's Controls collection. 您当然可以将原始面板传递给第二个表单,然后将其添加到表单的Controls集合中。 I am not sure if this is the best way to do it (sharing one control across two forms), but I don't know your requirements either. 我不确定这是否是最好的方法(在两种形式之间共享一个控件),但是我也不知道您的要求。

I wouldn't use a second form, but a second 'mode' (fullscreen vs. not) on your existing form. 我不会使用第二种形式,而是在现有表单上使用第二种“模式”(全屏或非全屏)。 You can have 2 panel controls, or just one and resize. 您可以有2个面板控件,也可以只有一个并调整其大小。

I think this kind of behaviour calls for a model-view pattern. 我认为这种行为需要模型视图模式。 If that's implemented, the rest should fall into place. 如果实现了,其余的应该就位。

The problem is that you only have one instance of your inherited panel. 问题是您只有一个继承面板的实例。 You actually have to make another "copy" of it, a new instance, before you can add it to the other form. 实际上,您必须先对其进行另一个“复制”,即创建新实例,然后才能将其添加到其他表单中。

Mypanel mypanel1 = new Mypanel();
Mypanel mypanel1copy = new Mypanel();

You can either edit these instances to contain the same data all the time through the run, or use something like "Deep Copy": 您可以在运行过程中始终编辑这些实例以包含相同的数据,也可以使用“深度复制”之类的方法:

How do you do a deep copy of an object in .NET (C# specifically)? 如何在.NET(特别是C#)中对对象进行深层复制?

Keep in mind, that any changes to mypanel1 should be done to mypanel1copy , too. 请记住,对mypanel1所做的任何更改mypanel1应该对mypanel1copy

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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