创建具有多个页面的 Win 表单的最有效方法是什么? [英] What is the most efficient way to create a Win form with multiple pages?

查看:33
本文介绍了创建具有多个页面的 Win 表单的最有效方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的目标
我正在使用 Visual Studio 2013 在 C# 中处理一个项目.该项目是我打算包含很多页面的项目.这些页面都使用按钮链接在一起.我的问题是我无法为此提出一个有效而优雅的解决方案.

My goal
I am working on a project in C# using Visual Studio 2013. The project is one that I intend to contain a lot of pages. These pages are all linked together using buttons. My problem is that I cannot come up with an efficient and elegant solution for this.

我的尝试
到目前为止,我已经为我的问题提出了两种可能的解决方案.首先,我添加了额外的表单,然后按下按钮,我隐藏了当前表单并显示了新表单,如下所示:

My attempts
So far I have came up with two potenial solutions to my problem. First I added extra forms and then on button press I hid the current form and displayed the new form, like so:

Form2 frm = new Form2();
frm.Show();`
Form1.Hide();

虽然这确实有效,但我有两个问题.

While this does work, I have two problems with it.

  1. 我的项目最终会有数百个表单
  2. 表单之间的过渡看起来很草率.我的目标是提供一种类似过渡的浏览器,所有导航都发生在一个窗口上,而无需打开和关闭其他窗口.

我尝试的第二个可能的解决方案结合了面板的使用.所以我基本上在不同的面板上创建了每个页面.然后在按下按钮时显示适当的面板,其余的则隐藏.像这样:

The second potential solution I tried incorporated the use of Panels. So I essentially created each page on a different Panel. Then the appropriate panel was shown upon a button press and the rest were hidden. Like this:

private void button1_Click(object sender, EventArgs e)
{
    mainMenuPanel.Hide();
    submenuPanel1.Show();
    submenuPanel2.Hide();  
    submenuPanel3.Hide();   
    submenuPanel4.Hide();     
}

这正是我一直在寻找的,但我的问题是管理大量面板很快就变成了一场噩梦.编辑隐藏在其他 9 个面板后面的面板上的控件,并且我的项目中的面板数量只会增加 - 这似乎不是当前形式的理想解决方案.

This is exactly what I was looking for however my issue with it is that managing the vast amount of panels quickly became a nightmare. Editing the controls on a Panel that was hidden behind 9 other Panels and as the number of panels in my project was only going to grow - this does not seem like the ideal solution in its current form.

在我的脑海中,我认为 Visual Studio 2013 中可能有一个选项可以让我隐藏"我没有在表单上使用的面板,或者暂时将它们从表单中拖出.这是 Visual Studio 中的一个选项.

In my head I thought there maybe an option in Visual Studio 2013 that allows me to 'hide' the Panels I am not using on the form, or drag them off the form temporarily. Is that an option in Visual Studio.

如果没有,你们中有人知道实现这一目标的更有效和易于管理的方法吗?

If not do any of you know a more efficient and manageable way of achieving this?

提前致谢.

推荐答案

如果您在使用 WinForms 时遇到困难,最好的办法可能是使用 UserControls.您实际上可以将 UserControl 类扩展为页面",即:UserControlPage.这使得表单在功能上更简单,但是如果控件需要相互通信,您将需要在处理事件/传递数据方面做一些挑剔的工作.

If you are stuck using WinForms, your best bet is probably using UserControls. you can actually extend the UserControl class out to be a "page" ie: UserControlPage. This makes the form much simpler in function, but you will need to do some finicky work with handling events /passing data if the controls need to talk to each other.

如果您不习惯使用 Winforms,WPF 本身就支持所有这些,并且具有用于构建您需要的所有页面、存储/填充数据和传播事件的出色工具.

if you aren't nailed into using Winforms, WPF supports all of this natively, and has wonderful tools for building all the pages you would need, and storing/populating your data, and propagating events.

这篇关于创建具有多个页面的 Win 表单的最有效方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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