简体   繁体   English

Visual C#/ WinForms面板

[英]Visual C#/WinForms Panels

I am trying to overlap panels so that whenever I click a button A certain panel will be visible. 我试图重叠面板,以便每当我点击一个按钮时,某个面板将可见。

However doing this job is very tricky as because the panels doesn't overlap. 然而,做这项工作非常棘手,因为面板不重叠。

for ex. 对于前 I have panel 1 and panel 2: 我有面板1和面板2:

I make panel 2 the same as panel 1, Whenever I position them on the same position... Sometimes, the panel 2 becomes a member of panel 1 and whenever I set the visibility of panel 1 to true panel 2 also shows up. 我将面板2与面板1相同,每当我将它们放置在相同的位置时...有时,面板2成为面板1的成员,每当我将面板1的可见性设置为真时,面板2也会显示出来。

What I want is that the two panels to overlap each other. 我想要的是两个面板相互重叠。

"Btw, I'm making a vertical tab that's why I thought that hiding, unhiding the panels might be my best approach. “顺便说一句,我正在制作一个垂直标签,这就是为什么我认为隐藏,取消隐藏面板可能是我最好的方法。

Is it possible to make the panels overlap each other? 是否可以使面板相互重叠?

The designer is fighting you do get them overlapped. 设计师正在与你作斗争让他们重叠。 You need to use a little trick to stop the bottom panel from sucking up the overlapping panel. 您需要使用一个小技巧来阻止底部面板吸收重叠面板。 Put it overlapping panel somewhat off towards the upper-left so they are truly overlapped. 将它重叠的面板朝左上方放置,使它们真正重叠。 Then put it in the right spot by adding code to the form constructor: 然后通过向表单构造函数添加代码将其放在正确的位置:

    public Form1() {
        InitializeComponent();
        panel2.Location = panel1.Location;
        panel2.Size = panel1.Size;  // optional
    }

Another way to do it is with View + Other Windows + Document Layout. 另一种方法是使用View + Other Windows + Document Layout。 You can drag and drop the inner panel to the outer container (form). 您可以将内部面板拖放到外部容器(窗体)。 You will however have to edit the Location property by hand. 但是,您必须手动编辑“位置”属性。

It is absolutely possible to have overlapping panels. 绝对可能有重叠的面板。

The problem you're facing is that the GUI editor treats your panel as containers (that is true) and as long as you place something (including other panel) within a panel it is "nested" in this container. 您面临的问题是GUI编辑器将您的面板视为容器(这是真的),并且只要您在面板中放置某些内容(包括其他面板),它就会“嵌套”在此容器中。

To avoid this behavior first place one panel and position/size it appropriately. 为避免此行为,首先放置一个面板并适当地定位/调整它。 Then right-click on it and choose "Lock controls". 然后右键单击它并选择“锁定控件”。 That will lock all current form controls and you will be able to put new controls -- including panels -- directly over them, without any fear that something will be nested or somehow placed inside an existing container. 这将锁定所有当前的表单控件,您将能够将新控件(包括面板)直接放在它们上面,而不必担心某些东西会嵌套或以某种方式放置在现有容器中。

And of course your controls can overlap -- consider only the order or control creation, that will also define the z-order of them in the form -- controls added later and drawn later and thus are positioned on top of those added earlier. 当然,您的控件可以重叠 - 仅考虑订单或控件创建,也将在表单中定义它们的z顺序 - 稍后添加并稍后绘制的控件,因此位于之前添加的控件之上。

EDIT: Unfortunately I wasn't completely right with my answer. 编辑:不幸的是,我的回答并不完全正确。 Locking panels does not prevent them from sucking up the controls places entirely within them. 锁定面板不会阻止它们完全吸入控制位置。 But in case of a partial overlap both containers are created on the same level of deepness, so the problem does not exist in case of overlapping panels, as it was asked in the question. 但是在部分重叠的情况下,两个容器都在相同的深度水平上创建,因此在重叠面板的情况下不存在问题,正如问题中所询问的那样。

The Panel has a property called Location, which you can modify to fit your needs. Panel有一个名为Location的属性,您可以根据自己的需要进行修改。 As long as you manage to place your Panel so that it is given the correct parent, you can change the position by altering the Location property later. 只要您设置了Panel以便为其提供正确的父级,您就可以稍后通过更改Location属性来更改位置。 There's really no need to put design code into the constructor or anything such. 实际上没有必要将设计代码放入构造函数或其他类似的东西中。

And to place the panel in the correct parent, just have the parent selected and double click the Panel control in the toolbar, rather than dragging it into the form manually. 要将面板放在正确的父级中,只需选择父级,然后双击工具栏中的“面板”控件,而不是手动将其拖动到窗体中。 There's really no need to try to fight the designer on this one. 真的没有必要尝试在这个上与设计师作斗争。

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

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