在Windows窗体中更改内容 [英] Change content in a windows form

查看:90
本文介绍了在Windows窗体中更改内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Windows窗体在C#中创建一个应用程序,我想完全交换掉Windows窗体中的所有内容,并用其他东西替换它.有什么方便的方法吗?

I'm making an application in C# using windows forms, I want to completely swap out all the content in a windows form and replace it with something else. Is there any convenient way to do this?

示例:我有一个菜单,当我单击开始"时,我希望菜单消失并开始游戏.我没有使用XNA或类似的东西,这是整个项目的重点.

Example: I have a menu, when I click "start" I want the menu to disappear and the game to start. I'm not using XNA or anything like that which is kind of the point of this whole project.

推荐答案

为要切换的每个唯一内容集使用一个 Panel .隐藏所有面板,除了最初的面板.创建一个变量 activePanel .将 activePanel 设置为当前显示的面板(即初始面板).

Use one Panel for each unique content set you want to switch. Hide all of the panels, except the initial one. Create a variable activePanel. Set activePanel to current shown panel (i.e. initial one).

当您需要切换时,请执行以下操作:

When you need to switch, do the following:

activePanel.Visible = false;
activePanel = <Panel you want to open now>; //I mean the Control, not an ID or something.
activePanel.Visible = true;

另一种方法是动态删除控件并将其添加到表单中,但是这样一来,您将不得不编写更多代码,但是,您的应用程序内存占用应该更少.

Another approach is to dynamically remove and add Controls to the form, but this way you'll have to write a lot more code, hovewer, your application memory footprint should be lesser.

这篇关于在Windows窗体中更改内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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