I am trying to pass a couple variables between 3 separate forms. Form 1 is where each player (2 players) picks their name and type. It then sets player 1 variables into the second form and player 2 variables into the third form.
private void button_player2Ready_Click(object sender, EventArgs e)
{
p2_name = textBox_P2.Text;
if (p2_name.Length > 0 && p2_type != null)
{
foreach (Control cont in groupBox_p2.Controls)
{
cont.Enabled = false;
}
player2Ready = true;
if (player1Ready == true && player2Ready == true)
{
Form1 firstForm = new Form1();
Form2 secondForm = new Form2();
Form3 thirdForm = new Form3();
//thirdForm.p2_name = "hello";
thirdForm.p2_type = p2_type;
thirdForm.p2_name = p2_name;
secondForm.p1_name = p1_name;
secondForm.p1_type = p1_type;
this.Hide();
secondForm.Show();
}
}
else
{
MessageBox.Show("Error: You have not selected a name or type...");
}
}
}
The second form works fine and player 1's name is displayed in the label but I believe this is causing problems when going to 3rd form as it is resetting the name of the third form because of this " Form3 thirdForm = new Form3();"
public string p1_name { get; set; }
public string p1_type { get; set; }
public string word4player2;
public Form2()
{
InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e)
{
label1.Text = $"{p1_name} choose your opponents word...";
}
private void button_submit_Click(object sender, EventArgs e)
{
if (!textbox_p1Word.Text.Contains(" "))
{
word4player2 = textbox_p1Word.Text;
Form1 firstForm = new Form1();
Form2 currentForm = new Form2();
Form3 thirdForm = new Form3();
thirdForm.Show();
Hide();
}
else
{
MessageBox.Show("No Spaces allowed...");
}
}
private void Form2_FormClosed(object sender, FormClosedEventArgs e) //this is needed bc form1 is only hidden when form 2 opens thus not closing application properly
{
Application.Exit();
}
}
This is my third form which is supposed to display player 2's name. The form opens with no errors but the spot where the name goes is blank.
public string p2_name { get; set; }
public string p2_type { get; set; }
public string word4player2;
public Form3()
{
InitializeComponent();
}
private void button_submit_Click(object sender, EventArgs e)
{
if (!textbox_p2Word.Text.Contains(" "))
{
word4player2 = textbox_p2Word.Text;
}
else
{
MessageBox.Show("No Spaces allowed...");
}
}
private void Form2_FormClosed(object sender, FormClosedEventArgs e) //this is needed bc form1 is only hidden when form 2 opens thus not closing application properly
{
Application.Exit();
}
private void Form3_Load(object sender, EventArgs e)
{
label1.Text = $"{p2_name} choose your opponents word...";
}
Any help would be great!