Create the forms at the begin of your class (outside the methods) like this:
using System;
using System.Drawing;
using System.Windows.Forms;
namespace WindowsFormsApp1
{
public partial class Form2 : Form
{
static Form2 cwForm1;
static Form2 cwForm2;
static Form2 cwForm3;
static int formNumber = 1;
public Form2()
{
InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e)
{
if (cwForm2 == null)
{
cwForm1 = this;
cwForm1.labelNr.Text = "1";
cwForm1.Text = "Test form 1";
cwForm1.BackColor = Color.LightSkyBlue;
cwForm2 = new Form2();
cwForm2.Text = "Test form 2";
cwForm2.labelNr.Text = "2";
cwForm2.BackColor = Color.Beige;
cwForm3 = new Form2();
cwForm3.Text = "Test form 3";
cwForm3.labelNr.Text = "3";
cwForm3.BackColor = Color.LightGreen;
}
}
private void ButtonBack_Click(object sender, EventArgs e)
{
if (formNumber > 1)
{
formNumber--;
ChangeForm();
}
}
private void ButtonNext_Click(object sender, EventArgs e)
{
if (formNumber < 3)
{
formNumber++;
ChangeForm();
}
}
private void ChangeForm()
{
this.Hide();
if (formNumber == 1)
{
cwForm1.Show();
}
else if (formNumber == 2)
{
cwForm2.Show();
}
else
{
cwForm3.Show();
}
}
}
}