多形式之一窗户C#或可能只面板/用户控件? [英] Multiple forms one windows C# possible or only panels/usercontrol?

查看:215
本文介绍了多形式之一窗户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

所以

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.

现在有人告诉我,用户控件和/或小组将解决这个问题,但它会引起完全重新设计从多个表单移动到多个面板屏幕,并找出如何让这些工作(可见光和不可见的),我假设它不会是沿着面板名.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();

不过,才有可能真正一行代码添加到原有的代码(以免引起完整reesign),或者是面板和用户控件的唯一真正的方法在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:

您创建一个无形 设置页与尽可能多的网页窗口中,你所需要的。放置一个面板每个选项卡上,并在其上​​创建的所有控制。这确实的不可以意味着你必须做这一切了 - 你可以移动或删除你已经拥有了控制,没有什么麻烦。当然,你需要关闭对接,也许主播,但比,这是一个简单的过程等。

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.

如果您有第二个窗体上的控件与同名,这些名称应改为东西唯一虽然。我希望所有的控件有适当的名字了,但特别是标签获得忽视,至少在这里。(随着一点点运气,你甚至可以使用剪切和粘贴来获取其它控件形成以PANEL2!)

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!)

这招的大pro是,你可以做所有的工作在同一形式的设计师。在设置页控制仅作为一个容器,你保持你的面板不会增加用户界面,并没有给出所显示的用户控件。

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.

接下来创建的在主窗体有一个 面板变量:

Panel currentPanel;

在加载指定第一个真正的面板它是这样的:

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 );

如果你的真实面板停靠填补TabPage的,因为他们应该的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天全站免登陆