多种形式一个窗口 C# 可能还是只有面板/用户控件? [英] Multiple forms one windows C# possible or only panels/usercontrol?

查看:20
本文介绍了多种形式一个窗口 C# 可能还是只有面板/用户控件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以这是我的问题,我是 C# 的新手(自学) 事情就是这样,我正在开发一个基本的 sim 游戏,没什么复杂的,但我已经完成了设计和基本功能.

So here's my Question, I'm new to C#(teaching my self at that) Here's the thing, I'm working on a basic sim game, nothing to complex but I've got the design and basic functions done.

但是为了实现它,我目前使用多个表单(Visual Studio 2013)我有我的主"表单,其中包含操作"按钮

However In order to implement it, I'm currently using multiple Forms(Visual Studio 2013) I have my "main" form which has the "action" buttons to it

所以当我想转到用户个人资料页面时,我有

So when i want to go to a user Profile page I have

Btn_profileview Click(object sender, EventArgs e){
 Form profile = new Form();
        profile.Show();
}

然后用户将实现写入文本文件的更改(例如更改名称),以用于程序的其他区域.然而,它打开了一个新窗口,我尝试了模态和非模态窗口,虽然模态的好处所以他们必须实际关闭窗口解决了这个问题,我宁愿让它覆盖预先存在的表单,然后关闭回到主"屏幕而不实际使用多个窗口.

The User would then implement the changes(for instance change name) which is written to a text file, for use in other areas of the program. However It opens a new windows, I've tried modal and nonmodal windows and while the benefit of Modal so they have to actual close the window solves the issue, i'd rather have it just overwrite the preexisting Form, and then on close go back to the "main" screen without actually using multiple windows.

现在我被告知 UserControl 和/或 Panel 会解决这个问题,但它会导致从多个表单移动到多个面板屏幕并弄清楚如何让它们工作(可见和不可见)的完整重新设计,我'我假设沿着 Panel"name".show(); 行不会是非常困难的事情;和面板名称".close();

Now I was told UserControl and/or Panel would solve the issue, but it would cause a complete redesign moving from the multiple forms to the multiple panel screens and figuring out how to get those to work(Visible and Invisible), i'm assuming it wouldn't be extremely difficult something along the lines of Panel"name".show(); and panel"name".close();

但是是否有可能在预先存在的代码中实际添加一行代码(以免导致完全重新设计),或者面板和用户控件是在 1 个连续窗口内实现的唯一真正方法吗?

But would it be possible to actually add a line of code to the pre-existing code(so as not to cause a complete reesign) or are Panels and UserControl the only real way to implement within 1 continuous windows?

推荐答案

paqogomez 是对的:有很多方法可以做到.

paqogomez is right: There are many ways to do it.

这是一个结合了很多优点的:

Here is one that combines a lot of the pros:

您可以在窗口上创建一个不可见 Tab,其中包含您需要的任意数量的页面.在每个选项卡上放置一个 Panel 并在其上创建所有控件.这并不意味着您必须从头再来 - 您可以毫不费力地移动和放下已有的控件.当然,您需要关闭停靠和锚点,但除此之外,这是一个简单的过程.

You create an invisible Tab on your window with as many pages as you need. Place a Panel on each tab and create all your controls on of them. This does not mean that you have to do it all over - you can move and drop the controls you already have without much hassle. Of course you need to turn off docking and maybe anchors, but other than that this is a simple process.

如果您在第二个表单上有相同名称的控件,那么这些名称应该更改为独特的.我希望所有控件都已经有了正确的名称,但尤其是 Labels 被忽略了,至少在这里..(如果幸运的话,您甚至可以使用剪切和粘贴将控件从另一个表单获取到面板 2!)

If you have controls on the 2nd form with the same name, these names should be changed to something unique though. I hope all Controls have proper names already, but especially Labels get neglected, at least here.. (With a little luck you can even use cut and paste to get Controls from another form to panel2!)

这个技巧的最大优点是您可以在相同形式的设计器中完成所有工作.Tab 控件仅用作容器,您可以在其中保留面板,而无需添加到 UI 中,也不会让用户控制显示的内容.

The big pro of this trick is that you can do all work in the designer of the same form. The Tab control serves only as a container where you keep your panels without adding to the UI and without giving the user control of what is shown.

接下来在主窗体中创建one Panel 变量:

Next you create one Panel variable in your main form:

Panel currentPanel;

加载时,您可以像这样将第一个真实"Panel 分配给它:

On Load you assign the first 'real' Panel to it like this:

currentPanel = panel1;
this.Controls.Add(currentPanel);

以后,每次要切换时,都像这样重新分配所需的面板:

Later, each time you want to switch, you re-assign the panels you need like this:

this.Controls.Remove(currentPanel);
currentPanel  = panel2;  // or whichever panel you want to show..
this.Controls.Add(currentPanel );

如果您的真实面板停靠以填充标签页,那么currentPanel 将填充表单.您仍然可以随时通过名称访问每个面板和每个控件,但您看不到选项卡的开销,并且您的表单永远不会改变,除了完整内容.

If your real panels are docked to fill the tabpage, as they should, the currentPanel will fill the form. You still have access to each panel and to each control by their names at any time but you see no overhead of tabs and your form never changes, except for the full content.

这篇关于多种形式一个窗口 C# 可能还是只有面板/用户控件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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