在面板中打开多个FORMS [英] Open multiple FORMS in panel

查看:51
本文介绍了在面板中打开多个FORMS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有4种表格..

MAINFORM ..

FORM1
FORM2
FORM3


在我的mainForm中.我有3个按钮和1个面板...

我想在其中创建应用程序.

如果我单击button1 .. PANEL将显示form1 ..

如果我单击button2 ..面板将显示form2 ..

如果我单击button3 ..面板将显示form3 ...

I have 4 forms..

MAINFORM..

FORM1
FORM2
FORM3


In my mainForm .. I have 3 button and 1 panel...

I want to create application in which..

If i click button1.. PANEL will display form1..

if i click button2 .. panel will display form2..

if i click button3 .. panel will display form3...

推荐答案

这将是对System.Windows.Forms库和预期的UI结构的滥用.面板设计为由表单托管,也不设计为任何控件(包括其他表单)托管.



即使在技术上可以使用P/Invoked原始Windows API SetParent来实现,它也不能通过纯.NET代码工作,因为属性System.Windows.Control.ParentSystem.Windows.Control.Controls是专门设计用于抛出试图使表单成为任何控件的子元素时发生异常.


属性System.Windows.Control.ParentSystem.Windows.Control.Controls专门用于引发异常ArgumentException无法将顶级控件添加到控件中",以试图使任何控件的子窗体成为表单.如Bill所指出的,可以通过将Form.TopLevel设置为false来解决此问题(请参阅下面的评论).如果尝试这样做,您会看到子窗体的非客户区域显示在父控件内,因此,其他解决方法将是提供一个没有它的窗体:

This would be a misuse of System.Windows.Forms library and intended UI structure. A panel is designed to be hosted by a Form, nor a Form is designed to be hosted by any Control (including other Form).



Even though it is technically possible to achieve using P/Invoked raw Windows API SetParent, it won''t work through pure .NET code, as the properties System.Windows.Control.Parent and System.Windows.Control.Controls are specifically designed to throw an exception on an attempt to make a form a child of any control.


The properties System.Windows.Control.Parent and System.Windows.Control.Controls are specifically designed to throw an exception ArgumentException "Top-level control cannot be added to a control" on an attempt to make a form a child of any control. This can be worked around by setting Form.TopLevel to false, as Bill pointed out (see his comment below). If you try to do that, you will see that the non-client area of the child form is shown inside the parent control, so additional work around would be presenting a form without it:

Form child = ...
child.TopLevel = false;
child.ShowIcon = false;
child.FormBorderStyle = FormBorderStyle.None;
whateverPanel.Controls.Add(child);
//or child.Parent = whateverPanel;



(使用P/Invoked raw Windows API SetParent也可以达到相同的效果,但是我不推荐这样做.
使用纯.NET API并避免P/Invoke是Forms编程中非常重要的因素.使用纯.NET可使Forms应用程序跨多个平台移植(通常在Mono下, http://en.wikipedia.org /wiki/Mono_%28software%29 [ ^ ], http://www.mono-project.com/ [
根本不需要建议的行为.如果用面板替换FORM1,FORM2和FORM3,则可以达到类似的效果.导航控制ListBoxTreeView可以使用三个列表项或树节点来代替三个按钮.使用属性System.Windows.Forms.Control.Visible可以使选定的面板可见,而其他面板则隐藏.但是,在仅应切换三个面板的情况下使用这种导航样式似乎不切实际(但是,如果控制切换面板的数字项太大而无法容纳表格,那将是非常有益的).而是应使用System.Windows.Forms.TabControl.它即用即用,方便并且只需要很少的编码.

—SA



(The same effect can also be achieved using P/Invoked raw Windows API SetParent, but I would not recommend it.
Using pure .NET API and avoiding P/Invoke is a very important factor in Forms programming. Using pure .NET keeps Forms application portable across many platforms (typically, under Mono, http://en.wikipedia.org/wiki/Mono_%28software%29[^], http://www.mono-project.com/[^]). A Form application can run on Linux, Mac and other systems without recompilation (I do it all the time), but a single P/Invoke will break such compatibility.
Anyway, there is simply no point in doing any of the above, see below.

[END EDIT]

Also, from the point of view or UI style, this would misuse a button, which should not be used for navigation purposes, because the button has no state reflecting "current position" in navigation.

The suggested behavior is simply never needed. The similar effect could be achieved if you replace FORM1, FORM2 and FORM3 with Panels. Instead of three buttons three list items or tree nodes could be used by a navigation-controlling ListBox or a TreeView. A selected panel could be made visible and other panels hidden using the property System.Windows.Forms.Control.Visible. However, using this navigation style when only three panels should be switched looks impractical (but could be very beneficial if the number items controlling switching panels would be so big that they might not fit the form). Instead, System.Windows.Forms.TabControl should be used. It''s ready-to-use, convenient and require very little coding.

—SA


这听起来像一个设计方案,您还可以考虑使用TabControl:然后将现在的窗体"放入UserControls中,然后将每个UserControl放入TabPage中,或使每个TabPage包含现在位于窗体1〜3上的控件.

我提到这是SA上面建议使用Panels的替代方法.

最好,比尔
This sounds like a design scenario where you could, also, consider using a TabControl: then make what are now your ''Forms'' into UserControls, and put each UserControl into a TabPage, or make each TabPage contain the Controls that are now on Forms 1~3.

I mention this as an alternative to SA''s suggestion above to use Panels.

best, Bill


我同意比尔,使用标签页将满足您的需求.
您可以使用Janus之类的控件,因为它具有一些漂亮的界面.
最好,哈米德(Hamid)
I do agree with bill, Using tab page will do what you need.
You could use controls such as Janus, because it has some beautiful interfaces.
Best, Hamid


这篇关于在面板中打开多个FORMS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆