1

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.

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.

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

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?

M88K
  • 11
  • 1
  • 2
  • I'm not sure I quite understand what you mean by `overwrite the preexisting form` – crthompson Jun 16 '14 at 04:32
  • So basically instead of having another window pop up, it would use the existing window to show form 2's layout and controls, or form 3's, or form 4's. Everything would happen within 1 actual Window instead of popping up in a second window. – M88K Jun 16 '14 at 04:44
  • Lots of ways to do what you're describing. But the advice you've been given is good. You might also try a tabbed control. You can hide the tabs and just switch tabs based on user input. – crthompson Jun 16 '14 at 04:57

1 Answers1

1

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.

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

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.

Next you create one Panel variable in your main form:

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

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.

TaW
  • 53,122
  • 8
  • 69
  • 111