如何在 Windows 窗体中动态加载面板?
winform顶部有一些按钮,当我单击其中一个按钮时,下面的面板将加载不同的预定义面板,我该如何实现呢?
请参阅此示例:
there are some buttons on the top of the winform, and when I click one of them, the panel below will load different predefined panel, how can I implement this ?
please see this example:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这是使用标准 WinForms TabControl 的解决方案,其中选项卡在运行时隐藏,但当然它们在设计时可用。
假设:
您不想创建 OwnerDrawn 选项卡,这是可能的。
标准的 WinForms TabControl 将满足您所有的设计时需求。
代码:
在托管 TabControl 的表单的表单加载事件中,使用如下代码:
隐藏选项卡。
然后,“连接”按钮以处理在 TabControl 中选择不同的 TabPage。显然你可以用比这更优雅的方式来做到这一点:
注意:如果您想将辅助表单或用户控件插入到 TabControl 的 TabPage 中:这不是问题:当然使用 UserControl 更简单。将它们插入每个 TabPage 的 Controls 集合中,并将其“Dock 属性”设置为“DockStyle.Fill”。
注意:有一些更奇特的方法可以隐藏选项卡,例如使用派生的 TabControl,如 CodeProject 上所示:WinForm 上的 TabControl 不显示 Tab 标题? 还有其他使用修改后的 WndProc 的解决方案。它们并不难找到。
Here's a solution using a standard WinForms TabControl, where the Tabs are hidden at run-time, but of course they are available at design-time.
Assumptions :
You don't want to get into creating OwnerDrawn Tabs, which is possible.
A standard WinForms TabControl will meet all your design-time needs.
Code :
In the Form Load event of the Form that hosts your TabControl use code like this :
To hide the Tabs.
Then, "wire" up your buttons to handle selecting the different TabPages in the TabControl. Obviously you could do this in a more elegant way than this :
Note : if you want to insert secondary Forms or UserControls into the TabPages of a TabControl : that's not a problem : of course simpler to use UserControls. Insert them into the Controls collection of each TabPage and set their 'Dock Property to 'DockStyle.Fill.
Note : there are fancier ways you can hide the Tabs, like using a derived TabControl as shown here on CodeProject : TabControl on a WinForm without showing the Tab header? There are other solutions out there that use a modified WndProc. They're not hard to find.
我不知道你到底想做什么,但是如果你的表单上有一个名为
contentArea
的面板,并且创建了一堆用户控件(但不在表单上),那么您可以使用它作为按钮的事件处理程序:...尽管正如其他人所说,在这种情况下选项卡控件会更好。
I don't know exactly what you're trying to do, but if you've got a Panel on your form named
contentArea
and a bunch of user controls created (but not on the form), then you could use this as an event handler for a button:...though as other people have said, a tab control would be better in this case.
您可以做的是将它们分别放在单独的
面板
中。将每个的Visible
属性设置为false
。当按钮上发生Click
事件时,将所有按钮的Visible
属性设置为false
并设置您想要显示的Visible
到true
。What you can do is have them each in a separate
Panel
. Set theVisible
property tofalse
to each. When theClick
event on the button, set theVisible
property of all of them tofalse
and set the one you want shown'sVisible
totrue
.例如,如果您有两个表单 Form1 和 Form2,并且您想在 from1 中加载 form2。当您按下按钮加载form2时,代码如下所示,
此代码会将form2中的所有控件加载到form1中。
For example if you have two form Form1 and Form2 and you want to load form2 inside from1. when you press a button to load form2 the code is like this
this code will load all the controls in the form2 into form1.