Multiple forms one windows C# possible or only panels/usercontrol?

后端 未结 1 524
渐次进展
渐次进展 2020-12-22 05:28

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 f

相关标签:
1条回答
  • 2020-12-22 05:56

    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.

    0 讨论(0)
提交回复
热议问题