0

I am making a game using Windows Forms, and I feel that the easiest way to make different screens (Main Menu, Settings, Etc...) is using a separate form for each screen. I have looked up up many different ways to do this. Many people talk of using form2.Show(); this.Hide();, however, this creates a new popup window. People have also suggested using this.IsMdiContainer = true; to create a new window inside of the current window, but this is also not the functionality I want.

EDIT: Another question, multiple-guis-one-window, explains slightly that this can be achieved using UserControls, but does not elaborate with any examples, or what I should do.

I want to completely replace the current form's functionality with that of a new form.

Is there a way to achieve this? If not, is there a good alternative?

Larkenshine
  • 18
  • 1
  • 5
  • This seems like a strange design, however in cases where you need to switch content completely i have used a tabcontrol without the headers. Also id look at other frameworks like wpf, it has a lot more flexibility, winforms does what winforms does best, its usually a main form that you open other forms from, if you deviate from the whole standardized application look and feel concept it can just be a pain – TheGeneral Jun 03 '18 at 03:42
  • Alright. I will keep this question open, but look into more recent solutions. Thank you! – Larkenshine Jun 03 '18 at 03:45
  • Use a single `Form`. Each screen can be a `UserControl`. To switch between Screens, remove the existing `UserControl` and add the next one at run-time. – Reza Aghaei Jun 03 '18 at 04:10
  • https://stackoverflow.com/a/10769349/17034 – Hans Passant Jun 03 '18 at 09:45

2 Answers2

2

As I understood, you want to keep the same form and change the data inside that form (to not get the effect that your form was closed and reopened again).

The problem is windows form does not support such thing directly (at least not in an optimal way), I would use another UI frameworks for that. However, if you want to stick though with windows forms, you may use GroupBox or Panel tools (available from windows form design tools in Visual studio). Here you can group your elements as required, and show/hide the group/panel as required.

That is one of the best available solution for windows form AFAIK.

Mohammed Noureldin
  • 14,913
  • 17
  • 70
  • 99
  • "*The problem is windows form does not support such thing directly (at least not in an optimal way)*" → Wrong. You can simply remove (and dispose) existing controls and add another one at runtime. – Reza Aghaei Jun 03 '18 at 06:32
  • @RezaAghaei, I meant in an elegant direct way, I just misused the words here (were the form can be completely changed with an atomic command). – Mohammed Noureldin Jun 03 '18 at 13:44
1

You want to open child forms within main form then you should try this i have create it without any User Control.

I have manage one parent form and two child forms. child forms should be open within parent form.

  • frmMain(Parent Form)

enter image description here

  • Set frmMain Property as Following

    WindowState = System.Windows.Forms.FormWindowState.Maximized

I have take 3 panel in frmMain window.

  1. pnlMenu (For Display Menu)

    • Set pnlMenu.Dock = System.Windows.Forms.DockStyle.Top property
    • Set height of this panel as you require.
  2. pnlMain (For Display Child Forms)

    • Set pnlMain.Dock = System.Windows.Forms.DockStyle.Fill property
  3. pnlFooter (For Footer Section)

    • Set pnlFooter.Dock = System.Windows.Forms.DockStyle.Bottom; property
    • Set height of this panel as you require.

I have set menubar in pnlMenu(Click on that menu for display child form in pnlMain)

frmMain.cs

    public void BindFormIntoMainForm(Form Main)
    {
        Main.TopLevel = false;
        Main.WindowState = FormWindowState.Maximized;
        Main.AutoScroll = true;
        pnlMain.Controls.Clear();
        pnlMain.Controls.Add(Main);
        pnlMain.Refresh();
        Main.Show();
    }

    private void childToolStripMenuItem_Click(object sender, EventArgs e)
    {
        frmChildForm1 ChildForm1 = new frmChildForm1();
        BindFormIntoMainForm(ChildForm1);
    }

    private void childForm2ToolStripMenuItem1_Click(object sender, EventArgs e)
    {
        frmChildForm2 ChildForm2 = new frmChildForm2();
        BindFormIntoMainForm(ChildForm2);
    }

BindFormIntoMainForm method responsible for display child form in main window.

  • frmChildForm1 & frmChildForm2(ChildForm)

  • Set both form Property as Following

    FormBorderStyle = System.Windows.Forms.FormBorderStyle.None

Now when you click on Child Form 1 Menu then display following output: enter image description here

when you click on Child Form 2 Menu then display following output: enter image description here

I think it can be helpful for you.

Krunal Soni
  • 153
  • 6
  • This might work on the surface, but keeping up with logic between both form contents is going to be painful. There must be better ways. – fdrobidoux Sep 02 '22 at 17:04