Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change content in a windows form

Tags:

c#

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?

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.

like image 340
Jacco Avatar asked Nov 27 '12 13:11

Jacco


People also ask

How do I add Text to Windows form?

Step 1: Create a windows form. Step 2: Drag the TextBox control from the ToolBox and Drop it on the windows form. You can place TextBox anywhere on the windows form according to your need. Step 3: After drag and drop you will go to the properties of the TextBox control to set the Text property of the TextBox.

What Windows form controls?

Windows Forms controls are reusable components that encapsulate user interface functionality and are used in client-side, Windows-based applications. Not only does Windows Forms provide many ready-to-use controls, it also provides the infrastructure for developing your own controls.


2 Answers

This works for me: Adding all the controls like:

form.Controls.AddRange(Pages.ToArray());

Then activating the needed one with

form.Controls[i].BringToFront();
like image 98
user2894667 Avatar answered Sep 30 '22 05:09

user2894667


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.

like image 32
J0HN Avatar answered Sep 30 '22 05:09

J0HN