I am trying to create a menu for a game I' m developing. This is the source code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Racing_Manager
{
public partial class Form1 : Form
{
Form1 form1 = new Form1();
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
//Exit Button
private void button1_Click(object sender, EventArgs e)
{
Application.Exit();
}
//Help Button
private void Help_Click(object sender, EventArgs e)
{
}
//Play Button
private void Play_Click(object sender, EventArgs e)
{
Hide();
Form2 secondMenu = new Form2();
secondMenu.Show();
}
}
}
Then I have:
namespace Racing_Manager
{
public partial class Form2 : Form
{
public Form2 form2 = new Form2();
public Form2()
{
InitializeComponent();
}
//Back Button
private void button2_Click(object sender, EventArgs e)
{
Hide();
Form1 form1 = new Form1();
form1.Show();
}
}
}
When I run this, it gives a System.StackOverflowException (I know, quite appropriate) on this line:
Form1 form1 = new Form1();
What am I doing wrong? How do I fix it? Anything I can do to improve the code quality?